# Copyright (C) 2020-2023 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 Parser Factory module.
"""

import io
import logging
from pathlib import Path
from typing import Optional, Union

from here.platform.exceptions import SchemaException
from here.platform.schema import ProtobufParser
from here.platform.schema.geojson_parser import GeoJsonParser
from here.platform.schema.json_parser import JsonParser
from here.platform.schema.parser import Parser

logger = logging.getLogger(__name__)


class ParserFactory:
    """Factory that is exposed to accept request for content Parser initialization."""

    @staticmethod
    def get_parser(
        schema_pkg_file: Union[str, Path, io.BytesIO], content_type: Optional[str]
    ) -> "Parser":
        """
        Initialize an appropriate Parser instance based on the layer content_type.

        :param schema_pkg_file: the schema package zip-file as a str, Path or file-like object
        :param content_type: The MIME type of the blobs stored in the layer
        :raises SchemaException: if the required meta information is missing in schema package
        :returns: an instance of a Parser
        """

        if content_type in ["application/protobuf", "application/x-protobuf"]:
            return ProtobufParser(schema_pkg_file)
        elif content_type == "application/json":
            return JsonParser(schema_pkg_file)
        elif content_type in ["application/vnd.geo+json", "application/geo+json"]:
            return GeoJsonParser(schema_pkg_file)

        raise SchemaException(
            "Schema could not be instantiated. The content-type of the layer is not supported"
        )
