# 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.

"""
Features related to HERE basemaps.
"""

from typing import Union

import xyzservices.providers as tile_providers
from ipyleaflet import TileLayer
from xyzservices.lib import Bunch, TileProvider

here_basemaps = tile_providers.HEREv3

external_basemaps = Bunch()

for provider in tile_providers:
    if provider not in ("HERE", "HEREv3"):
        external_basemaps[provider] = tile_providers[provider]


def basemap_to_tiles(basemap: Union[TileProvider, dict], **kwargs):
    """Convert basemap into a ``TileLayer`` object.

    :param basemap: Either a dictionary or an object of class:`xyzservices.lib.TileProvider`
    :param kwargs: Extra keyword arguments to pass to the TileLayer constructor.
    :return: An object of class:`ipyleaflet.TileLayer`
    :raises ValueError: If type of basemap is not ``dict`` or class:`xyzservices.lib.TileProvider`
    """
    if type(basemap) is dict:
        url = basemap.get("url", "")
    elif isinstance(basemap, TileProvider):
        url = basemap.build_url()
    else:
        raise ValueError("Invalid basemap")

    return TileLayer(
        url=url,
        max_zoom=basemap.get("max_zoom", 19),
        min_zoom=basemap.get("min_zoom", 1),
        attribution=basemap.get("html_attribution", "") or basemap.get("attribution", ""),
        name=basemap.get("name", ""),
        **kwargs
    )
