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

"""HERE Platform Python SDK, Content access module
"""

from typing import Optional

from here.platform import Platform
from here.platform.adapter import Adapter, ContentAdapter


class Content:
    """
    Provide schema-specific access to content catalogs of selected schemas.
    """

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

        :param platform: access to the HERE platform
        :param catalog_hrn: which instance of the content to read
        :param version: which version of the content to read, latest if not specified
        :param adapter: the ``Adapter`` to transform data between different representations,
          ``None`` to use the default adapter of the platform.
        """

        adapter = adapter or platform.adapter

        # We use this to convert from tabular, when set
        self.adapter: Optional[ContentAdapter] = adapter.content_adapter

        # Make sure the platform adapter doesn't interfere with raw reads
        self.catalog = platform.get_catalog(catalog_hrn, adapter=platform.default_adapter)

        # The version must be set or detected once and in one place, otherwise
        # we risk to operate on two different versions if the catalog is modified
        # between multiple `latest_version` calls.
        self.version: int = version or self.catalog.latest_version()
        if not self.version:
            raise Exception(f"Catalog {catalog_hrn} is empty.")
        # TODO: check that a version exists if specified from outside
