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

"""
Provide access to HERE Real Time Traffic catalogs and layers.
"""
from typing import Optional

from here.content.content import Content, ContentBinding
from here.content.real_time_traffic import TrafficIncidents
from here.platform import Environment, Platform
from here.platform.adapter import Adapter
from here.platform.layer import LayerType


class RealTimeIncidents(ContentBinding):
    """
    Entry point to provide access to HERE Real-Time Traffic catalog.
    Use an instance of this class to access layers of HERE Real-Time catalog.
    """

    default_hrns = {
        Environment.DEFAULT: "hrn:here:data::olp-here:here-traffic-incidents-v2",
    }

    def __init__(
        self,
        platform: Platform,
        catalog_hrn: Optional[str] = None,
        version: Optional[int] = None,
        adapter: Optional[Adapter] = None,
    ):
        """
        Initialize traffic incidents bindings, selecting which version to use.

        :param platform: access to the HERE platform
        :param catalog_hrn: a catalog name with HERE Real-Time Traffic schema.
            When ``None``, a default catalog is selected given the environment
            of the platform passed above. If no default can be set, an exception is raised
        :param version: which version of the to read, latest if not specified
        :param adapter: the ``Adapter`` to transform data between different representations,
          ``None`` to use the adapter defined in ``platform``.
        :raises ValueError: if a default catalog HRN is not defined for the environment
        """

        catalog_hrn = catalog_hrn or RealTimeIncidents.default_hrns.get(platform.environment)
        if not catalog_hrn:
            raise ValueError(
                "The environment does not have a default HRN for Real Time Traffic, "
                "a catalog HRN should be specified explicitly"
            )

        content = Content(platform, catalog_hrn, version)

        super().__init__(content, adapter)

        # Feature bindings
        self.traffic_incidents = TrafficIncidents(
            content, "traffic-incidents-delta-volatile", adapter, LayerType.VOLATILE
        )
