# Copyright (C) 2020-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.

"""
This module contains a :class:`DataStatisticsApi` class to perform API operations.

The HERE API reference documentation used in this module can be found here:
|statistics_api_reference|

.. |statistics_api_reference| raw:: html

   <a href="https://development-architecture.pages.gitlab.in.here.com/olp-apis/?urls.primaryName=data-statistics-v1#/" target="_blank">Statistics API Reference</a>  # noqa E501
"""

from typing import Any, Optional

from here.platform.api.base_api import BaseApi
from here.platform.auth import Auth
from here.platform.config import ApplicationConfig, PlatformConfig
from here.platform.exceptions import PlatformException


class DataStatisticsApi(BaseApi):
    """
    This class provides access to HERE platform Data Statistics APIs.

    Use the statistics service to retrieve statistics for the data coverage of catalogs.
    It shows where data exists, freshness of the data and other statistics.
    """

    def __init__(
        self,
        base_url: str,
        platform_config: PlatformConfig,
        application_config: ApplicationConfig,
        auth: Optional[Auth],
        proxies: Optional[dict] = None,
    ):
        """
        Instantiate DataStatisticsApi object.

        :param base_url: base url
        :param platform_config: a mandatory :class:`PlatformConfig` object to provide
        configuration information for the API.
        :param application_config: a mandatory :class:`ApplicationConfig` object to provide
        configuration information for the API.
        :param auth: an Authentication instance.
        :param proxies: an optional proxy configuration. Defaults to the environment proxy
        configuration.
        """
        super(DataStatisticsApi, self).__init__(
            platform_config=platform_config,
            application_config=application_config,
            auth=auth,
            proxies=proxies,
        )
        self.base_url = base_url

    def get_tile_map(
        self,
        layer_id: str,
        data_level: str,
    ) -> bytes:
        """
        Retrieve bitmap representing availability of data in partitions.

        :param layer_id: The ID of the index layer you want to query.
        :param data_level: One of the Data Levels configured for this layer.
            By default, assets generated at deepest data level are returned.
            Note that assets returned for data levels greater than 11 represent data
            at data level 11.
        :return: response from the API.
        :raises PlatformException: If platform responds with an HTTP error.
        """
        path = f"/layers/{layer_id}/tilemap"
        url = self.format_url(self.base_url, path)
        params = {"datalevel": data_level}
        resp = self.get(url, params=params)
        if resp.status_code == 200:
            return resp.content
        else:
            raise PlatformException(resp)

    def get_summary(self, layer_id: str) -> Any:
        """
        Retrieve layer statistics.

        :param layer_id: The layer ID of the index layer.
        :return: response from the API.
        :raises PlatformException: If platform responds with an HTTP error.
        """
        path = f"/layers/{layer_id}/summary"
        url = self.format_url(self.base_url, path)
        resp = self.get(url)
        if resp.status_code == 200:
            return resp.json()
        else:
            raise PlatformException(resp)

    def get_size_map(self, layer_id: str, data_level: str) -> bytes:
        """
        Retrieve HeatMap representing partition size.

        :param layer_id: The ID of the index layer you want to query.
        :param data_level: One of the Data Levels configured for this layer.
            By default, assets generated at deepest data level are returned.
            Note that assets returned for data levels greater than 11 represent data
            at data level 11.
        :return: response from the API.
        :raises PlatformException: If platform responds with an HTTP error.
        """
        path = f"/layers/{layer_id}/heatmap/size"
        url = self.format_url(self.base_url, path)
        params = {"datalevel": data_level}
        resp = self.get(url, params=params)
        if resp.status_code == 200:
            return resp.content
        else:
            raise PlatformException(resp)

    def get_age_map(self, layer_id: str, data_level: str) -> bytes:
        """
        Retrieve HeatMap representing partition size.

        :param layer_id: The ID of the index layer you want to query.
        :param data_level: One of the Data Levels configured for this layer.
            By default, assets generated at deepest data level are returned.
            Note that assets returned for data levels greater than 11 represent data
            at data level 11.
        :return: response from the API.
        :raises PlatformException: If platform responds with an HTTP error.
        """
        path = f"/layers/{layer_id}/heatmap/age"
        url = self.format_url(self.base_url, path)
        params = {"datalevel": data_level}
        resp = self.get(url, params=params)
        if resp.status_code == 200:
            return resp.content
        else:
            raise PlatformException(resp)
