import os
import json
import io

fName = os.path.realpath(__file__)

CONTRIB_INFO = ['affiliation', 'location', 'email', 'url', 'ORCID']
CASEHISTORY_INFO = ['citations', 'contributors', 'tags']

ORCID_URL = 'http://orcid.org/'

THIS_IS_AUTOGENERATED = (
    ".. --------------------------------- ..\n"
    "..                                   ..\n"
    "..    THIS FILE IS AUTO GENEREATED   ..\n"
    "..                                   ..\n"
    "..    autodoc.py                     ..\n"
    "..                                   ..\n"
    ".. --------------------------------- ..\n"
)


def make_contributorslist(fpath='contributors.json',
                          fout='contributors.rst',
                          contrib_info=CONTRIB_INFO):

    fpath = os.path.sep.join(fName.split(os.path.sep)[:-2] + [fpath])
    fout = os.path.sep.join(fName.split(os.path.sep)[:-2] + [fout])

    fpath = open(fpath)  # file to write to
    contribs = json.load(fpath)  # contributors json
    keys = contribs.keys()

    # sort by last name
    last_names = []
    for key, val in iter(contribs.items()):
        if 'name' not in val:
            raise Exception('{} has no name!?'.format(keys))
        last_names.append(val['name'].split(' ')[-1])

    # # get relavent info
    # contrib_info = list(
    #     set([item for entries in contribs.itervalues() for item in entries]
    #         ).difference(set(['avatar']))
    #     )

    last_names = zip(last_names, contribs.keys())
    sorted_names = sorted(last_names)

    out = u"""
{}

.. _contibutors:

Contributors
============

""".format(THIS_IS_AUTOGENERATED)

    print('\nCreating: contributors.rst')
    with io.open(fout, 'w', encoding='utf-8') as f:
        f.write(out)

        for _, key in sorted_names:

            contrib = contribs[key]

            html_block = []
            for info_key in contrib_info:
                if info_key in contrib:
                    # if info_key == 'ORCID':
                    #     html = """
                    #         <strong>ORCID:</strong><a class="reference external" href="http://orcid.org/{val}">{val}</a><br>
                    #     """.format(val=contrib[info_key])
                    # val = contrib[info_key]
                    # if info_key == 'ORCID':
                    #     val = "`{val} <{url}>`_".format(val=val, url=ORCID_URL+val)
                    val = contrib[info_key]
                    if info_key == 'ORCID':
                                    htmlval = """
        <a class="reference external" href="{url}">{orcid}</a>
                        """.format(url=ORCID_URL + val, orcid=val)
                    # a website
                    elif 'http' in val or 'www' in val:
                        if 'www' in val and 'http' not in val:
                            val = 'http://' + val
                        htmlval = """
        <a class="reference external" href="{url}">{url}</a>
                        """.format(url=val)
                    # an email
                    elif '@' in val:
                        htmlval = """
        <a class="reference external" href="mailto:{email}">{email}</a>
                        """.format(email=val)
                    # otherwise assume it is text
                    else:
                        htmlval = val

                    htmlval = """
        <strong>{key}:</strong> {htmlval}
                              """.format(key=info_key, htmlval=htmlval)

                    html_block.append(htmlval)

            # join the block
            html_block = '<br>'.join(html_block)

            if 'avatar' in contrib:
                avatar = u"""
        <a class="reference internal image-reference" href="{avatar}"><img alt="{avatar}" class="align-left" src="{avatar}" style="width: 120px; border-radius: 10px; vertical-align: text-middle padding-left="20px" /></a>
                """.format(avatar=contrib['avatar'])

            else:
                avatar = u""

            out = u"""

.. _{id}:

{name}
{underline}

.. raw:: html

    <div class="row" style="min-height: 170px">
    <div class="col-md-4">
        {avatar}
    </div>
    <div class="col-md-6" style="line-height: 1.5">
        {html_block}
    </div>
    <br><br>
    </div>


            """.format(id=key,
                       name=contrib['name'],
                       underline='-'*len(contrib['name']),
                       namepermalink=key,
                       par='&para;',
                       avatar=avatar,
                       html_block=html_block
                       )

            f.write(out)

        f.close()

    print('Done writing contributors.rst\n')


