# Path: termicoder/models/Problem.py
# class Problem(ABC):
#     @abstractmethod
#     def __init__(self, data):
#         self.name = None
#         self.status = None
#         self.submissions_count = 0
#         self.data = data
#         self.code = None
#         self.html = None
#         self.testcases = None
#         self.contest_code = None
#         self.judge_name = None
#         self.timelimit = 3.0  # in seconds
# 
#     # used by list
#     @abstractmethod
#     def __str__(self):
#         pass
# 
# Path: termicoder/models/Contest.py
# class Contest(ABC):
#     @abstractmethod
#     def __init__(self, data=None):
#         # If contest_data, problems is passed, it should take priority
#         self.code = None
#         self.problems = None
#         self.judge_name = None
#         self.data = data
# 
#     @abstractmethod
#     def __str__(self):
#         pass
# 
# Path: termicoder/models/Testcase.py
# class Testcase(ABC):
#     @abstractmethod
#     def __init__(self, ans, inp, code):
#         self.ans = ans
#         self.inp = inp
#         self.code = code
# 
#     # judges can override this if they want
#     # this is used to produce diff on termicoder test
#     def diff(self, out):
#         ans_file_name = self.code + ".ans"
#         out_file_name = self.code + ".out"
#         ans_list = [x+'\n' for x in self.ans.split('\n')]
#         out_list = [x+'\n' for x in out.split('\n')]
#         diffobj = icdiff.ConsoleDiff(line_numbers=True, show_all_spaces=True)
# 
#         if(ans_list == out_list):
#             return None
#         else:
#             return '\n'.join(diffobj.make_table(
#                              out_list, ans_list, fromdesc=out_file_name,
#                              todesc=ans_file_name, context=False, numlines=10))
# 
# Path: termicoder/utils/logging.py

# 
# Path: termicoder/utils/yaml.py
# def write(file_path, key, value):
#     existing_data = read(file_path, None)
#     if existing_data is None:
#         existing_data = {}
# 
#     if key is not None:
#         existing_data[key] = value
#     else:
#         existing_data = value
#     data_file = click.open_file(file_path, 'w')
#     try:
#         logger.debug("writing data to file %s" % file_path)
#         logger.debug(existing_data)
#         yaml.dump(data=existing_data, stream=data_file, Dumper=Dumper)
#     except yaml.YAMLError:
#         raise


from ..models import Problem
from ..models import Contest
from ..models import Testcase
from .logging import logger
from ..utils.yaml import write
from builtins import FileExistsError
import click
import os


def output_testcase(testcase, testcase_dir):
    try:
        assert isinstance(testcase, Testcase)
        directory_path = testcase_dir
        inp_path = os.path.join(
            directory_path, '%s.in' % testcase.code)
        ans_path = os.path.join(
            directory_path, '%s.ans' % testcase.code)
        logger.debug(inp_path)
        logger.debug(ans_path)
        os.makedirs(directory_path)
    except FileExistsError:
        pass
    except AssertionError:
        logger.debug(type(testcase))
        raise
    except BaseException:
        raise
    inp_file = click.open_file(inp_path, 'w')
    ans_file = click.open_file(ans_path, 'w')
    logger.debug('writing testcase %s for problem' % (testcase.code))
    inp_file.write(testcase.inp)

# Path: termicoder/models/Problem.py
# class Problem(ABC):
#     @abstractmethod
#     def __init__(self, data):
#         self.name = None
#         self.status = None
#         self.submissions_count = 0
#         self.data = data
#         self.code = None
#         self.html = None
#         self.testcases = None
#         self.contest_code = None
#         self.judge_name = None
#         self.timelimit = 3.0  # in seconds
# 
#     # used by list
#     @abstractmethod
#     def __str__(self):
#         pass
# 
# Path: termicoder/models/Contest.py
# class Contest(ABC):
#     @abstractmethod
#     def __init__(self, data=None):
#         # If contest_data, problems is passed, it should take priority
#         self.code = None
#         self.problems = None
#         self.judge_name = None
#         self.data = data
# 
#     @abstractmethod
#     def __str__(self):
#         pass
# 
# Path: termicoder/models/Testcase.py
# class Testcase(ABC):
#     @abstractmethod
#     def __init__(self, ans, inp, code):
#         self.ans = ans
#         self.inp = inp
#         self.code = code
# 
#     # judges can override this if they want
#     # this is used to produce diff on termicoder test
#     def diff(self, out):
#         ans_file_name = self.code + ".ans"
#         out_file_name = self.code + ".out"
#         ans_list = [x+'\n' for x in self.ans.split('\n')]
#         out_list = [x+'\n' for x in out.split('\n')]
#         diffobj = icdiff.ConsoleDiff(line_numbers=True, show_all_spaces=True)
# 
#         if(ans_list == out_list):
#             return None
#         else:
#             return '\n'.join(diffobj.make_table(
#                              out_list, ans_list, fromdesc=out_file_name,
#                              todesc=ans_file_name, context=False, numlines=10))
# 
# Path: termicoder/utils/logging.py

# 
# Path: termicoder/utils/yaml.py
# def write(file_path, key, value):
#     existing_data = read(file_path, None)
#     if existing_data is None:
#         existing_data = {}
# 
#     if key is not None:
#         existing_data[key] = value
#     else:
#         existing_data = value
#     data_file = click.open_file(file_path, 'w')
#     try:
#         logger.debug("writing data to file %s" % file_path)
#         logger.debug(existing_data)
#         yaml.dump(data=existing_data, stream=data_file, Dumper=Dumper)
#     except yaml.YAMLError:
#         raise


# Path: termicoder/utils/setup.py
from ..models import Problem
from ..models import Contest
from ..models import Testcase
from .logging import logger
from ..utils.yaml import write
from builtins import FileExistsError
import click
import os



def output_testcase(testcase, testcase_dir):
    try:
        assert isinstance(testcase, Testcase)
        directory_path = testcase_dir
        inp_path = os.path.join(
            directory_path, '%s.in' % testcase.code)
        ans_path = os.path.join(
            directory_path, '%s.ans' % testcase.code)
        logger.debug(inp_path)
        logger.debug(ans_path)
        os.makedirs(directory_path)
    except FileExistsError:
        pass
    except AssertionError:
        logger.debug(type(testcase))
        raise
    except BaseException:
        raise
    inp_file = click.open_file(inp_path, 'w')
    ans_file = click.open_file(ans_path, 'w')
    logger.debug('writing testcase %s for problem' % (testcase.code))
    inp_file.write(testcase.inp)

    ans_file.write(testcase.ans)