# coding: utf-8

from __future__ import absolute_import
import os, shutil, tempfile

from pandas.testing import assert_frame_equal
from pandas import read_hdf

from jass.models.inittable import create_inittable_file

from . import JassTestCase


class TestInitTable(object):

    __test__ = False

    def setUp(self):
        # Create a temporary directory
        self.test_dir = tempfile.mkdtemp()
        input_data_path = self.get_file_path_fn("*chr*.txt")
        init_covariance_path = self.get_file_path_fn("COV.txt")
        regions_map_path = self.get_file_path_fn("regions.txt")
        description_file_path = self.get_file_path_fn("summary.csv")
        self.expected_hdf_path = self.get_file_path_fn("initTable.hdf5")
        init_table_path = os.path.join(self.test_dir, "test_init_table.hdf5")
        create_inittable_file(
            input_data_path,
            init_covariance_path,
            regions_map_path,
            description_file_path,
            init_table_path,
        )
        self.expected_phenolist = read_hdf(self.expected_hdf_path, "PhenoList")
        self.expected_sum_stat_tab = read_hdf(self.expected_hdf_path, "SumStatTab")
        self.expected_cov = read_hdf(self.expected_hdf_path, "COV")
        self.result_phenolist = read_hdf(init_table_path, "PhenoList")
        self.result_sum_stat_tab = read_hdf(init_table_path, "SumStatTab")
        self.result_cov = read_hdf(init_table_path, "COV")

    def tearDown(self):
        # Remove the directory after the test
        #shutil.rmtree(self.test_dir)
        print(self.test_dir)

    def test_compare_phenolist(self):
        """
        Compare result and expected PhenoList
        """
        assert_frame_equal(self.expected_phenolist, self.result_phenolist, check_like=True)

    def test_compare_sumstattab(self):
        """
        Compare result and expected SumStatTab
        """
        assert_frame_equal(self.expected_sum_stat_tab, self.result_sum_stat_tab, check_like=True)

    def test_compare_cov(self):
        """
        Compare result and expected COV
        """
        assert_frame_equal(self.expected_cov, self.result_cov, check_like=True)


params = [("nonans", "data_test1"), ("withnans", "data_test2")]

for name, param in params:
    cls_name = "TestInitTable_%s" % (name,)
    print(cls_name)
    globals()[cls_name] = type(
        cls_name,
        (TestInitTable, JassTestCase),
        {"test_folder": param, "__test__": True},
    )

if __name__ == "__main__":
    import unittest

    unittest.main()
