import json
import os

from eocanvas.exceptions import MalformedSnapError
from eocanvas.utils import Singleton


class Snap(metaclass=Singleton):
    def __init__(self, filename=None):
        if filename is None:
            filename = os.path.join(os.path.dirname(os.path.abspath(__file__)), "operators.json")
        self.filename = filename
        self._operators = None

    @property
    def operators(self):
        if self._operators is None:
            try:
                with open(self.filename) as f:
                    self._operators = json.load(f)
            except FileNotFoundError:
                raise FileNotFoundError(f"SNAP operators file {self.filename} not found.")
            except json.JSONDecodeError as exc:
                raise MalformedSnapError(
                    f"SNAP operators file{self.filename} is not valid JSON: {exc}"
                )

        return self._operators

    @property
    def read_formats(self):
        return [
            "ACE",
            "ACE2",
            "ALOS PALSAR CEOS",
            "ALOS-2 CEOS",
            "ALOS-2 GeoTIFF",
            "AlosAV2Dimap",
            "AlosPRIDimap",
            "Aquarius",
            "ARC_INFO_BIN_GRID",
            "ATS_L1_S3",
            "ATSR",
            "AVNIR-2",
            "Basic CEOS",
            "BEAM-DIMAP",
            "Binned_data_product",
            "Capella",
            "Complex Binary",
            "CosmoSkymed",
            "CSV",
            "DEIMOSDimap",
            "DSCOVER-L2",
            "ENVI",
            "ENVISAT",
            "ERS CEOS",
            "FAST L5",
            "Gamma",
            "Gaofen3",
            "GDAL-BT-READER",
            "GDAL-ERS-READER",
            "GDAL-GS7BG-READER",
            "GDAL-GSBG-READER",
            "GDAL-GTiff-READER",
            "GDAL-GTX-READER",
            "GDAL-HFA-READER",
            "GDAL-ILWIS-READER",
            "GDAL-KRO-READER",
            "GDAL-MFF-READER",
            "GDAL-NITF-READER",
            "GDAL-PCIDSK-READER",
            "GDAL-RMF-READER",
            "GDAL-RST-READER",
            "GDAL-SAGA-READER",
            "GDAL-SGI-READER",
            "Generic Binary",
            "GeoTIFF-BigTIFF",
            "GeoTIFF",
            "GETASSE30",
            "GOCI-L1B",
            "GTOPO30",
            "HDF-EOS",
            "HGT",
            "HICO-L1B",
            "HICO",
            "IceyeProduct",
            "IkonosGeoTIFF",
            "IMAGE",
            "JERS CEOS",
            "JPEG2000",
            "Kompsat2GeoTIFF",
            "Kompsat5",
            "L3_ENV_MER",
            "Landsat8GeoTIFF15m",
            "Landsat8GeoTIFF30m",
            "LandsatC2L2GeoTIFF",
            "LandsatGeoTIFF",
            "LandsatL2GeoTIFF",
            "Matlab",
            "MER_L1_S3",
            "MER_L2_S3",
            "METOP_AVHRR_3_L1B",
            "MODIS-L1B",
            "MODIS",
            "MUSCATE",
            "NetCDF-BEAM",
            "NetCDF-CF",
            "NetCDF",
            "NOAA_AVHRR_3_L1B",
            "NOAA_POD_AVHRR_HRPT",
            "OCM2-L1B",
            "PACE-L1B",
            "PAZ",
            "PGX",
            "PleiadesDimap",
            "PolSARPro",
            "PRISM",
            "RADARSAT-1",
            "RADARSAT-2",
            "RapidEyeGeoTIFF",
            "RapidEyeNITF",
            "RAT",
            "RCM",
            "RISAT-1",
            "SAOCOM",
            "SeaDAS-ANC",
            "SeaDAS-Browse",
            "SeaDAS-L1",
            "SeaDAS-L2",
            "SeaDAS-L3",
            "SeaSat",
            "Sen3_SLSTRL1B_1km",
            "Sen3_SLSTRL1B_500m",
            "Sen3",
            "SENTINEL-1",
            "SENTINEL-2-MSI-10M",
            "SENTINEL-2-MSI-20M",
            "SENTINEL-2-MSI-60M",
            "SENTINEL-2-MSI-L1B-MultiRes",
            "SENTINEL-2-MSI-MultiRes-UTM01N",
            "SENTINEL-2-MSI-MultiRes-UTM01S",
            "SENTINEL-2-MSI-MultiRes-UTM02N",
            "SENTINEL-2-MSI-MultiRes-UTM02S",
            "SENTINEL-2-MSI-MultiRes-UTM03N",
            "SENTINEL-2-MSI-MultiRes-UTM03S",
            "SENTINEL-2-MSI-MultiRes-UTM04N",
            "SENTINEL-2-MSI-MultiRes-UTM04S",
            "SENTINEL-2-MSI-MultiRes-UTM05N",
            "SENTINEL-2-MSI-MultiRes-UTM05S",
            "SENTINEL-2-MSI-MultiRes-UTM06N",
            "SENTINEL-2-MSI-MultiRes-UTM06S",
            "SENTINEL-2-MSI-MultiRes-UTM07N",
            "SENTINEL-2-MSI-MultiRes-UTM07S",
            "SENTINEL-2-MSI-MultiRes-UTM08N",
            "SENTINEL-2-MSI-MultiRes-UTM08S",
            "SENTINEL-2-MSI-MultiRes-UTM09N",
            "SENTINEL-2-MSI-MultiRes-UTM09S",
            "SENTINEL-2-MSI-MultiRes-UTM10N",
            "SENTINEL-2-MSI-MultiRes-UTM10S",
            "SENTINEL-2-MSI-MultiRes-UTM11N",
            "SENTINEL-2-MSI-MultiRes-UTM11S",
            "SENTINEL-2-MSI-MultiRes-UTM12N",
            "SENTINEL-2-MSI-MultiRes-UTM12S",
            "SENTINEL-2-MSI-MultiRes-UTM13N",
            "SENTINEL-2-MSI-MultiRes-UTM13S",
            "SENTINEL-2-MSI-MultiRes-UTM14N",
            "SENTINEL-2-MSI-MultiRes-UTM14S",
            "SENTINEL-2-MSI-MultiRes-UTM15N",
            "SENTINEL-2-MSI-MultiRes-UTM15S",
            "SENTINEL-2-MSI-MultiRes-UTM16N",
            "SENTINEL-2-MSI-MultiRes-UTM16S",
            "SENTINEL-2-MSI-MultiRes-UTM17N",
            "SENTINEL-2-MSI-MultiRes-UTM17S",
            "SENTINEL-2-MSI-MultiRes-UTM18N",
            "SENTINEL-2-MSI-MultiRes-UTM18S",
            "SENTINEL-2-MSI-MultiRes-UTM19N",
            "SENTINEL-2-MSI-MultiRes-UTM19S",
            "SENTINEL-2-MSI-MultiRes-UTM20N",
            "SENTINEL-2-MSI-MultiRes-UTM20S",
            "SENTINEL-2-MSI-MultiRes-UTM21N",
            "SENTINEL-2-MSI-MultiRes-UTM21S",
            "SENTINEL-2-MSI-MultiRes-UTM22N",
            "SENTINEL-2-MSI-MultiRes-UTM22S",
            "SENTINEL-2-MSI-MultiRes-UTM23N",
            "SENTINEL-2-MSI-MultiRes-UTM23S",
            "SENTINEL-2-MSI-MultiRes-UTM24N",
            "SENTINEL-2-MSI-MultiRes-UTM24S",
            "SENTINEL-2-MSI-MultiRes-UTM25N",
            "SENTINEL-2-MSI-MultiRes-UTM25S",
            "SENTINEL-2-MSI-MultiRes-UTM26N",
            "SENTINEL-2-MSI-MultiRes-UTM26S",
            "SENTINEL-2-MSI-MultiRes-UTM27N",
            "SENTINEL-2-MSI-MultiRes-UTM27S",
            "SENTINEL-2-MSI-MultiRes-UTM28N",
            "SENTINEL-2-MSI-MultiRes-UTM28S",
            "SENTINEL-2-MSI-MultiRes-UTM29N",
            "SENTINEL-2-MSI-MultiRes-UTM29S",
            "SENTINEL-2-MSI-MultiRes-UTM30N",
            "SENTINEL-2-MSI-MultiRes-UTM30S",
            "SENTINEL-2-MSI-MultiRes-UTM31N",
            "SENTINEL-2-MSI-MultiRes-UTM31S",
            "SENTINEL-2-MSI-MultiRes-UTM32N",
            "SENTINEL-2-MSI-MultiRes-UTM32S",
            "SENTINEL-2-MSI-MultiRes-UTM33N",
            "SENTINEL-2-MSI-MultiRes-UTM33S",
            "SENTINEL-2-MSI-MultiRes-UTM34N",
            "SENTINEL-2-MSI-MultiRes-UTM34S",
            "SENTINEL-2-MSI-MultiRes-UTM35N",
            "SENTINEL-2-MSI-MultiRes-UTM35S",
            "SENTINEL-2-MSI-MultiRes-UTM36N",
            "SENTINEL-2-MSI-MultiRes-UTM36S",
            "SENTINEL-2-MSI-MultiRes-UTM37N",
            "SENTINEL-2-MSI-MultiRes-UTM37S",
            "SENTINEL-2-MSI-MultiRes-UTM38N",
            "SENTINEL-2-MSI-MultiRes-UTM38S",
            "SENTINEL-2-MSI-MultiRes-UTM39N",
            "SENTINEL-2-MSI-MultiRes-UTM39S",
            "SENTINEL-2-MSI-MultiRes-UTM40N",
            "SENTINEL-2-MSI-MultiRes-UTM40S",
            "SENTINEL-2-MSI-MultiRes-UTM41N",
            "SENTINEL-2-MSI-MultiRes-UTM41S",
            "SENTINEL-2-MSI-MultiRes-UTM42N",
            "SENTINEL-2-MSI-MultiRes-UTM42S",
            "SENTINEL-2-MSI-MultiRes-UTM43N",
            "SENTINEL-2-MSI-MultiRes-UTM43S",
            "SENTINEL-2-MSI-MultiRes-UTM44N",
            "SENTINEL-2-MSI-MultiRes-UTM44S",
            "SENTINEL-2-MSI-MultiRes-UTM45N",
            "SENTINEL-2-MSI-MultiRes-UTM45S",
            "SENTINEL-2-MSI-MultiRes-UTM46N",
            "SENTINEL-2-MSI-MultiRes-UTM46S",
            "SENTINEL-2-MSI-MultiRes-UTM47N",
            "SENTINEL-2-MSI-MultiRes-UTM47S",
            "SENTINEL-2-MSI-MultiRes-UTM48N",
            "SENTINEL-2-MSI-MultiRes-UTM48S",
            "SENTINEL-2-MSI-MultiRes-UTM49N",
            "SENTINEL-2-MSI-MultiRes-UTM49S",
            "SENTINEL-2-MSI-MultiRes-UTM50N",
            "SENTINEL-2-MSI-MultiRes-UTM50S",
            "SENTINEL-2-MSI-MultiRes-UTM51N",
            "SENTINEL-2-MSI-MultiRes-UTM51S",
            "SENTINEL-2-MSI-MultiRes-UTM52N",
            "SENTINEL-2-MSI-MultiRes-UTM52S",
            "SENTINEL-2-MSI-MultiRes-UTM53N",
            "SENTINEL-2-MSI-MultiRes-UTM53S",
            "SENTINEL-2-MSI-MultiRes-UTM54N",
            "SENTINEL-2-MSI-MultiRes-UTM54S",
            "SENTINEL-2-MSI-MultiRes-UTM55N",
            "SENTINEL-2-MSI-MultiRes-UTM55S",
            "SENTINEL-2-MSI-MultiRes-UTM56N",
            "SENTINEL-2-MSI-MultiRes-UTM56S",
            "SENTINEL-2-MSI-MultiRes-UTM57N",
            "SENTINEL-2-MSI-MultiRes-UTM57S",
            "SENTINEL-2-MSI-MultiRes-UTM58N",
            "SENTINEL-2-MSI-MultiRes-UTM58S",
            "SENTINEL-2-MSI-MultiRes-UTM59N",
            "SENTINEL-2-MSI-MultiRes-UTM59S",
            "SENTINEL-2-MSI-MultiRes-UTM60N",
            "SENTINEL-2-MSI-MultiRes-UTM60S",
            "SNAPHU",
            "Spacety",
            "SPOT-VGT",
            "SPOT4Take5",
            "SPOT5Take5",
            "SPOT6Dimap",
            "SPOTDimap",
            "SPOTView",
            "TerraSarX",
            "VIIRS-L1B",
            "VIIRS",
            "WorldView2GeoTIFF",
            "WorldViewGeoTIFF",
        ]

    @property
    def write_formats(self):
        return [
            "BEAM-DIMAP",
            "CSV",
            "ENVI",
            "Gamma for PyRate",
            "Gamma",
            "GDAL-BMP-WRITER",
            "GDAL-BT-WRITER",
            "GDAL-GS7BG-WRITER",
            "GDAL-GSBG-WRITER",
            "GDAL-GTiff-WRITER",
            "GDAL-GTX-WRITER",
            "GDAL-HFA-WRITER",
            "GDAL-ILWIS-WRITER",
            "GDAL-KRO-WRITER",
            "GDAL-MFF-WRITER",
            "GDAL-NITF-WRITER",
            "GDAL-PCIDSK-WRITER",
            "GDAL-PNM-WRITER",
            "GDAL-RMF-WRITER",
            "GDAL-RST-WRITER",
            "GDAL-SAGA-WRITER",
            "GDAL-SGI-WRITER",
            "Generic Binary BSQ",
            "GeoTIFF-BigTIFF",
            "GeoTIFF",
            "GeoTIFF+XML",
            "HDF5",
            "JP2",
            "JPEG2000",
            "NetCDF4-BEAM",
            "NetCDF4-CF",
            "PolSARPro",
            "Snaphu",
        ]
