#!/usr/bin/env python3

import sys
from json import dump, loads
from pathlib import Path
from ruamel.yaml import YAML
from ansi2html import Ansi2HTMLConverter

from rougail import Rougail
from rougail.config import get_rougail_config
from rougail.output_console import RougailOutputConsole
from rougail.user_data_yaml import RougailUserDataYaml
from rougail.output_doc import RougailOutputDoc


PREVIOUS = Path("previous.yml")


if __name__ == "__main__":
    commit_id = sys.argv[1]
#    rougailconfig = RougailConfig.copy()
    rougailconfig = get_rougail_config(backward_compatibility=False, add_extra_options=False)
    rougailconfig['main_structural_directories'] = ['firefox']
    if Path('config/namespace').is_file():
        has_namespace = True
        has_foxyproxy = False
    elif Path('foxyproxy').is_dir():
        has_foxyproxy = True
        has_namespace = True
        rougailconfig['main_namespace'] = 'Firefox'
        rougailconfig['extra_namespaces'] = {'FoxyProxy': ['foxyproxy']}
    else:
        has_foxyproxy = False
        has_namespace = False
        rougailconfig['main_namespace'] = None
#    rougailconfig['tiramisu_cache'] = "cache.py"
    if Path('mode').is_file():
        rougailconfig['modes_level'] = ['basic', 'standard', 'advanced']
        has_mode = True
    else:
        has_mode = False
    rougail = Rougail(rougailconfig)
    config = rougail.run()
#    print(config.value.get())
    rougailconfig['step.output'] = 'doc'
    rougailconfig['doc.output_format'] = 'github'
    rougailconfig['doc.title_level'] = 3
    inventory = RougailOutputDoc(config, rougailconfig=rougailconfig.copy())
    inventory.load_formatter()
    inv_formatter = inventory.formatter
    rougailconfig['step.output'] = 'console'
    summary = Path('summary_before.md')
    if summary.is_file():
        with summary.open('r') as sfh:
            doc = sfh.read() + '\n\n'
    else:
        doc = ''
    yaml = YAML()
    namespace = 'firefox'
    file_found = False
    if rougailconfig['main_namespace']:
        all_dirs = [[rougailconfig['main_structural_directories']], [['foxyproxy']]]
    else:
        all_dirs = [[rougailconfig['main_structural_directories']]]

    doc += inv_formatter.title('Screenshot', 2) + '\n'
    doc += f'<img src="{namespace}.png" width=50% height=50% alt="{namespace.capitalize()} Proxy setting"/>\n\n'
#    with doc_file.open('r') as docfh:
#        doc += docfh.read()
    doc += '\n' + inv_formatter.title('Structure', 2) + '\n'
    print(all_dirs)
    for r in all_dirs:
        for dirs in r:
            for d in dirs:
                print(d)
                files = list(Path(d).iterdir())
                files.sort()
                for f in files:
                    namespace = f.parent.name
                    file_found = True
                    if f.name.endswith('.yml') or f.name.endswith('.yaml'):
                        doc += inv_formatter.title(str(f), 3)
                        doc += '\n```yml\n'
                        with f.open(encoding="utf8") as file_fh:
                            doc += file_fh.read()
                        doc += '\n```\n'
    rougailconfig['step.output'] = 'doc'
    inv_doc = inventory.run()[1]
    if file_found and inv_doc:
        doc += inv_formatter.title('Generated documentation', 3)
        CMD = f"foo@bar:~$ git switch --detach {commit_id}\n"
        CMD += "foo@bar:~$ rougail -m firefox/ "
        if has_namespace:
            CMD += "-s Firefox "
        if has_foxyproxy:
            CMD += "-xn FoxyProxy -xd 0 foxyproxy/ "
        if has_mode:
            CMD += "--modes_level basic standard advanced "
        cmd = CMD + "-o doc -do github"
        doc += f'```console\n{cmd}\n```\n'
        doc += inv_doc
    if PREVIOUS.is_file():
        rougailconfig["doc.title_level"] = 5
        rougailconfig["doc.contents"] = ["changelog"]
        rougailconfig["doc.previous_json_file"] = str(PREVIOUS)
    inv_doc = inventory.run()[1]
    if file_found and inv_doc:
        doc += inv_formatter.title('Changelog', 3)
        cmd = CMD + "-o doc --doc.contents variables changelog -do github"
        doc += f'```console\n{cmd}\n```\n'
        doc += inv_doc
    # save PREVIOUS
    rougailconfig["doc.contents"] = ["variables"]
    rougailconfig['doc.output_format'] = 'json'
    data = inventory.run()[1]
    if data:
        with PREVIOUS.open('w') as fh:
            dump(loads(data), fh)
    #
    rougailconfig['step.output'] = 'console'
    config = Path('config')
    if config.is_dir():
        doc += inv_formatter.title('User data', 2)
        configs = list(config.iterdir())
        configs.sort()
        for idx, dirname in enumerate(configs):
            if dirname.is_file():
                continue
            for filename in dirname.iterdir():
                if filename.name.endswith('.yml') or filename.name.endswith('.yaml'):
                    doc += inv_formatter.title(f'Example {idx + 1}', 3)
                    doc += inv_formatter.title(str(filename), 4)
                    doc += '\n```yml\n'
                    with filename.open(encoding="utf8") as file_fh:
                        doc += file_fh.read()
                    doc += '\n```\n'
