import copy
import os
import unittest

from ews.coordinator.processor_advisory import ProcessorAdvisory
from integration.partial.integration_test_utils import IntegrationTestUtils
from integration.test_suites.advisory_test_suite import BaseAdvisoryTestSuite


class TestAdvisory(BaseAdvisoryTestSuite.AdvisoryTestSuite):

    @classmethod
    def setUpClass(cls) -> None:
        """
        This is overridden in the class setup function to ensure that the value is not set from a previous run
        when being run in a TestSuite
        """
        IntegrationTestUtils.TEST_OUT_PATH = None

    def set_expected_values(self):
        IntegrationTestUtils.TEST_START_DATE = '20221001'
        IntegrationTestUtils.TEST_JOB_DIR = "ADVISORY_" + IntegrationTestUtils.TEST_START_DATE
        self.EXPECTED_EA_IMAGE_COUNT = 6
        self.EXPECTED_ETH_IMAGE_COUNT = 6
        self.EXPECTED_EA_SHAPE_COUNT = 3


    def setUp(self) -> None:

        IntegrationTestUtils.check_resources_exist()
        self.set_expected_values()

        if IntegrationTestUtils.TEST_OUT_PATH is None or not os.path.isdir(IntegrationTestUtils.TEST_OUT_PATH):
        # if True:
            TestAdvisory.write_temp_run_config_files()
            TestAdvisory.unpack_dependencies()
            TestAdvisory.run_advisory_pipeline()
        else:
            print(f"output in {IntegrationTestUtils.TEST_OUT_PATH} already written, skipping rerun")


    @staticmethod
    def write_temp_run_config_files():
        nowstring: str = IntegrationTestUtils.get_now_string()
        prefix: str = "temp_advisory_" + nowstring
        # prefix: str = "temp_advisory"

        IntegrationTestUtils.TEST_OUT_PATH = IntegrationTestUtils.generate_output_path(prefix)
        os.makedirs(IntegrationTestUtils.TEST_OUT_PATH, exist_ok = True)

        sys_config_path = IntegrationTestUtils.DEFAULT_SYS_CONFIG_FILE_PATH
        sys_config_dict: dict = IntegrationTestUtils.load_json_file(sys_config_path)
        sys_config_dict['WorkspacePathout'] = IntegrationTestUtils.TEST_OUT_PATH
        sys_config_dict['WorkspacePath'] = IntegrationTestUtils.TEST_OUT_PATH
        IntegrationTestUtils.RUN_SYS_CONFIG_FILE_PATH = IntegrationTestUtils.TEST_OUT_PATH + "temp_sys_config.json"
        IntegrationTestUtils.write_json_file(sys_config_dict, IntegrationTestUtils.RUN_SYS_CONFIG_FILE_PATH)

        run_config = IntegrationTestUtils.DEFAULT_ADVISORY_CONFIG_FILE_PATH
        run_config_dict: dict = IntegrationTestUtils.load_json_file(run_config)
        IntegrationTestUtils.TEST_OUT_PATH = IntegrationTestUtils.generate_output_path(prefix)

        # may be reusing a non-timestamped output file during development, so allow extant TEST_OUT_PATH
        os.makedirs(IntegrationTestUtils.TEST_OUT_PATH, exist_ok = True)
        IntegrationTestUtils.RUN_CONFIG_FILE_PATH = IntegrationTestUtils.TEST_OUT_PATH + "temp_config.json"
        IntegrationTestUtils.write_json_file(run_config_dict, IntegrationTestUtils.RUN_CONFIG_FILE_PATH)


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


    @staticmethod
    def run_advisory_pipeline():
        component = 'Advisory'
        short_name = 'ADVISORY'
        advisory_processor = ProcessorAdvisory()
        IntegrationTestUtils.run_partial_integration_test_pipeline(component,
                                                                   short_name,
                                                                   IntegrationTestUtils.TEST_START_DATE,
                                                                   advisory_processor)


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