# Path: esteid/constants.py
# MOBILE_ID_DEMO_URL = "https://tsp.demo.sk.ee/mid-api"
# MOBILE_ID_LIVE_URL = "https://mid.sk.ee/mid-api"
# MOBILE_ID_DEMO_SERVICE_NAME = "DEMO"
# MOBILE_ID_DEMO_SERVICE_UUID = "00000000-0000-0000-0000-000000000000"
# SMART_ID_DEMO_URL = "https://sid.demo.sk.ee/smart-id-rp/v1"
# SMART_ID_LIVE_URL = "https://rp-api.smart-id.com/v1"
# SMART_ID_DEMO_SERVICE_NAME = MOBILE_ID_DEMO_SERVICE_NAME
# SMART_ID_DEMO_SERVICE_UUID = MOBILE_ID_DEMO_SERVICE_UUID
# OCSP_DEMO_URL = "http://demo.sk.ee/ocsp"
# OCSP_LIVE_URL = "http://ocsp.sk.ee/"
# TSA_DEMO_URL = "http://demo.sk.ee/tsa/"
# TSA_LIVE_URL = "http://tsa.sk.ee"
# HASH_SHA256 = "SHA256"
# HASH_SHA384 = "SHA384"
# HASH_SHA512 = "SHA512"
# HASH_ALGORITHMS = {
#     HASH_SHA256,
#     HASH_SHA384,
#     HASH_SHA512,
# }
#     ESTONIA = "EE"
#     LATVIA = "LV"
#     LITHUANIA = "LT"
#     ALL = (ESTONIA, LATVIA, LITHUANIA)
#     ENG = "ENG"
#     EST = "EST"
#     LIT = "LIT"
#     RUS = "RUS"
#     ALL = (ENG, EST, LIT, RUS)
#     _MAP_ISO_639_1_TO_MID = {
#         "en": ENG,
#         "et": EST,
#         "lt": LIT,
#         "ru": RUS,
#     }
# class Countries:
# class Languages:
#     def identify_language(cls, language_code):
# 
# Path: esteid/constants.py
# class Countries:
#     """Mainly used by SmartID, also phone code"""
# 
#     ESTONIA = "EE"
#     LATVIA = "LV"
#     LITHUANIA = "LT"
# 
#     ALL = (ESTONIA, LATVIA, LITHUANIA)
# 
# class Languages:
#     """Used by MobileID"""
# 
#     ENG = "ENG"
#     EST = "EST"
#     LIT = "LIT"
#     RUS = "RUS"
# 
#     ALL = (ENG, EST, LIT, RUS)
# 
#     # also allow ISO 639-1 (or alpha-2) codes
#     _MAP_ISO_639_1_TO_MID = {
#         "en": ENG,
#         "et": EST,
#         "lt": LIT,
#         "ru": RUS,
#     }
# 
#     @classmethod
#     def identify_language(cls, language_code):
#         all_langs = {**cls._MAP_ISO_639_1_TO_MID, **{c.lower(): c for c in cls.ALL}}
#         try:
#             language_code_lower = language_code.lower()
#             return all_langs[language_code_lower]
#         except (AttributeError, KeyError) as e:
#             raise ImproperlyConfigured(f"Language should be one of {','.join(all_langs)}, got `{language_code}`") from e


import re
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from esteid import constants
from esteid.constants import Countries, Languages
ID_CARD_FRAME_TARGET_ORIGIN = getattr(settings, "ID_CARD_FRAME_TARGET_ORIGIN", "*")

# *** Mobile ID ***

MOBILE_ID_ENABLED = getattr(settings, "MOBILE_ID_ENABLED", False)
# Whether to use demo services and credentials for Mobile ID. Default to global demo mode
MOBILE_ID_TEST_MODE = getattr(settings, "MOBILE_ID_TEST_MODE", ESTEID_DEMO)
# MobileID Relying party name and UUID, for DEMO they are always the same so no need to explicitly set them
MOBILE_ID_SERVICE_NAME = getattr(settings, "MOBILE_ID_SERVICE_NAME", None)
MOBILE_ID_SERVICE_UUID = getattr(settings, "MOBILE_ID_SERVICE_UUID", None)
MOBILE_ID_API_ROOT = getattr(settings, "MOBILE_ID_API_ROOT", None)

# Mobile phone number sanity check: optional, a regexp pattern. If not set or None, defaults to a simple regexp.
# To disable checks, set it to empty string.
_MOBILE_ID_PHONE_NUMBER_REGEXP = getattr(settings, "MOBILE_ID_PHONE_NUMBER_REGEXP", None)
if _MOBILE_ID_PHONE_NUMBER_REGEXP is None:
    # Mobile ID supports Estonian and Lithuanian phones
    _MOBILE_ID_PHONE_NUMBER_REGEXP = re.compile(r"^\+37[02]\d{7,8}$")
elif _MOBILE_ID_PHONE_NUMBER_REGEXP:
    try:
        _MOBILE_ID_PHONE_NUMBER_REGEXP = re.compile(_MOBILE_ID_PHONE_NUMBER_REGEXP)
    except ValueError as e:
        raise ImproperlyConfigured("MOBILE_ID_PHONE_NUMBER_REGEXP must be a valid regular expression") from e

MOBILE_ID_PHONE_NUMBER_REGEXP = _MOBILE_ID_PHONE_NUMBER_REGEXP

_MOBILE_ID_DEFAULT_LANGUAGE = getattr(settings, "MOBILE_ID_DEFAULT_LANGUAGE", None)

if _MOBILE_ID_DEFAULT_LANGUAGE is None:
    try:

