from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parents[2]
DATA_ROOT = PROJECT_ROOT / "data" / "geo_mask"
SOURCE_ROOT = DATA_ROOT / "source"
OUTPUT_ROOT = Path("/home/wwwroot/weather/geo-mask")
LAND_SEA_TILE_ROOT = OUTPUT_ROOT / "land-sea"
METADATA_ROOT = OUTPUT_ROOT / "metadata"
POLICY_ROOT = OUTPUT_ROOT / "policies"

SOURCE_ROOT.mkdir(parents=True, exist_ok=True)
LAND_SEA_TILE_ROOT.mkdir(parents=True, exist_ok=True)
METADATA_ROOT.mkdir(parents=True, exist_ok=True)
POLICY_ROOT.mkdir(parents=True, exist_ok=True)

SOURCE_ASSET = SOURCE_ROOT / "ne_10m_land.geojson"
DEFAULT_SOURCE_URL = "https://raw.githubusercontent.com/nvkelso/natural-earth-vector/master/geojson/ne_10m_land.geojson"

REGION = {
    "lon_min": 120.0,
    "lon_max": 150.0,
    "lat_min": 20.0,
    "lat_max": 50.0,
}

DEFAULT_ZOOMS = [2, 4, 6, 8]
TILE_SIZE = 256


def build_mask_metadata():
    return {
        "maskId": "land-sea",
        "version": "v1",
        "source": {
            "id": "natural-earth-10m-land",
            "url": DEFAULT_SOURCE_URL,
            "licenseNote": "See upstream Natural Earth licensing terms.",
        },
        "classes": {
            "sea": 0,
            "land": 255,
            "coastTransition": "green channel 0-255",
        },
        "encoding": {
            "format": "png",
            "channels": {
                "red": "land mask: 255 land, 0 sea",
                "green": "coast transition strength: 0-255",
                "blue": "reserved",
                "alpha": "255 where mask asset has data semantics, 0 otherwise",
            },
        },
        "coverage": REGION,
        "supportedZoom": {
            "min": min(DEFAULT_ZOOMS),
            "max": max(DEFAULT_ZOOMS),
        },
        "coastTransition": {
            "supported": True,
            "radiusPixelsByZoom": {
                "2": 3,
                "4": 4,
                "6": 6,
                "8": 10,
            },
        },
        "noDataMode": "outside configured region is not emitted as tiles",
        "displayPolicies": "/geo-mask/policies/display-product-policies.json",
    }


def build_display_policies():
    return {
        "wind": {
            "sea": "normal",
            "land": "attenuate",
            "coastTransition": "soft-attenuate",
        },
        "wave": {
            "sea": "normal",
            "land": "mask",
            "coastTransition": "soft-mask",
        },
        "current": {
            "sea": "normal",
            "land": "mask",
            "coastTransition": "soft-mask",
        },
        "pressure": {
            "sea": "normal",
            "land": "normal",
            "coastTransition": "normal",
        },
    }
