import copy
import os
import unittest

from integration.partial.integration_test_utils import IntegrationTestUtils


class TestEpi(unittest.TestCase):

    TEST_OUT_PATH: str = "not_set"
    TEST_START_DATE: str = '20221001'
    TEST_JOB_DIR: str = "EPI_" + TEST_START_DATE

    @classmethod
    def setUpClass(cls) -> None:
        TestEpi.write_temp_run_config_files()
        TestEpi.unpack_dependencies()
        TestEpi.run_epi_pipeline()


    @staticmethod
    def write_temp_run_config_files():
        nowstring: str = IntegrationTestUtils.get_now_string()
        prefix: str = "temp_epi_" + nowstring
        # prefix: str = "temp_epi_2022-11-23_164339"
        # prefix: str = ""

        default_config = IntegrationTestUtils.DEFAULT_CONFIG_FILE_PATH
        default_config_dict: dict = IntegrationTestUtils.load_json_file(default_config)
        run_dict: dict = copy.deepcopy(default_config_dict)
        TestEpi.TEST_OUT_PATH = run_dict['WorkspacePathout'] + prefix + os.sep
        run_dict['WorkspacePathout'] = TestEpi.TEST_OUT_PATH
        run_dict['WorkspacePath'] = TestEpi.TEST_OUT_PATH
        run_dict['ServerName'] = ''  # nothing, as local machine

        IntegrationTestUtils.write_json_file(run_dict, IntegrationTestUtils.TEMP_CONFIG_FILE_NAME)


    @staticmethod
    def unpack_dependencies():
        IntegrationTestUtils.unpack_zip(IntegrationTestUtils.EXAMPLE_SURVEY_FILE_PATH, TestEpi.TEST_OUT_PATH)
        IntegrationTestUtils.unpack_zip(IntegrationTestUtils.EXAMPLE_DEPO_FILE_PATH, TestEpi.TEST_OUT_PATH)
        IntegrationTestUtils.unpack_zip(IntegrationTestUtils.EXAMPLE_ENV_SUIT_FILE_PATH, TestEpi.TEST_OUT_PATH)


    @staticmethod
    def run_epi_pipeline():
        component = 'Epidemiology'
        IntegrationTestUtils.run_unittest_pipeline(component, TestEpi.TEST_START_DATE)


    def test_standard_run_input_status_success(self):
        status_file_path = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR, "STATUS_SUCCESS")
        success_file_exists: bool = os.path.isfile(status_file_path)
        self.assertTrue(success_file_exists)

    def test_standard_run_input_all_stem_rust_files_produced(self):

        alpha_beta_gamma_root = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR,
                                             "EastAfrica",
                                             "StemRust",
                                             "*psbeta0.004gamma0.00025alpha1.0")

        self.check_wildcard_exists_and_not_empty(alpha_beta_gamma_root, ".csv")
        self.check_wildcard_exists_and_not_empty(alpha_beta_gamma_root, "*comparison.png")
        self.check_wildcard_exists_and_not_empty(alpha_beta_gamma_root, "*progression.csv")
        self.check_wildcard_exists_and_not_empty(alpha_beta_gamma_root, ".png")
        self.check_wildcard_exists_and_not_empty(alpha_beta_gamma_root, ".tif")

        env_root = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR,
                                "EastAfrica",
                                "StemRust",
                                "infections_temp_config_Epidemiology")

        self.check_wildcard_exists_and_not_empty(env_root, "*env.csv")
        self.check_wildcard_exists_and_not_empty(env_root, "*env.png")
        self.check_wildcard_exists_and_not_empty(env_root, "*env.tif")

        data_input_root = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR,
                                       "EastAfrica",
                                       "StemRust",
                                       "input_data",
                                       "data_input*")

        self.check_wildcard_exists_and_not_empty(data_input_root, "*deposition.csv")
        self.check_wildcard_exists_and_not_empty(data_input_root, "*environment.csv")

        map_spam_root = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR,
                                     "EastAfrica",
                                     "StemRust",
                                     "input_data",
                                     "wheat_area_frac_MapSPAM2010_EastAfrica*")

        self.check_wildcard_exists_and_not_empty(map_spam_root, ".csv")
        self.check_wildcard_exists_and_not_empty(map_spam_root, ".csv")

    def test_standard_run_input_all_plotting_files_produced(self):

        input_csvs_root = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR,
                                       "plotting",
                                       "input_csvs",
                                       "*stem*")

        self.check_wildcard_exists_and_not_empty(input_csvs_root, ".csv")

        images_root = os.path.join(TestEpi.TEST_OUT_PATH, TestEpi.TEST_JOB_DIR,
                                   "plotting",
                                   "images",
                                   "*stem*")

        self.check_wildcard_exists_and_not_empty(images_root, "png")

    def check_wildcard_exists_and_not_empty(self, root: str, extension: str):
        file_exists: bool = IntegrationTestUtils.check_wildcard_exists_and_not_empty(root + extension)
        self.assertTrue(file_exists)


if __name__ == '__main__':
    unittest.main()