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

"""
Bindings for the HMC Administrative Tile Index.
"""
from dataclasses import dataclass
from typing import List, Mapping

from google.protobuf.message import Message
from here.content.base import ContentIndexer, ContentPartition, SingleLayerBinding
from here.content.content import hmc_schema_version
from here.content.utils.proto import parse_ref
from here.platform.adapter import Identifier, Ref


@dataclass
class CountryTiles:
    """Country Tile Index."""

    iso_country_code: Identifier
    tile_id: List[int]

    indexer = ContentIndexer(identifier="iso_country_code")


@dataclass
class LocationTiles:
    """Location Tile Index."""

    location_ref: Ref
    iso_country_code: str
    tile_id: List[int]
    boundary_tile_id: List[int]

    indexer = ContentIndexer(refs={"location": "location_ref"})


class AdminIndex(SingleLayerBinding):
    """
    Bindings for the HMC Administrative Tile Index.
    """

    @classmethod
    def schema_hrn(cls) -> List[str]:
        """
        :return: the HRN of schema this binding can parse and index
        """
        return [f"hrn:here:schema:::com.here.schema.rib:indexed-locations_v2:{hmc_schema_version}"]

    @classmethod
    def object_types(cls) -> Mapping[str, type]:
        """
        :return: the types of attributes supported by this binding
        """
        return {"country": CountryTiles, "location": LocationTiles}

    @classmethod
    def parse_and_index(cls, partition_id: str, msg: Message) -> ContentPartition:
        """
        Parse and index decoded message.

        :param partition_id: identifier of the partition
        :param msg: decoded content from the layer, in the form of a Protobuf message
        :return: the content of a partition parsed and indexed in its own data structure
        """
        assert hasattr(msg, "tile_id")
        assert hasattr(msg, "indexed_location")

        tile_ids = [int(t) for t in msg.tile_id]  # type: ignore

        country = [CountryTiles(iso_country_code=Identifier(partition_id), tile_id=tile_ids)]

        location = [
            LocationTiles(
                location_ref=parse_ref(il.location_ref),
                tile_id=[tile_ids[i] for i in il.tile_index],
                boundary_tile_id=[tile_ids[i] for i in il.boundary_tile_index],
                iso_country_code=il.iso_country_code,
            )
            for il in msg.indexed_location  # type: ignore
        ]

        return ContentPartition(cls, country=country, location=location)
