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 = 'sh'


excludes = []
#excludes = [
#    '60_2family_dynamic_jinja_fill_sub_group',
#    '60_2family_dynamic_jinja_fill_sub_group_2',
#]

test_ok = get_structures_list(excludes)
# test_ok = [Path('../rougail-tests/structures/04_1auto_save_and_hidden')]


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 read_write in [False, True]:
        for mandatory in [False, True]:
            for show_secrets in [False, True]:
                yield read_write, mandatory, show_secrets


def _test_structural_files(test_dir, namespace):
    rougailconfig = get_rougail_config(test_dir, namespace)
    if not rougailconfig:
        return
    rougailconfig['step.output'] = 'display'
    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, mandatory, show_secrets in gen_cases():
            if not do_calc and (mandatory or not read_write):
                continue
#            print(read_write, mandatory, show_secrets, do_calc)
            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}'


def test_structural_files_display(test_dir):
    _test_structural_files(test_dir, True)


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