import copy
import os
import unittest

from ProcessorSurveys import ProcessorSurveys
from integration.partial.integration_test_utils import IntegrationTestUtils
from integration.test_suites.survey_test_suite import BaseSurveyTestSuite


class TestSurvey(BaseSurveyTestSuite.SurveyTestSuite):

    @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 = "SURVEYDATA_" + IntegrationTestUtils.TEST_START_DATE


    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):
            TestSurvey.write_temp_run_config_file()
            TestSurvey.unpack_dependencies()
            TestSurvey.run_survey_pipeline()
        else:
            print(f"output in {IntegrationTestUtils.TEST_OUT_PATH} already written, skipping rerun")


    @staticmethod
    def write_temp_run_config_file():
        nowstring: str = IntegrationTestUtils.get_now_string()
        prefix: str = "temp_survey_" + nowstring
        # prefix: str = "temp_survey"

        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)
        IntegrationTestUtils.TEST_OUT_PATH = IntegrationTestUtils.TEST_WORKSPACE_PATH + prefix + os.sep
        run_dict['WorkspacePathout'] = IntegrationTestUtils.TEST_OUT_PATH
        run_dict['WorkspacePath'] = IntegrationTestUtils.TEST_OUT_PATH
        run_dict['Environment']['WORK_PATH'] = IntegrationTestUtils.TEST_OUT_PATH
        run_dict['Environment']['INPUT_PATH'] = IntegrationTestUtils.TEST_OUT_PATH
        run_dict['Environment']['OUTPUT_PATH'] = IntegrationTestUtils.TEST_OUT_PATH
        run_dict['ServerName'] = ''  # nothing, as local machine

        # 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.TEMP_CONFIG_FILE_NAME = IntegrationTestUtils.TEST_OUT_PATH + "temp_config.json"
        IntegrationTestUtils.write_json_file(run_dict, IntegrationTestUtils.TEMP_CONFIG_FILE_NAME)

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

    @staticmethod
    def run_survey_pipeline():
        component = 'Survey'
        #  need EMAIL_CRED in the environment before we create a Processor
        os.environ["EMAIL_CRED"] = IntegrationTestUtils.EMAIL_CRED_PATH
        survey_processor = ProcessorSurveys()
        IntegrationTestUtils.run_partial_integration_test_pipeline(component,
                                                                   IntegrationTestUtils.TEST_START_DATE,
                                                                   survey_processor)


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