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

"""
A class to hold road attributes
"""
from typing import Optional

from here.content.hmc.baseattributes import BaseLayer
from here.platform.adapter import Adapter


class RoadAttributes(BaseLayer):
    """
    A class to hold road attributes
    """

    def get_attribute(self, pid, attribute_name, adapter: Optional[Adapter] = None):
        """
        Get the attribute for given partitionId and attribute name
        Intended attributes: accessible_by, overpass_underpass, functional_class,
        iso_country_code, physical, road_class, special_traffic_area_category, display_level

        :param pid: a partition ID
        :param attribute_name: attribute collection name in the partition
        :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: attribute dict or an adapter-specific type
        """

        # first fill the attribute
        self.fill_attribute(pid, attribute_name)

        content_adapter = self._content_adapter(adapter)

        if attribute_name not in self.attributes or pid not in self.attributes[attribute_name]:
            return None

        if content_adapter is None:
            return self.attributes[attribute_name][pid]
        else:
            columns, data = self.get_tabular_data(pid, attribute_name)
            return content_adapter.from_tabular(columns, data)

    def fill_attribute(self, pid, attribute_name):
        """
        Fill the attribute for given attribute name
        Intended attributes: accessible_by, overpass_underpass, functional_class,
        iso_country_code, physical, road_class, special_traffic_area_category, display_level

        :param pid: a partition ID
        :param attribute_name: attribute collection name in the partition
        """

        # initialize self.attributes[attribute_name] container
        self.attributes.setdefault(attribute_name, {})

        # do nothing if pid is already filled
        if pid in self.attributes[attribute_name]:
            return

        # get partition object
        partition = self.get_partition(pid)

        # associate segments with attributes via segment_anchors
        attributes = self.resolve_single_segment_anchors(partition, attribute_name)

        # update attributes container
        self.attributes[attribute_name].update(attributes)

    def get_ranged_attribute(self, attribute_name, pid, sid, direction, offset=None):
        """
        Get an attribute at a specific offset if offset is given
        otherwise return a list of attributes for all ranges.

        :param attribute_name: attribute name to look for
        :param pid: partition ID
        :param sid: segment ID
        :param direction: travel direction: 0: UNKNOWN, 1: BOTH, 2: FORWARD, 3: BACKWARD
        :param offset: should be within [0, 1]

        :return: an attribute if offset is given, otherwise a list of attributes
        """

        attributes = None
        if attribute_name in self.attributes:
            if pid in self.attributes[attribute_name]:
                if sid in self.attributes[attribute_name][pid]:
                    if direction in self.attributes[attribute_name][pid][sid]:
                        attributes = self.attributes[attribute_name][pid][sid][direction]
        if attributes is None:
            return None
        else:
            if offset is None:
                return attributes
            else:
                attribute = None
                for attr in attributes:
                    if attr.start_offset <= offset <= attr.end_offset:
                        attribute = attr.attribute
                        break
                return attribute

    def get_tabular_data(self, pid, attribute_name):
        """
        Convert the given attribute to tabular data
        :param pid: partition ID
        :param attribute_name: attribute name to look for
        :return: columns, data
        """
        return self.get_tabular_data_from_single_segment(pid, attribute_name)
