from copy import deepcopy

DEFAULT_NODATA = {
    "type": "transparent",
    "value": None,
    "description": "No display data is rendered for missing cells.",
}

PRODUCT_DEFINITIONS = {
    "wind": {
        "product": "wind",
        "display_type": "raster",
        "path_template": "/weather-display/raster/wind/{time}/{z}/{x}/{y}.png",
        "unit": "m/s",
        "palette_id": "wind-speed-navsea-v9",
        "recommended_range": {"min": 0, "max": 17},
        "supported_zoom": {"min": 2, "max": 8},
        "opacity": 0.7,
        "title": "Wind Speed",
        "subtitle": "10 m above sea surface",
        "legend": {
            "scale_type": "continuous",
            "sections": [
                {"label": "Calm", "min": 0, "max": 2},
                {"label": "Light", "min": 2, "max": 4},
                {"label": "Breeze", "min": 4, "max": 6},
                {"label": "Fresh", "min": 6, "max": 9},
                {"label": "Strong", "min": 9, "max": 12},
                {"label": "Near Gale", "min": 12, "max": 15},
                {"label": "Gale+", "min": 15, "max": 17},
            ],
            "color_stops": [
                {"value": 0.0, "color": "#6468a8"},
                {"value": 1.5, "color": "#4f78b8"},
                {"value": 2.5, "color": "#4f9bb8"},
                {"value": 4.0, "color": "#4eb89e"},
                {"value": 5.0, "color": "#58c278"},
                {"value": 6.0, "color": "#7bc857"},
                {"value": 7.0, "color": "#abc94d"},
                {"value": 8.0, "color": "#e0c34a"},
                {"value": 10.0, "color": "#e28a3d"},
                {"value": 12.0, "color": "#cf4b3f"},
                {"value": 14.0, "color": "#a23f6f"},
                {"value": 15.0, "color": "#8b356f"},
                {"value": 17.0, "color": "#6b2368"},
            ],
        },
        "download": {
            "base_url": "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl",
            "dir_template": "/gfs.{date}/{cycle}/atmos",
            "file_template": "gfs.t{cycle}z.pgrb2.0p25.f{forecast_hour}",
            "variables": ["UGRD", "VGRD"],
            "levels": ["lev_10_m_above_ground"],
            "suffix": ".grib2",
            "region": None,
        },
    },
    "wave": {
        "product": "wave",
        "display_type": "raster",
        "path_template": "/weather-display/raster/wave/{time}/{z}/{x}/{y}.png",
        "unit": "m",
        "palette_id": "wave-height-navsea-v4",
        "recommended_range": {"min": 0, "max": 8},
        "supported_zoom": {"min": 2, "max": 8},
        "opacity": 0.68,
        "title": "Wave Height",
        "subtitle": "Significant wave height",
        "legend": {
            "scale_type": "continuous",
            "sections": [
                {"label": "Low", "min": 0, "max": 1},
                {"label": "Moderate", "min": 1, "max": 3},
                {"label": "High", "min": 3, "max": 5},
                {"label": "Very High", "min": 5, "max": 8},
            ],
            "color_stops": [
                {"value": 0.0, "color": "#4ea6a6"},
                {"value": 0.5, "color": "#3f86c8"},
                {"value": 1.0, "color": "#4d62d4"},
                {"value": 1.5, "color": "#6f4ad1"},
                {"value": 2.0, "color": "#8d46c1"},
                {"value": 3.0, "color": "#a648b1"},
                {"value": 6.0, "color": "#be7ab4"},
                {"value": 8.0, "color": "#d0a2be"},
            ],
        },
        "download": {
            "base_url": "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfswave.pl",
            "dir_template": "/gfs.{date}/{cycle}/wave/gridded",
            "file_template": "gfswave.t{cycle}z.global.0p25.f{forecast_hour}.grib2",
            "variables": ["HTSGW"],
            "levels": ["lev_surface"],
            "suffix": "_wave.grib2",
            "region": {
                "leftlon": 120,
                "rightlon": 150,
                "toplat": 50,
                "bottomlat": 20,
            },
        },
    },
    "rain": {
        "product": "rain",
        "display_type": "raster",
        "path_template": "/weather-display/raster/rain/{time}/{z}/{x}/{y}.png",
        "unit": "mm/h",
        "palette_id": "rain-rate-navsea-v3",
        "recommended_range": {"min": 0, "max": 20},
        "supported_zoom": {"min": 2, "max": 8},
        "opacity": 0.72,
        "title": "Rain",
        "subtitle": "Display precipitation layer",
        "legend": {
            "scale_type": "continuous",
            "sections": [
                {"label": "<3 mm/h", "min": 0, "max": 3},
                {"label": "3-5 mm/h", "min": 3, "max": 5},
                {"label": "5-8 mm/h", "min": 5, "max": 8},
                {"label": "8-15 mm/h", "min": 8, "max": 15},
                {"label": ">15 mm/h", "min": 15, "max": 20},
            ],
            "color_stops": [
                {"value": 0.0, "color": "#6d6d73"},
                {"value": 1.5, "color": "#4d86c7"},
                {"value": 2.0, "color": "#427cc8"},
                {"value": 3.0, "color": "#5566cf"},
                {"value": 7.0, "color": "#69b84f"},
                {"value": 10.0, "color": "#b7c545"},
                {"value": 20.0, "color": "#c24a3d"},
                {"value": 30.0, "color": "#8f2f45"},
            ],
        },
        "download": {
            "base_url": "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl",
            "dir_template": "/gfs.{date}/{cycle}/atmos",
            "file_template": "gfs.t{cycle}z.pgrb2.0p25.f{forecast_hour}",
            "variables": ["APCP"],
            "levels": ["lev_surface"],
            "suffix": "_rain.grib2",
            "region": None,
        },
    },
    "pressure": {
        "product": "pressure",
        "display_type": "raster",
        "path_template": "/weather-display/raster/pressure/{time}/{z}/{x}/{y}.png",
        "unit": "Pa",
        "palette_id": "pressure-raster-navsea-v1",
        "recommended_range": {"min": 98000, "max": 104000},
        "supported_zoom": {"min": 2, "max": 8},
        "opacity": 0.58,
        "title": "Pressure",
        "subtitle": "Mean sea level pressure raster",
        "legend": {
            "scale_type": "continuous",
            "sections": [
                {"label": "Low", "min": 98000, "max": 100000},
                {"label": "Normal", "min": 100000, "max": 102000},
                {"label": "High", "min": 102000, "max": 104000},
            ],
            "color_stops": [
                {"value": 98000, "color": "#8e24aa"},
                {"value": 99500, "color": "#4568dc"},
                {"value": 101000, "color": "#00a7c2"},
                {"value": 102500, "color": "#5dbb63"},
                {"value": 104000, "color": "#f9a825"},
            ],
        },
        "download": {
            "base_url": "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl",
            "dir_template": "/gfs.{date}/{cycle}/atmos",
            "file_template": "gfs.t{cycle}z.pgrb2.0p25.f{forecast_hour}",
            "variables": ["PRMSL"],
            "levels": ["lev_mean_sea_level"],
            "suffix": "_pressure.grib2",
            "region": {
                "leftlon": 120,
                "rightlon": 150,
                "toplat": 50,
                "bottomlat": 20,
            },
        },
    },
    "pressure-isoline": {
        "product": "pressure-isoline",
        "display_type": "vector",
        "path_template": "/weather-display/vector/pressure-isoline/{time}/{z}/{x}/{y}.pbf",
        "unit": "Pa",
        "palette_id": "pressure-line-navsea-v1",
        "recommended_range": {"min": 98000, "max": 104000},
        "supported_zoom": {"min": 2, "max": 8},
        "opacity": 0.85,
        "title": "Mean Sea Level Pressure",
        "subtitle": "Pressure isolines",
        "legend": {
            "scale_type": "discrete",
            "sections": [
                {"label": "Low Pressure", "min": 98000, "max": 100000},
                {"label": "Normal", "min": 100000, "max": 102000},
                {"label": "High Pressure", "min": 102000, "max": 104000},
            ],
            "color_stops": [
                {"value": 98000, "color": "#8e24aa"},
                {"value": 100000, "color": "#5e92f3"},
                {"value": 102000, "color": "#26a69a"},
                {"value": 104000, "color": "#2e7d32"},
            ],
            "contour_levels": [98000, 98400, 98800, 99200, 99600, 100000, 100400, 100800, 101200, 101600, 102000, 102400, 102800, 103200, 103600, 104000],
        },
        "download": {
            "base_url": "https://nomads.ncep.noaa.gov/cgi-bin/filter_gfs_0p25.pl",
            "dir_template": "/gfs.{date}/{cycle}/atmos",
            "file_template": "gfs.t{cycle}z.pgrb2.0p25.f{forecast_hour}",
            "variables": ["PRMSL"],
            "levels": ["lev_mean_sea_level"],
            "suffix": "_pressure.grib2",
            "region": {
                "leftlon": 120,
                "rightlon": 150,
                "toplat": 50,
                "bottomlat": 20,
            },
        },
    },
}


def get_product_definition(product):
    if product not in PRODUCT_DEFINITIONS:
        raise KeyError(f"unknown display product: {product}")

    definition = deepcopy(PRODUCT_DEFINITIONS[product])
    definition["no_data"] = deepcopy(DEFAULT_NODATA)
    return definition
