import os
from pytest import fixture  # , raises
from pathlib import Path
from rougail import Rougail
from rougail.output_doc import RougailOutputDoc as RougailOutput

from rougail_tests.utils import get_structures_list, get_rougail_config, get_values_for_config


excludes = []
excludes = [
    '60_5family_dynamic_unknown_suffix',
    '60_5family_dynamic_variable_outside_sub_suffix',
]

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


os.environ['COLUMNS'] = '80'


def idfn(fixture_value):
    return fixture_value.name


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


def _test_dictionaries(test_dir, namespace, ext, *, examples=False):
    rougailconfig = get_rougail_config(test_dir, namespace)
    if not rougailconfig:
        return
    ##################################
    rougailconfig['step.output'] = 'doc'
    rougailconfig['doc.output_format'] = ext
    ext = {'github': 'md', 'asciidoc': 'adoc', 'json': 'json', 'console': 'sh'}.get(ext)
    if examples:
        rougailconfig['doc.example'] = True
    else:
        rougailconfig['doc.example'] = False
    ##################################
    dir_name = 'test'
    if namespace:
        dir_name += '_namespace'
    elif (test_dir / 'force_namespace').is_file():
        return
    ##################################
    if examples:
        dir_name += '_examples'
    ##################################
    rougail = Rougail(rougailconfig)
    config = rougail.run()
    generated_output = RougailOutput(config, rougailconfig=rougailconfig).run()[1]
    output_file = Path(__file__).parent / 'results' / dir_name / (test_dir.name + "." + 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_dictionaries_json(test_dir):
    _test_dictionaries(test_dir, True, 'json')


def test_dictionaries_console(test_dir):
    _test_dictionaries(test_dir, True, 'console')


def test_dictionaries_github(test_dir):
    _test_dictionaries(test_dir, True, 'github')


def test_dictionaries_asciidoc(test_dir):
    _test_dictionaries(test_dir, True, 'asciidoc')

################
def test_dictionaries_examples(test_dir):
    _test_dictionaries(test_dir, True, 'github', examples=True)

################
def test_dictionaries_json_no_namespace(test_dir):
    _test_dictionaries(test_dir, False, 'json')


def test_dictionaries_console_no_namespace(test_dir):
    _test_dictionaries(test_dir, False, 'console')


def test_dictionaries_github_no_namespace(test_dir):
    _test_dictionaries(test_dir, False, 'github')


def test_dictionaries_asciidoc_no_namespace(test_dir):
    _test_dictionaries(test_dir, False, 'asciidoc')

################
def test_dictionaries_examples_no_namespace(test_dir):
    _test_dictionaries(test_dir, False, 'github', examples=True)
