"""Module with class that handle smoke test results."""


class SmokeTests:
    """Class that handle smoke test results."""

    # keys used in the ci_jobs.ini configuration file
    CI_JOBS = {
        "production": "smoketests",
        "stage": None
    }

    INPUT_FILES = {
        "production": {
            "logs": "smoketests_prod.log",
            "results": "smoketests_prod.results"
        },
        "stage": {
            "logs": "smoketests_stage.log",
            "results": "smoketests_stage.results"
        }
    }

    def __init__(self, ci_jobs=None, all_jobs_statuses=None):
        """Construct an instance of the class."""
        self._results = {}
        self._ci_jobs_links = {}
        self._ci_jobs_statuses = {}
        self._logs = {}
        self._ci_jobs = ci_jobs
        self._all_jobs_statuses = all_jobs_statuses

    def read_logs(self):
        """Read logs for all systems (stage, production)."""
        for system, filenames in SmokeTests.INPUT_FILES.items():
            input_file = filenames["logs"]
            with open(input_file) as fin:
                self._logs[system] = fin.read()

    def read_ci_results(self):
        """Read results generated on CI side."""
        if self._ci_jobs is not None:
            for system, job_name in SmokeTests.CI_JOBS.items():
                if job_name is not None:
                    # the job has been setup -> let's check its status on CI
                    url = self._ci_jobs.get_job_url(system, job_name)
                    name = self._ci_jobs.get_job_name(system, job_name)
                    job_status = self._all_jobs_statuses.get(name)
                    self._ci_jobs_links[system] = url
                    self._ci_jobs_statuses[system] = job_status

    def read_results(self):
        """Read results generated by all smoke tests."""
        for system, filenames in SmokeTests.INPUT_FILES.items():
            input_file = filenames["results"]
            with open(input_file) as fin:
                self._results[system] = fin.read().strip() == "0"

    @property
    def results(self):
        """Getter for the 'results' attribute."""
        if not self._results:
            self.read_results()
        return self._results

    @property
    def logs(self):
        """Getter for the 'logs' attribute."""
        if not self._logs:
            self.read_logs()
        return self._logs

    @property
    def ci_jobs_links(self):
        """Getter for the 'jobs_links' attribute."""
        if not self._ci_jobs_links:
            self.read_ci_results()
        return self._ci_jobs_links

    @property
    def ci_jobs_statuses(self):
        """Getter for the 'jobs_statuses' attribute."""
        if not self._ci_jobs_statuses:
            self.read_ci_results()
        return self._ci_jobs_statuses


if __name__ == "__main__":
    # execute only if run as a script
    smoke_tests = SmokeTests()
    print("Results:")
    print(smoke_tests.results)
    print("Logs:")
    print(smoke_tests.logs)