#                    with filename.open(encoding="utf8") as file_fh:
#                        objects = yaml.load(file_fh)
#                    doc += inv_formatter.yaml(objects)
                    #readme = dirname / 'README.md'
                    #if readme.is_file():
                    #    doc += inv_formatter.title('Description', 4)
                    #    with readme.open() as fh:
                    #        doc += fh.read() + '\n\n'
                    rougail = Rougail(rougailconfig)
                    tiramisu_config = rougail.run()
                    rougailconfig['step.user_data'] = ['yaml']
                    rougailconfig['yaml.filename'] = [str(filename)]
                    data = RougailUserDataYaml(tiramisu_config, rougailconfig=rougailconfig)
                    ret = data.run()
                    errors = rougail.user_datas(ret)
                    export = RougailOutputConsole(tiramisu_config,
                                                  rougailconfig=rougailconfig,
                                                  user_data_errors=errors['errors'],
                                                  user_data_warnings=errors['warnings'],
                                                  )
                    console = export.run()[1]
                    conv = Ansi2HTMLConverter(inline=True)
                    doc += inv_formatter.title('Output', 4)
                    cmd = CMD + f"-u yaml -ff {filename}"
                    doc += f"```console\n{cmd}\n```\n"
                    conv_data = '<pre>' + conv.convert(console, full=False) + "</pre>\n"
                    doc += conv_data
                    with open(dirname / 'output_ro.html', 'w') as fh_output:
                        fh_output.write(conv_data)
                    if (config / 'read_write').is_file():
                        tiramisu_config.property.read_write
                        export = RougailOutputConsole(tiramisu_config,
                                                      rougailconfig=rougailconfig,
                                                      user_data_errors=data.errors,
                                                      user_data_warnings=data.warnings,
                                                      )
                        console = export.run()[1]
                        conv = Ansi2HTMLConverter(inline=True)
                        doc += inv_formatter.title('Output in read write mode', 4)
                        cmd = cmd + " --cli.read_write"
                        doc += f"```console\n{cmd}\n```\n"
                        conv_data = '<pre>' + conv.convert(console, full=False) + "</pre>\n"
                        doc += '<pre>' + conv_data + "</pre>\n"
                        with open(dirname / 'output_rw.html', 'w') as fh_output:
                            fh_output.write(conv_data)
    #
    summary = Path('summary_after.md')
    if summary.is_file():
        with summary.open('r') as sfh:
            doc += '\n\n' + sfh.read()
    #
    doc_file = Path('README.md')
    with doc_file.open('w') as docfh:
        docfh.write(doc)
    with doc_file.open('w') as docfh:
        docfh.write(doc)
