from pathlib import Path
from rougail import Rougail
from tiramisu import MetaConfig
from rougail.output_display import RougailOutputDisplay as RougailOutput

from rougail_tests.utils import get_rougail_config


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


def test_layers():
    layer_datas = ['name1', 'name2', 'name3', 'name4']
    rougailconfig = get_rougail_config(Path("tests/layers/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run(name="source3")
    metaconfig1 = MetaConfig([config], name="source2")
    metaconfig2 = MetaConfig([metaconfig1], name="source1")
    metaconfig2.property.read_only()
    metaconfig1.owner.set(metaconfig1.path())
    metaconfig2.owner.set(metaconfig2.path())
    metaconfig2.information.set("description_type", "description")
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, layer_datas=layer_datas, root_config=metaconfig2, config_owner_is_path=True).run()
        assert no_pb == True
        output_file = Path(__file__).parent / 'layers-results' / f'display.{ext}'
        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 set_value(config):
    config.option("a_variable").value.set("a modified value")
    config.option("a_variable").information.set("loaded_from", config.name())


def test_layers_values_config2():
    layer_datas = ['name1', 'name2', 'name3', 'name4']
    rougailconfig = get_rougail_config(Path("tests/layers/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run(name="source3")
    metaconfig1 = MetaConfig([config], name="source2")
    metaconfig2 = MetaConfig([metaconfig1], name="source1")
    metaconfig2.property.read_only()
    metaconfig2.information.set("description_type", "description")
    metaconfig1.owner.set(metaconfig1.path())
    metaconfig2.owner.set(metaconfig2.path())
    set_value(config)
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, layer_datas=layer_datas, root_config=metaconfig2, config_owner_is_path=True).run()
        assert no_pb == True
        output_file = Path(__file__).parent / 'layers-values-config-results' / f'display.{ext}'
        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_layers_values_metaconfig1():
    layer_datas = ['name1', 'name2', 'name3', 'name4']
    rougailconfig = get_rougail_config(Path("tests/layers/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run(name="source3")
    metaconfig1 = MetaConfig([config], name="source2")
    metaconfig2 = MetaConfig([metaconfig1], name="source1")
    metaconfig2.property.read_only()
    metaconfig2.information.set("description_type", "description")
    metaconfig1.owner.set(metaconfig1.path())
    metaconfig2.owner.set(metaconfig2.path())
    set_value(metaconfig1)
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, layer_datas=layer_datas, root_config=metaconfig2, config_owner_is_path=True).run()
        assert no_pb == True
        output_file = Path(__file__).parent / 'layers-values-metaconfig1-results' / f'display.{ext}'
        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_layers_values_metaconfig2():
    layer_datas = ['name1', 'name2', 'name3', 'name4']
    rougailconfig = get_rougail_config(Path("tests/layers/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run(name="source3")
    metaconfig1 = MetaConfig([config], name="source2")
    metaconfig2 = MetaConfig([metaconfig1], name="source1")
    metaconfig2.information.set("description_type", "description")
    metaconfig1.owner.set(metaconfig1.path())
    metaconfig2.owner.set(metaconfig2.path())
    set_value(metaconfig2)
    metaconfig2.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, layer_datas=layer_datas, root_config=metaconfig2, config_owner_is_path=True).run()
        assert no_pb == True
        output_file = Path(__file__).parent / 'layers-values-metaconfig2-results' / f'display.{ext}'
        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_layers_values_metaconfig_mix():
    layer_datas = ['name1', 'name2', 'name3', 'name4']
    rougailconfig = get_rougail_config(Path("tests/layers/"), namespace=False)
    rougailconfig = get_rougail_config(Path("tests/layers/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run(name="source3")
    metaconfig1 = MetaConfig([config], name="source2")
    metaconfig2 = MetaConfig([metaconfig1], name="source1")
    metaconfig2.information.set("description_type", "description")
    metaconfig1.owner.set(metaconfig1.path())
    metaconfig2.owner.set(metaconfig2.path())
    set_value(config)
    set_value(metaconfig1)
    set_value(metaconfig2)
    metaconfig2.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, layer_datas=layer_datas, root_config=metaconfig2, config_owner_is_path=True).run()
        assert no_pb == True
        output_file = Path(__file__).parent / 'layers-values-metaconfig-mix-results' / f'display.{ext}'
        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}'