# Path: esteid/constants.py
# MOBILE_ID_DEMO_URL = "https://tsp.demo.sk.ee/mid-api"
# MOBILE_ID_LIVE_URL = "https://mid.sk.ee/mid-api"
# MOBILE_ID_DEMO_SERVICE_NAME = "DEMO"
# MOBILE_ID_DEMO_SERVICE_UUID = "00000000-0000-0000-0000-000000000000"
# SMART_ID_DEMO_URL = "https://sid.demo.sk.ee/smart-id-rp/v1"
# SMART_ID_LIVE_URL = "https://rp-api.smart-id.com/v1"
# SMART_ID_DEMO_SERVICE_NAME = MOBILE_ID_DEMO_SERVICE_NAME
# SMART_ID_DEMO_SERVICE_UUID = MOBILE_ID_DEMO_SERVICE_UUID
# OCSP_DEMO_URL = "http://demo.sk.ee/ocsp"
# OCSP_LIVE_URL = "http://ocsp.sk.ee/"
# TSA_DEMO_URL = "http://demo.sk.ee/tsa/"
# TSA_LIVE_URL = "http://tsa.sk.ee"
# HASH_SHA256 = "SHA256"
# HASH_SHA384 = "SHA384"
# HASH_SHA512 = "SHA512"
# HASH_ALGORITHMS = {
#     HASH_SHA256,
#     HASH_SHA384,
#     HASH_SHA512,
# }
#     ESTONIA = "EE"
#     LATVIA = "LV"
#     LITHUANIA = "LT"
#     ALL = (ESTONIA, LATVIA, LITHUANIA)
#     ENG = "ENG"
#     EST = "EST"
#     LIT = "LIT"
#     RUS = "RUS"
#     ALL = (ENG, EST, LIT, RUS)
#     _MAP_ISO_639_1_TO_MID = {
#         "en": ENG,
#         "et": EST,
#         "lt": LIT,
#         "ru": RUS,
#     }
# class Countries:
# class Languages:
#     def identify_language(cls, language_code):
# 
# Path: esteid/constants.py
# class Countries:
#     """Mainly used by SmartID, also phone code"""
# 
#     ESTONIA = "EE"
#     LATVIA = "LV"
#     LITHUANIA = "LT"
# 
#     ALL = (ESTONIA, LATVIA, LITHUANIA)
# 
# class Languages:
#     """Used by MobileID"""
# 
#     ENG = "ENG"
#     EST = "EST"
#     LIT = "LIT"
#     RUS = "RUS"
# 
#     ALL = (ENG, EST, LIT, RUS)
# 
#     # also allow ISO 639-1 (or alpha-2) codes
#     _MAP_ISO_639_1_TO_MID = {
#         "en": ENG,
#         "et": EST,
#         "lt": LIT,
#         "ru": RUS,
#     }
# 
#     @classmethod
#     def identify_language(cls, language_code):
#         all_langs = {**cls._MAP_ISO_639_1_TO_MID, **{c.lower(): c for c in cls.ALL}}
#         try:
#             language_code_lower = language_code.lower()
#             return all_langs[language_code_lower]
#         except (AttributeError, KeyError) as e:
#             raise ImproperlyConfigured(f"Language should be one of {','.join(all_langs)}, got `{language_code}`") from e


# Path: esteid/settings.py
import re
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from esteid import constants
from esteid.constants import Countries, Languages

ID_CARD_FRAME_TARGET_ORIGIN = getattr(settings, "ID_CARD_FRAME_TARGET_ORIGIN", "*")

# *** Mobile ID ***

MOBILE_ID_ENABLED = getattr(settings, "MOBILE_ID_ENABLED", False)
# Whether to use demo services and credentials for Mobile ID. Default to global demo mode
MOBILE_ID_TEST_MODE = getattr(settings, "MOBILE_ID_TEST_MODE", ESTEID_DEMO)
# MobileID Relying party name and UUID, for DEMO they are always the same so no need to explicitly set them
MOBILE_ID_SERVICE_NAME = getattr(settings, "MOBILE_ID_SERVICE_NAME", None)
MOBILE_ID_SERVICE_UUID = getattr(settings, "MOBILE_ID_SERVICE_UUID", None)
MOBILE_ID_API_ROOT = getattr(settings, "MOBILE_ID_API_ROOT", None)

# Mobile phone number sanity check: optional, a regexp pattern. If not set or None, defaults to a simple regexp.
# To disable checks, set it to empty string.
_MOBILE_ID_PHONE_NUMBER_REGEXP = getattr(settings, "MOBILE_ID_PHONE_NUMBER_REGEXP", None)
if _MOBILE_ID_PHONE_NUMBER_REGEXP is None:
    # Mobile ID supports Estonian and Lithuanian phones
    _MOBILE_ID_PHONE_NUMBER_REGEXP = re.compile(r"^\+37[02]\d{7,8}$")
elif _MOBILE_ID_PHONE_NUMBER_REGEXP:
    try:
        _MOBILE_ID_PHONE_NUMBER_REGEXP = re.compile(_MOBILE_ID_PHONE_NUMBER_REGEXP)
    except ValueError as e:
        raise ImproperlyConfigured("MOBILE_ID_PHONE_NUMBER_REGEXP must be a valid regular expression") from e

MOBILE_ID_PHONE_NUMBER_REGEXP = _MOBILE_ID_PHONE_NUMBER_REGEXP

_MOBILE_ID_DEFAULT_LANGUAGE = getattr(settings, "MOBILE_ID_DEFAULT_LANGUAGE", None)

if _MOBILE_ID_DEFAULT_LANGUAGE is None:
    try:

        _MOBILE_ID_DEFAULT_LANGUAGE = Languages.identify_language(settings.LANGUAGE_CODE)