from pathlib import Path
from rougail import Rougail
from rougail.output_display import RougailOutputDisplay as RougailOutput
from rougail.user_data import mandatories

from rougail_tests.utils import get_rougail_config, fake_errors_for_user_data, fake_errors_for_user_data2, fake_dynamic_for_user_data, fake_for_user_data


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


def test_error_mandatory_hidden():
    rougailconfig = get_rougail_config(Path("tests/errors/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_write()
    errors = []
    mandatories(config, errors)
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        output_file = Path(__file__).parent / 'errors-results' / f'display.{ext}'
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors-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_error_mandatory_family_hidden():
    rougailconfig = get_rougail_config(Path("tests/errors2/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_write()
    errors = []
    mandatories(config, errors)
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors2-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_error_mandatory_family():
    rougailconfig = get_rougail_config(Path("tests/errors3/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_write()
    errors = []
    mandatories(config, errors)
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors3-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_error_not_valid_variable():
    rougailconfig = get_rougail_config(Path("tests/errors4/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    config.property.read_write()
    errors = []
    mandatories(config, errors)
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors4-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_error_not_valid_user_data():
    rougailconfig = get_rougail_config(Path("tests/errors5/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_errors_for_user_data(), invalid_user_data_error=True)
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors5-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_warning_not_valid_user_data():
    rougailconfig = get_rougail_config(Path("tests/errors5/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_errors_for_user_data())
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == True
        output_file = Path(__file__).parent / 'errors5-1-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_error_not_valid_dyn_user_data():
    rougailconfig = get_rougail_config(Path("tests/errors6/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_dynamic_for_user_data(), invalid_user_data_error=True, unknown_user_data_error=True)
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors6-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_error_not_valid_dynfamily_user_data():
    rougailconfig = get_rougail_config(Path("tests/errors6/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_for_user_data('dyn{{ identifier }}.var'), invalid_user_data_error=True, unknown_user_data_error=True)
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors8-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_error_not_valid_family_user_data():
    rougailconfig = get_rougail_config(Path("tests/errors7/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_for_user_data(), invalid_user_data_error=True, unknown_user_data_error=True)
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors7-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_warnings_not_valid_family_user_data():
    rougailconfig = get_rougail_config(Path("tests/errors7/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_for_user_data())
    config.property.read_write()
    mandatories(config, errors["errors"])
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors7-results' / f'display_warnings.{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_error_not_valid_user_data2():
    rougailconfig = get_rougail_config(Path("tests/errors9/"), namespace=False)
    rougailconfig['step.output'] = 'display'
    rougailconfig["display.console.max_width"] = 200
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    config.information.set("description_type", "description")
    errors = rougail.user_data(fake_errors_for_user_data2(), invalid_user_data_error=True)
    config.property.read_write()
    mandatories(config, errors["errors"])
    config.property.read_only()
    for output_format, ext in EXT.items():
        rougailconfig['display.output_format'] = output_format
        no_pb, generated_output = RougailOutput(config, rougailconfig=rougailconfig, user_data_errors=errors["errors"], user_data_warnings=errors["warnings"]).run()
        assert no_pb == False
        output_file = Path(__file__).parent / 'errors9-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}'
