from pytest import fixture  # , raises
from pathlib import Path
from rougail import Rougail
from rougail.output_display import RougailOutputDisplay as RougailOutput

from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config


EXT = {'console': 'sh', 'github': 'md', 'gitlab': 'gitlab.md'}


excludes = [
]
#excludes = [
#    '60_5family_dynamic_calc_identifier',
#]
WITH_DONE = False
#WITH_DONE = True

HERE = Path(__file__).parent

if WITH_DONE:
    test_ok = get_structures_list(excludes, HERE)
else:
    test_ok = get_structures_list(excludes)
# test_ok = [Path('../rougail-tests/structures/00_6boolean')]


def idfn(fixture_value):
    return fixture_value.name


@fixture(scope="module", params=test_ok, ids=idfn)
def test_dir(request):
    return request.param


def gen_cases():
    for namespace in [False, True]:
        for read_write in [False, True]:
    #        for mandatory in [False, True]:
            for show_secrets in [False, True]:
                yield namespace, read_write, show_secrets


def _test_structural_files(test_dir):
    for namespace, read_write, show_secrets in gen_cases():
        rougailconfig = get_rougail_config(test_dir, namespace)
        if not rougailconfig:
            return
        rougailconfig['step.output'] = 'display'
        rougailconfig["display.console.max_width"] = 120
        for do_calc in [False, True]:
            rougail = Rougail(rougailconfig)
            config = rougail.run()
            config.information.set("description_type", "description")
            if do_calc:
                get_values_for_config(config)
#            for read_write, show_secrets in gen_cases():
            if not do_calc and not read_write:
                continue
#            print(read_write, mandatory, show_secrets, do_calc)
            for output_format, ext in EXT.items():
                rougailconfig['display.output_format'] = output_format
#                rougailconfig["display.mandatory"] = mandatory
                rougailconfig["display.show_secrets"] = show_secrets
                ##################################
                dir_name = 'test'
                if namespace:
                    dir_name += '_namespace'
                elif (test_dir / 'force_namespace').is_file():
                    return
                ##################################
                if read_write:
                    dir_name += '_read_write'
#                if mandatory:
#                    dir_name += '_mandatory'
                if not show_secrets:
                    dir_name += '_secrets'
                if not do_calc:
                    dir_name += '_errors'
                output_file = Path(__file__).parent / 'results' / dir_name / (test_dir.name + "." + ext)
                ##################################
                if read_write:
                    config.property.read_write()
                else:
                    config.property.read_only()
                generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()[1]
                if not output_file.is_file():
                    if not output_file.parent.is_dir():
                        output_file.parent.mkdir()
                    with output_file.open('w') as outfh:
                        outfh.write(generated_output)
                with output_file.open() as outfh:
                     attented_output = outfh.read()
                assert generated_output == attented_output, f'filename {output_file}'
    if WITH_DONE:
        with (HERE / (test_dir.name + ".DONE")).open("w") as fh:
            fh.write('')


def test_structural_files_display(test_dir):
    _test_structural_files(test_dir)
#
#
#def test_structural_files_display_no_namespace(test_dir):
#    _test_structural_files(test_dir, False)
