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

"""
A class to hold environmental zone attributes
"""
from typing import Optional

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


class EnvironmentalZonesAttributes(BaseLayer):
    """
    A class to hold environmental zone attributes
    """

    def get_attribute(self, pid: str, attribute_name: str, adapter: Optional[Adapter] = None):
        """
        Get the attribute for given partitionId and attribute name
        Intended attributes: environmental_zone

        :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: str, attribute_name: str):
        """
        Fill the attribute for given attribute name
        Intended attributes: environmental_zone

        :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)

        if attribute_name in "environmental_zone":
            # build the environmental_zone dict
            self.build_envzones(partition)

    def build_envzones(self, partition):
        """
        Fill the environmental zone attribute
        Intended attributes: environmental zone

        :param partition: a protobuf partition object
        """

        # initialize self.attributes container
        self.attributes.setdefault("environmental_zone", {})

        pid = partition.partition_name
        attributes = {pid: {}}
        for envzone in partition.environmental_zone:
            attributes[pid][envzone.identifier] = envzone

        # update attributes container
        self.attributes["environmental_zone"].update(attributes)

    def get_tabular_data(self, pid: str, attribute_name: str):
        """
        Convert the given attribute to tabular data
        :param pid: partition ID
        :param attribute_name: attribute name to look for
        :return: columns, data
        """
        columns = ("partitionId", "envzoneId", "attribute")
        data = []
        if attribute_name in self.attributes and pid in self.attributes[attribute_name]:
            for aid, att in self.attributes[attribute_name][pid].items():
                data.append((pid, aid, att))
        return columns, data
