from contextlib import contextmanager

# Valid metadata
L1 = {'groups': 3,
      'spacecraft': 'LANDSAT_1',
      'id': "LANDSAT_1_MSS",
      'product_len': 21,
      'data': """GROUP = L1_METADATA_FILE
GROUP = METADATA_FILE_INFO
ORIGIN = "Image courtesy of the U.S. Geological Survey"
REQUEST_ID = "0001406050000_00004"
LANDSAT_SCENE_ID = "LM11740451972246AAA01"
FILE_DATE = 2014-07-08T20:13:41Z
STATION_ID = "XXX"
PROCESSING_SOFTWARE_VERSION = "LPGS_12.5.0"
DATA_CATEGORY = "NOMINAL"
END_GROUP = METADATA_FILE_INFO
GROUP = PRODUCT_METADATA
DATA_TYPE = "L1T"
DATA_TYPE_L0RP = "MSSX_L0RP"
ELEVATION_SOURCE = "GLS2000"
OUTPUT_FORMAT = "GEOTIFF"
EPHEMERIS_TYPE = "PREDICTIVE"
SPACECRAFT_ID = "LANDSAT_1"
SENSOR_ID = "MSS"
WRS_PATH = 174
WRS_ROW = 045
DATE_ACQUIRED = 1972-09-02
SCENE_CENTER_TIME = 06:30:37.1900000Z
FILE_NAME_BAND_4 = "LM11740451972246AAA01_B4.TIF"
FILE_NAME_BAND_5 = "LM11740451972246AAA01_B5.TIF"
FILE_NAME_BAND_6 = "LM11740451972246AAA01_B6.TIF"
FILE_NAME_BAND_7 = "LM11740451972246AAA01_B7.TIF"
GROUND_CONTROL_POINT_FILE_NAME = "LM11740451972246AAA01_GCP.txt"
REPORT_VERIFY_FILE_NAME = "LM11740451972246AAA01_VER.txt"
BROWSE_VERIFY_FILE_NAME = "LM11740451972246AAA01_VER.jpg"
METADATA_FILE_NAME = "LM11740451972246AAA01_MTL.txt"
CPF_NAME = "LM1CPF19720723_19780107.05"
END_GROUP = PRODUCT_METADATA
END_GROUP = L1_METADATA_FILE
END""".split('\n')}
L4 = {'groups': 3,
      'spacecraft': 'LANDSAT_4',
      'product_len': 25,
      'id': 'LANDSAT_4_TM',
      'data': """GROUP = L1_METADATA_FILE
GROUP = METADATA_FILE_INFO
ORIGIN = "Image courtesy of the U.S. Geological Survey"
REQUEST_ID = "0001406050000_00006"
LANDSAT_SCENE_ID = "LT40440321983224AAA02"
FILE_DATE = 2014-07-08T20:09:07Z
STATION_ID = "AAA"
PROCESSING_SOFTWARE_VERSION = "LPGS_12.5.0"
DATA_CATEGORY = "NOMINAL"
END_GROUP = METADATA_FILE_INFO
GROUP = PRODUCT_METADATA
DATA_TYPE = "L1T"
DATA_TYPE_L0RP = "TMR_L0RP"
ELEVATION_SOURCE = "GLS2000"
OUTPUT_FORMAT = "GEOTIFF"
EPHEMERIS_TYPE = "PREDICTIVE"
SPACECRAFT_ID = "LANDSAT_4"
SENSOR_ID = "TM"
SENSOR_MODE = "SAM"
WRS_PATH = 044
WRS_ROW = 032
DATE_ACQUIRED = 1983-08-12
SCENE_CENTER_TIME = 18:14:34.5780940Z
FILE_NAME_BAND_1 = "LT40440321983224AAA02_B1.TIF"
FILE_NAME_BAND_2 = "LT40440321983224AAA02_B2.TIF"
FILE_NAME_BAND_3 = "LT40440321983224AAA02_B3.TIF"
FILE_NAME_BAND_4 = "LT40440321983224AAA02_B4.TIF"
FILE_NAME_BAND_5 = "LT40440321983224AAA02_B5.TIF"
FILE_NAME_BAND_6 = "LT40440321983224AAA02_B6.TIF"
FILE_NAME_BAND_7 = "LT40440321983224AAA02_B7.TIF"
GROUND_CONTROL_POINT_FILE_NAME = "LT40440321983224AAA02_GCP.txt"
REPORT_VERIFY_FILE_NAME = "LT40440321983224AAA02_VER.txt"
BROWSE_VERIFY_FILE_NAME = "LT40440321983224AAA02_VER.jpg"
METADATA_FILE_NAME = "LT40440321983224AAA02_MTL.txt"
CPF_NAME = "L4CPF19830701_19830930.04"
END_GROUP = PRODUCT_METADATA
END_GROUP = L1_METADATA_FILE
END""".split('\n')}
L7 = {'groups': 3,
      'id': 'LANDSAT_7_ETM',
      'product_len': 27,
      'spacecraft': 'LANDSAT_7',
      'data': """GROUP = L1_METADATA_FILE
GROUP = METADATA_FILE_INFO
ORIGIN = "Image courtesy of the U.S. Geological Survey"
REQUEST_ID = "0501612276622_09852"
LANDSAT_SCENE_ID = "LE71840242008170ASN00"
LANDSAT_PRODUCT_ID = "LE07_L1TP_184024_20080618_20161228_01_T1"
COLLECTION_NUMBER = 01
FILE_DATE = 2016-12-28T19:19:15Z
STATION_ID = "ASN"
PROCESSING_SOFTWARE_VERSION = "LPGS_12.8.2"
DATA_CATEGORY = "NOMINAL"
END_GROUP = METADATA_FILE_INFO
GROUP = PRODUCT_METADATA
DATA_TYPE = "L1TP"
COLLECTION_CATEGORY = "T1"
ELEVATION_SOURCE = "GLS2000"
OUTPUT_FORMAT = "GEOTIFF"
EPHEMERIS_TYPE = "DEFINITIVE"
SPACECRAFT_ID = "LANDSAT_7"
SENSOR_ID = "ETM"
SENSOR_MODE = "BUMPER"
WRS_PATH = 184
WRS_ROW = 024
DATE_ACQUIRED = 2008-06-18
SCENE_CENTER_TIME = "08:57:02.4062512Z"
FILE_NAME_BAND_1 = "LE07_L1TP_184024_20080618_20161228_01_T1_B1.TIF"
FILE_NAME_BAND_2 = "LE07_L1TP_184024_20080618_20161228_01_T1_B2.TIF"
FILE_NAME_BAND_3 = "LE07_L1TP_184024_20080618_20161228_01_T1_B3.TIF"
FILE_NAME_BAND_4 = "LE07_L1TP_184024_20080618_20161228_01_T1_B4.TIF"
FILE_NAME_BAND_5 = "LE07_L1TP_184024_20080618_20161228_01_T1_B5.TIF"
FILE_NAME_BAND_6_VCID_1 = "LE07_L1TP_184024_20080618_20161228_01_T1_B6_VCID_1.TIF"
FILE_NAME_BAND_6_VCID_2 = "LE07_L1TP_184024_20080618_20161228_01_T1_B6_VCID_2.TIF"
FILE_NAME_BAND_7 = "LE07_L1TP_184024_20080618_20161228_01_T1_B7.TIF"
FILE_NAME_BAND_8 = "LE07_L1TP_184024_20080618_20161228_01_T1_B8.TIF"
FILE_NAME_BAND_QUALITY = "LE07_L1TP_184024_20080618_20161228_01_T1_BQA.TIF"
GROUND_CONTROL_POINT_FILE_NAME = "LE07_L1TP_184024_20080618_20161228_01_T1_GCP.txt"
ANGLE_COEFFICIENT_FILE_NAME = "LE07_L1TP_184024_20080618_20161228_01_T1_ANG.txt"
METADATA_FILE_NAME = "LE07_L1TP_184024_20080618_20161228_01_T1_MTL.txt"
CPF_NAME = "LE07CPF_20080401_20080630_01.02"
END_GROUP = PRODUCT_METADATA
END_GROUP = L1_METADATA_FILE
END""".split('\n')}
L8 = {'groups': 3,
      'product_len': 31,
      'spacecraft': 'LANDSAT_8',
      'id': "LANDSAT_8_OLI_TIRS",
      'data': """GROUP = L1_METADATA_FILE
GROUP = METADATA_FILE_INFO
ORIGIN = "Image courtesy of the U.S. Geological Survey"
REQUEST_ID = "0501704237886_00012"
LANDSAT_SCENE_ID = "LC81840242014114LGN01"
LANDSAT_PRODUCT_ID = "LC08_L1TP_184024_20140424_20170423_01_T1"
COLLECTION_NUMBER = 01
FILE_DATE = 2017-04-23T09:54:29Z
STATION_ID = "LGN"
PROCESSING_SOFTWARE_VERSION = "LPGS_2.7.0"
END_GROUP = METADATA_FILE_INFO
GROUP = PRODUCT_METADATA
DATA_TYPE = "L1TP"
COLLECTION_CATEGORY = "T1"
ELEVATION_SOURCE = "GLS2000"
OUTPUT_FORMAT = "GEOTIFF"
SPACECRAFT_ID = "LANDSAT_8"
SENSOR_ID = "OLI_TIRS"
WRS_PATH = 184
WRS_ROW = 24
NADIR_OFFNADIR = "NADIR"
TARGET_WRS_PATH = 184
TARGET_WRS_ROW = 24
DATE_ACQUIRED = 2014-04-24
FILE_NAME_BAND_1 = "LC08_L1TP_184024_20140424_20170423_01_T1_B1.TIF"
FILE_NAME_BAND_2 = "LC08_L1TP_184024_20140424_20170423_01_T1_B2.TIF"
FILE_NAME_BAND_3 = "LC08_L1TP_184024_20140424_20170423_01_T1_B3.TIF"
FILE_NAME_BAND_4 = "LC08_L1TP_184024_20140424_20170423_01_T1_B4.TIF"
FILE_NAME_BAND_5 = "LC08_L1TP_184024_20140424_20170423_01_T1_B5.TIF"
FILE_NAME_BAND_6 = "LC08_L1TP_184024_20140424_20170423_01_T1_B6.TIF"
FILE_NAME_BAND_7 = "LC08_L1TP_184024_20140424_20170423_01_T1_B7.TIF"
FILE_NAME_BAND_8 = "LC08_L1TP_184024_20140424_20170423_01_T1_B8.TIF"
FILE_NAME_BAND_9 = "LC08_L1TP_184024_20140424_20170423_01_T1_B9.TIF"
FILE_NAME_BAND_10 = "LC08_L1TP_184024_20140424_20170423_01_T1_B10.TIF"
FILE_NAME_BAND_11 = "LC08_L1TP_184024_20140424_20170423_01_T1_B11.TIF"
FILE_NAME_BAND_QUALITY = "LC08_L1TP_184024_20140424_20170423_01_T1_BQA.TIF"
ANGLE_COEFFICIENT_FILE_NAME = "LC08_L1TP_184024_20140424_20170423_01_T1_ANG.txt"
METADATA_FILE_NAME = "LC08_L1TP_184024_20140424_20170423_01_T1_MTL.txt"
CPF_NAME = "LC08CPF_20140401_20140630_01.01"
BPF_NAME_OLI = "LO8BPF20140424084554_20140424092403.01"
BPF_NAME_TIRS = "LT8BPF20140424084200_20140424092456.01"
RLUT_FILE_NAME = "LC08RLUT_20130211_20150302_01_11.h5"
END_GROUP = PRODUCT_METADATA
END_GROUP = L1_METADATA_FILE
END""".split('\n')}

# Invalid metadata


class OpenStub:
    def __init__(self, file_mock_obj):
        self.mock = file_mock_obj

    @contextmanager
    def open(self, name, mode):
        yield self.mock


class FileMock:
    def __init__(self, content):
        self.content = content

    def readlines(self):
        return self.content
