import copy
import glob
import os
import unittest

from integration.partial.integration_test_utils import IntegrationTestUtils


class TestAdvisory(unittest.TestCase):

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

    @classmethod
    def setUpClass(cls) -> None:
        TestAdvisory.write_temp_run_config_files()
        TestAdvisory.unpack_dependencies()
        TestAdvisory.run_advisory_pipeline()


    @staticmethod
    def write_temp_run_config_files():
        nowstring: str = IntegrationTestUtils.get_now_string()
        prefix: str = "temp_advisory_" + nowstring
        # 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)
        TestAdvisory.TEST_OUT_PATH = run_dict['WorkspacePathout'] + prefix + os.sep
        run_dict['WorkspacePathout'] = TestAdvisory.TEST_OUT_PATH
        run_dict['WorkspacePath'] = TestAdvisory.TEST_OUT_PATH
        run_dict['ServerName'] = ''  # nothing, as local machine

        IntegrationTestUtils.write_json_file(run_dict, IntegrationTestUtils.TEMP_CONFIG_FILE_PATH)


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


    @staticmethod
    def run_advisory_pipeline():
        component = 'Advisory'
        IntegrationTestUtils.run_unittest_pipeline(component, TestAdvisory.TEST_START_DATE)


    def test_standard_run_input_status_success(self):
        status_file_path = os.path.join(TestAdvisory.TEST_OUT_PATH, TestAdvisory.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_docs_produced(self):

        east_africa_image_path = os.path.join(TestAdvisory.TEST_OUT_PATH, TestAdvisory.TEST_JOB_DIR,
                                              "tight-layout",
                                              "wheat_rust_advisory_template_EastAfrica_20221001.docx")
        ethiopia_image_path = os.path.join(TestAdvisory.TEST_OUT_PATH, TestAdvisory.TEST_JOB_DIR,
                                           "tight-layout",
                                           "wheat_rust_advisory_template_Ethiopia_20221001.docx")

        ea_file_exists: bool = os.path.isfile(east_africa_image_path)
        eth_file_exists: bool = os.path.isfile(ethiopia_image_path)
        self.assertTrue(ea_file_exists)
        self.assertTrue(eth_file_exists)

    def test_standard_run_input_images_produced(self):

        east_africa_image_wildcard = os.path.join(TestAdvisory.TEST_OUT_PATH, TestAdvisory.TEST_JOB_DIR,
                                                  "images",
                                                  "*eastafrica*.png")
        ethiopia_image_wildcard = os.path.join(TestAdvisory.TEST_OUT_PATH, TestAdvisory.TEST_JOB_DIR,
                                               "images",
                                               "*ethiopia*.png")

        africa_image_count: int = len(glob.glob(east_africa_image_wildcard))
        ethiopia_image_count: int = len(glob.glob(ethiopia_image_wildcard))
        self.assertEqual(3, africa_image_count)
        self.assertEqual(3, ethiopia_image_count)

    def test_standard_run_input_shapefiles_produced(self):

        east_africa_image_wildcard = os.path.join(TestAdvisory.TEST_OUT_PATH, TestAdvisory.TEST_JOB_DIR,
                                                  "images",
                                                  "*eastafrica*.shp")

        africa_image_count: int = len(glob.glob(east_africa_image_wildcard))
        self.assertEqual(3, africa_image_count)


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