def make_case_histories(fpath='content/case_histories/case_histories.json',
                        fout='content/case_histories/case_histories.rst',
                        casehistory_info=CASEHISTORY_INFO):

    fpath = os.path.sep.join(fName.split(os.path.sep)[:-2] + fpath.split('/'))
    fout = os.path.sep.join(fName.split(os.path.sep)[:-2] + fout.split('/'))

    fpath = open(fpath)  # file to write to
    casehistories = json.load(fpath)  # casehistories json

    out = u"""

{}


""".format(THIS_IS_AUTOGENERATED)

    print('Creating: case_histories.html')
    f = open(fout, 'w')
    f.write(out)

    toctree = u"""
    """.join(
        ["{}/index".format(key) for key in casehistories.keys()]
    )

    toctreeblock = u"""
.. toctree::
    :maxdepth: 1
    :hidden:

    {toctree}
    """.format(toctree=toctree)
    f.write(toctreeblock)

    f.write("""


Gallery
-------
    """)

    for key in casehistories.keys():
        casehistory = casehistories[key]

        if 'citation' in casehistory:
            reference_block="- {citations}".format(
                citations=[':cite:`citation`' for citation in casehistory['citations']])
        else:
            reference_block=""

        if 'contributors' in casehistory:
            contrib_dict = {
                'authors': [],
                'editors': [],
                'reviewers': []}

            for contrib in casehistory['contributors']:
                contrib_style = contrib['as']

                contributor = ':ref:`{}`'.format(
                        contrib['uid'].split(':')[1]
                        )
                if contrib_style in contrib_dict:
                    contrib_dict[contrib_style] += ', ' + contributor
                else:
                    contrib_dict[contrib_style] = contributor

            contributions = ['    - {contrib_style}: {contribs}'.format(
                contrib_style=contrib_style, contribs=val
                ) for contrib_style, val in iter(contrib_dict.items()) if val]

            contributions = '\n'.join(contributions)

            contributors_block=u"""
- Contributors
{contributions}
""".format(contributions=contributions)

        if 'tags' in casehistory:
            tags_dict = {}
            for tags in casehistory['tags']:
                tags_style = tags['as'].replace('_', ' ')

                if tags_style in tags_dict:
                    tags_dict[tags_style] += ', '+tags['uid'].replace('_', ' ')
                else:
                    tags_dict[tags_style] = tags['uid'].replace('_', ' ')

            tags_list = ['    - {tags_style}: {tag}'.format(
                tags_style=tag_style, tag=val
                ) for tag_style, val in iter(tags_dict.items())]

            tags_list = '\n'.join(tags_list)

            tags_block=u"""
- Tags
{tags_list}
""".format(tags_list=tags_list)

        out="""

{title}
{underline}

.. image:: {thumbnail}
    :alt: {uid}
    :width: 260
    :align: right

- :ref:`{description} <{uid}_index>`
{references_block}
{contributors_block}
{tags_block}
|
|
|



        """.format(
            uid=key,
            title=casehistory['title'],
            description=(
                casehistory['description'] if 'description' in
                casehistory.keys() else
                casehistory['title']
            ),
            underline='^'*len(casehistory['title']),
            source=casehistory['source'],
            thumbnail=casehistory['thumbnail'],
            references_block=reference_block,
            contributors_block=contributors_block,
            tags_block=tags_block
            )

        f.write(out)

    f.close()

    print('Done writing case_histories.rst')


if __name__ == '__main__':
    """
        Run the following to create the formula sheet.
    """

    make_contributorslist()
    make_case_histories()
