""" Let's not actually hit the archive service """

import logging
import sys
from pathlib import Path

from datafetcher.locations_report_refactor import LocationsReportRefactor, UnknownLocatorException

# pylint: disable=C0301, R0903

_LOG = logging.getLogger(__name__)

PROFILE = "docker"

REPORT_FILES = {
    "vla_eb": "17A-109_fg_18468.json",
    "calibration": "CALIBRATION.json",
    "image": "IMG.json",
    "vlba": "VLBA_EB.json",
    "gbt": "AGBT17B_044_02.json",
    # "alma": "A001_X1296_Xa93_RAW.json",
    "empty": "EMPTY.json",
    "not_json": "NOT_JSON.json",
    "vla_bad_server": "VLA_BAD_SERVER.json",
    "vla_eb_busted": "VLA_SMALL_EB_BUSTED.json",
}


class FakeArchiveService:
    """ Stand-in for archive service """

    def __init__(self, product_locator: str):
        """
        Return locations report for given locator

        :param product_locator:
        """
        self.product_locator = product_locator
        self.data_dir = get_test_data_dir()

    def get_locations_report(self) -> LocationsReportRefactor:
        """
        Depending on the product locator, return locations report for a VLA EB, an image, a VLBA product, etc.

        :return: the location report
        """

        if "vlba" in self.product_locator:
            return self._vlba_locations_report()
        elif "calibration" in self.product_locator:
            return self._calibration_locations_report()
        elif "image" in self.product_locator:
            return self._image_locations_report()
        elif "alma" in self.product_locator:
            return self._alma_locations_report()
        elif "gbt" in self.product_locator:
            return self._gbt_locations_report()
        elif "evla/execblock" in self.product_locator:
            return self._vla_eb_locations_report()
        raise UnknownLocatorException

    def _vla_eb_locations_report(self) -> LocationsReportRefactor:
        """
        Read in a VLA EB locations report from a .json file.

        :return:
        """
        file = self.data_dir / REPORT_FILES["vla_eb"]
        return self._get_location_report_from_file(file)

    def _calibration_locations_report(self) -> LocationsReportRefactor:
        """
        Read in a VLA calibrations locations report from a .json file.

        :return:
        """
        file = self.data_dir / REPORT_FILES["calibration"]
        return self._get_location_report_from_file(file)

    def _image_locations_report(self) -> LocationsReportRefactor:
        """
        Read in a VLASS image locations report from a .json file.

        :return:
        """
        file = self.data_dir / REPORT_FILES["image"]
        return self._get_location_report_from_file(file)

    def _vlba_locations_report(self) -> LocationsReportRefactor:
        """
        Read in a VLBA locations report from a .json file.

        :return:
        """
        file = self.data_dir / REPORT_FILES["vlba"]
        return self._get_location_report_from_file(file)

    def _gbt_locations_report(self) -> LocationsReportRefactor:
        """
        Read in a GBT locations report from a .json file.

        :return:
        """
        file = self.data_dir / REPORT_FILES["gbt"]
        return self._get_location_report_from_file(file)

    def _alma_locations_report(self) -> LocationsReportRefactor:
        """
        Read in an ALMA EB locations report from a .json file.

        :return:
        """
        file = self.data_dir / REPORT_FILES["alma"]
        return self._get_location_report_from_file(file)

    @staticmethod
    def _get_location_report_from_file(location_file: Path) -> LocationsReportRefactor:
        """
        Read a .json file into a LocationsReport.

        :param location_file: Path to file
        :return: the locations report
        """
        args = ["--location-file", str(location_file), "--profile", PROFILE]
        return LocationsReportRefactor(args)


def get_test_data_dir() -> Path:
    """
    Find the .json locations report files we use for testing.

    :return:
    """

    top_level_subdirs = sys.path
    shared_ws_src = None
    for pathname in top_level_subdirs:
        # test data will be a few levels under shared_wksp
        if "shared/workspaces" in pathname:
            shared_ws_src = pathname
            break
    shared_wksp = Path(shared_ws_src).parent

    for item in shared_wksp.rglob("location_files"):
        assert item.is_dir()
        return item

    return None