# content of conftest.py
import pytest
import os
from omegaconf import OmegaConf
import pymasep.common as common
from pymasep.utils import remove_recursive_file
from test.unit.mocks.game_mock import GameMock


@pytest.fixture(scope="function")
def base_directory_unit(relative_path, request):
    directory = os.path.dirname(request.node.fspath)
    yield os.path.join(directory, relative_path, 'unit')


@pytest.fixture(scope="function")
def base_directory(relative_path, request):
    directory = os.path.dirname(request.node.fspath)
    yield os.path.join(directory, relative_path)


def new_environment(cfg):
    env = common.Environment()
    game = GameMock(cfg=cfg)
    env.game = game
    return env


def new_game(cfg):
    game = GameMock(cfg=cfg)
    return game


@pytest.fixture(scope="function")
def mock_game():
    cfg = OmegaConf.create()
    cfg['root_path'] = '.'
    yield new_game(cfg=cfg)


@pytest.fixture(scope="function")
def mock_environment():
    yield new_environment(cfg=None)


@pytest.fixture(scope="function")
def mock_environment_conf(configuration):
    yield new_environment(configuration)


@pytest.fixture(scope="session", autouse=True)
def cleanup(request):
    """Cleanup a testing directory once we are finished."""

    def remove_test_dir():
        directory = os.path.dirname(os.path.realpath(__file__))
        remove_recursive_file(directory, '*.log')
        remove_recursive_file(directory, 'logs')

    request.addfinalizer(remove_test_dir)

# @pytest.fixture(scope="function", autouse=True)
# def cleanup(request):
#     """Cleanup a testing directory once we are finished."""
#
#     def remove_test_dir():
#         directory = os.path.dirname(os.path.realpath(__file__))
#         remove_recursive_file(directory, '*.log')
#         remove_recursive_file(directory, 'logs')
#
#     request.addfinalizer(remove_test_dir)
