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

"""Environment module

This module defines the different platform environments supported.
Users can select the platform environment using enum, or by setting environment variable.
"""

import enum
import os

from here.platform.exceptions import ConfigException


class Environment(enum.Enum):
    """
    Environment enum defines the different platform environment supported.
    Environment: Default, Internal, China, Local.
    """

    DEFAULT = 1
    INTERNAL = 2
    CHINA = 3
    LOCAL = 4

    @classmethod
    def from_default(cls) -> "Environment":
        """
        Return the `Environment` from default.

        If environmental variable is set, this value will override the default value.

        :return: environment
        """
        try:
            environment = cls.from_env_variable()
        except ConfigException:
            environment = Environment.DEFAULT
        return environment

    @classmethod
    def from_env_variable(cls) -> "Environment":
        """
        Return the `Environment` from environment variable.

        Expected variable name: ``HERE_ENVIRONMENT``.
        :return: environment
        :raises ConfigException: Missing environmental variable
        """
        env_variable_key = "HERE_ENVIRONMENT"
        environment = os.environ.get(env_variable_key)
        if environment:
            try:
                return Environment[environment.upper()]
            except KeyError:
                raise ConfigException(
                    "Erroneous environmental variable: {}".format(",".join(env_variable_key))
                )
        else:
            raise ConfigException(
                "Environmental variable not set: {}".format(",".join(env_variable_key))
            )
