# Copyright (C) 2020-2021 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 functions to combine speed limit from navigation-attributes layer and
advancded-navigation-attributes layer
"""

from typing import Optional

from here.content.content import Content, ContentBinding
from here.content.hmc.advancednavigationattributes import AdvancedNavigationAttributes
from here.content.hmc.navigationattributes import NavigationAttributes
from here.platform.adapter import Adapter


class SegmentToSpeedLimit(ContentBinding):
    """
    class to combine speedlimit from navigation and advanced navigation attributes layers
    """

    def __init__(
        self,
        content: Content,
        na: str = "navigation-attributes",
        ana: str = "advanced-navigation-attributes",
        adapter: Optional[Adapter] = None,
    ):
        """
        :param content: Content object
        :param na: layer ID for navigation-attributes
        :param ana: layer ID for advanced-navigation-attributes
        :param adapter: the ``Adapter`` to transform data between different representations,
            ``None`` to use the adapter defined in ``content``.
        """
        super().__init__(content, adapter)

        # Attribute bindings used internally should always use the default adapter
        self.advanced_navigation_attributes = AdvancedNavigationAttributes(
            content, ana, adapter=self._default_adapter
        )
        self.navigation_attributes = NavigationAttributes(
            content, na, adapter=self._default_adapter
        )

    def get_attribute(self, pid, adapter: Optional[Adapter] = None):
        """
        Get segment associated speed_limits from navigation and advanced navigation
        attributes layers
        :param pid: partition ID
        :param adapter: the ``Adapter`` to transform data between different representations,
            ``None`` to use the adapter defined in the constructor or, if unset, in ``content``.
        :return: a dict of segment -> speed_limit or adapter-specific
        """

        content_adapter = self._content_adapter(adapter)

        if content_adapter is None:
            atts = {}
            speed_limt_from_na = self.navigation_attributes.get_attribute(pid, "speed_limit")
            if speed_limt_from_na is not None:
                atts.update(speed_limt_from_na)
            speed_limit_from_ana = self.advanced_navigation_attributes.get_attribute(
                pid, "speed_limit"
            )
            if speed_limit_from_ana is not None:
                atts.update(speed_limit_from_ana)
            return atts
        else:
            columns, data = self.navigation_attributes.get_tabular_data(pid, "speed_limit")
            _, data1 = self.advanced_navigation_attributes.get_tabular_data(pid, "speed_limit")
            return content_adapter.from_tabular(columns, data + data1)
