from pathlib import Path

import right_tree.api.data
from ._spreadsheet_helpers import *
from right_tree.api.models import EcologicalRegion, SoilOrder, SoilVariant, ToleranceLevel, Zone

# Mapping adjustments between the shapefile ecological regions and those in the spreadsheet
ECO_REGION_ADJUSTMENTS = {
    "Whakatane": "Whatkatane",
    "North West Nelson": "North-west Nelson",
    "Aorangi": "Aorrangi",
    "Mackenzie": "MacKenzie",
    "Southland Hills": "Southland Foothills",
    "Sounds Wellington": "Sounds-Wellington"
}

# Relevant columns and information used to retrieve information from the spreadsheet
PLANT_COLS = {
    'SCIENTIFIC NAME': {"str": "name", "expected_type": str, "max_length": 50},
    'MAX HT': {"str": "maxheight", "expected_type": float},
    'SPACING': {"str": "spacing", "expected_type": float},
    'COMMON NAME': {"str": "commonname", "expected_type": str, "null_allowed": True, "max_length": 200},
    'SYNONYM': {"str": "synonym", "expected_type": str, "null_allowed": True, "max_length": 200},
    'ECOLOGICAL REGION': {"str": "region", "expected_type": list, "model_name": "ecological_regions"},
    'SOIL ORDER': {"str": "soilorder", "expected_type": list, "model_name": "soil_order"},
    'Wet': {"str": "wet", "expected_type": list, "model_name": "soil_variants"},
    'Mesic': {"str": "mesic", "expected_type": list, "model_name": "soil_variants"},
    'Dry': {"str": "dry", "expected_type": list, "model_name": "soil_variants"},
    'Water': {"str": "water", "expected_type": int, "model_name": "water_tolerance"},
    'Drought': {"str": "drought", "expected_type": int, "model_name": "drought_tolerance"},
    'Frost': {"str": "frost", "expected_type": int, "model_name": "frost_tolerance"},
    'Salinity': {"str": "salinity", "expected_type": int, "model_name": "salinity_tolerance"},
    'ES': {"str": "purpose", "expected_type": str, "null_allowed": True},
    'STAGE': {"str": "stage", "expected_type": int},
    'GrowthForm': {"str": "growthform", "expected_type": str, "model_name": "growth_form", "null_allowed": True, "max_length": 50}
}

# Spreadsheet constants
SPREADSHEET_FILENAME = 'plant_data.xlsx'
DATA_START_COL = 3
DATA_START_ROW = 6
INFO_HEADER_ROW = 4

# Site col and row contants
SITE_DATA_START_COL = 25
SITE_DATA_STOP_COL = 157
HABITAT_ROW = 2
ZONE_NAME_ROW = 3
ZONE_VARIANT_ROW = 4
ZONE_REFINED_VARIANT_ROW = 5

# Data directory path
DATA_DIR_PATH = Path(right_tree.api.data.__file__).resolve().parent

# Mappings between values in the spreadsheet and primary key values in the database
ECO_REGION_PK_MAPPING = get_pk_mapping(EcologicalRegion)
SOIL_ORDER_PK_MAPPING = get_pk_mapping(SoilOrder)
SOIL_VARIANT_PK_MAPPING = get_pk_mapping(SoilVariant)
TOLERANCE_PK_MAPPING = get_pk_mapping(ToleranceLevel, "level")
ZONE_PK_MAPPING = get_zone_pk_mapping()

# Spreadsheet and corresponding value to column index mappings
SPREADSHEET = get_spreadsheet(DATA_DIR_PATH, SPREADSHEET_FILENAME)
INFO_COL_INDEXES = get_col_mappings(SPREADSHEET, DATA_START_COL, SITE_DATA_START_COL-1, INFO_HEADER_ROW)
ZONE_COL_INDEXES = get_zone_col_mappings(SPREADSHEET, SITE_DATA_START_COL, SITE_DATA_STOP_COL, ZONE_NAME_ROW, ZONE_REFINED_VARIANT_ROW)
