# Copyright (C) 2019-2022 HERE Global B.V. and its affiliate(s).
# All rights reserved.
#
# This software and other materials contain proprietary information
# controlled by HERE and are protected by applicable copyright legislation.
# Any use and utilization of this software and other materials and
# disclosure to any third parties is conditional upon having a separate
# agreement with HERE for the access, use, utilization or disclosure of this
# software. In the absence of such agreement, the use of the software is not
# allowed.

"""
Helper utilities for the ipyleaflet-based inspector
"""
from typing import Dict

from here.inspector.brand import colors
from here.inspector.ipyleaflet.basemaps import external_basemaps, here_basemaps
from here.inspector.styles import Color, Theme
from mapquadlib import HereQuad

# Conversion tables from generic inspector colors to Ipyleaflet colors
# This is the main color, used for the `color` parameter of the ipyleaflet style

light_colors = {
    Color.AQUA: colors.HERE_Aqua_Dark,
    Color.RED: colors.Red,
    Color.PINK: colors.Pink,
    Color.ORANGE: colors.Orange,
    Color.YELLOW: colors.Yellow,
    Color.GREEN: colors.Dark_Green,
    Color.BLUE: colors.Dark_Blue,
    Color.PURPLE: colors.Purple,
    Color.GRAY: colors.HERE_Gray,
}

dark_colors = {
    Color.AQUA: colors.HERE_Aqua,
    Color.RED: colors.Red_75,
    Color.PINK: colors.Pink_75,
    Color.ORANGE: colors.Orange_75,
    Color.YELLOW: colors.Yellow_75,
    Color.GREEN: colors.Light_Green,
    Color.BLUE: colors.Light_Blue,
    Color.PURPLE: colors.Purple_75,
    Color.GRAY: colors.HERE_Gray_50,
}

theme_colors = {
    Theme.LIGHT_BLANK: light_colors,
    Theme.LIGHT_MAP: light_colors,
    # Theme.DARK_BLANK: dark_colors,
    Theme.DARK_MAP: dark_colors,
}

theme_basemap = {
    Theme.LIGHT_BLANK: None,
    Theme.LIGHT_MAP: external_basemaps.Esri.WorldTopoMap,
    # Theme.DARK_BLANK: None,
    Theme.DARK_MAP: external_basemaps.CartoDB.DarkMatter,
}

theme_here_basemap = {
    Theme.LIGHT_BLANK: None,
    Theme.LIGHT_MAP: here_basemaps.reducedDay,
    # Theme.DARK_BLANK: None,
    Theme.DARK_MAP: here_basemaps.reducedNight,
}


def to_style(theme: Theme, color: Color) -> Dict:
    """Convert a color to an ipyleaflet style given a theme"""
    return {"color": theme_colors[theme][color], "opacity": 1}


def _geojson_with_longkey(tile_id: int) -> dict:
    """
    Return GeoJSON geometry for given mapquad with its long key as parameter.

    The quad's "long key" (an integer) will appear as "tile_id" in the
    properties.
    :param tile_id: Tile id
    :returns: A dict representing a GeoJSON geometry (polygon) for the mapquad.

    Example:

    >>> _geojson_with_longkey(1483)  # doctest: +SKIP
    {'type': 'Polygon',
     'coordinates': [[[101.25, 33.75],
       [112.5, 33.75],
       [112.5, 45.0],
       [101.25, 45.0],
       [101.25, 33.75]]],
     'properties': {'tile_id': 1483}}
    """  # noqa: D412
    quad = HereQuad.from_long_key(tile_id)
    gj: dict = quad.bounding_box._geojson()
    if "properties" not in gj:
        gj["properties"] = {}
    gj["properties"]["tile_id"] = tile_id
    return gj
