import os.path
import socket
import sys

from django.utils.translation import ugettext_lazy as _

# EZID-specific paths...
PROJECT_ROOT = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0]
SITE_ROOT = os.path.split(PROJECT_ROOT)[0]
DOWNLOAD_WORK_DIR = os.path.join(SITE_ROOT, "download")
DOWNLOAD_PUBLIC_DIR = os.path.join(DOWNLOAD_WORK_DIR, "public")
SETTINGS_DIR = os.path.join(PROJECT_ROOT, "settings")
EZID_CONFIG_FILE = os.path.join(SETTINGS_DIR, "ezid.conf")
EZID_SHADOW_CONFIG_FILE = EZID_CONFIG_FILE + ".shadow"
LOGGING_CONFIG_FILE = "logging.server.conf"
#MINTERS_PATH = os.path.join(PROJECT_ROOT, "db", "minters")
MINTERS_PATH = os.path.join(PROJECT_ROOT,'minters')

# TODO: Stop fudging the syspath
sys.path.append(os.path.join(PROJECT_ROOT, "impl"))


DEBUG = False
STANDALONE = False
TEST_RUNNER = "django.test.runner.DiscoverRunner"

MANAGERS = ADMINS = [
 ("Amir Alwash", "amir.alwash@hesge.ch"),
 ("Arnaud Gaudinat", "arnaud.gaudinat@hesge.ch")]

if "HOSTNAME" in os.environ:
  # SERVER_EMAIL = "ezid@" + os.environ["HOSTNAME"]
  SERVER_EMAIL = "info@arketype.ch"
else:
  SERVER_EMAIL = "info@arketype.ch"
  #SERVER_EMAIL = "ezid@" + socket.gethostname()

DATABASES = {
    # To keep the Django admin app happy, the store database must be
    # referred to as 'default', despite our use of a router below.
    "default": {
        "ENGINE": "django.db.backends.mysql",
        "HOST": "databases.store_host",  # see below
        "NAME": "ezid",
        "USER": "ezidrw",
        "PORT": "databases.store_port",
        "PASSWORD": "databases.store_password",  # see below
        "OPTIONS": {"charset": "utf8mb4"},
    },
    "search": {
        "ENGINE": "django.db.backends.mysql",
        "HOST": "databases.search_host",  # see below
        "NAME": "ezid",
        "USER": "ezidrw",
        "PORT": "databases.search_port",
        "PASSWORD": "databases.search_password",  # see below
        "OPTIONS": {"charset": "utf8mb4"},
        "fulltextSearchSupported": True,
    },
}

# Set the following to True if the two databases are actually one and
# the same.
SEARCH_STORE_SAME_DATABASE = True

DATABASE_ROUTERS = ["settings.routers.Router"]

TIME_ZONE = "Europe/Zurich"
TIME_FORMAT_UI_METADATA = "%Y-%m-%d %H:%M:%S"

STATIC_ROOT = os.path.join(PROJECT_ROOT, "static")
STATIC_URL = "/static/"

LANGUAGES = [
  ("en", _("English"))
]
LOCALE_PATHS = [os.path.join(STATIC_ROOT, "locale")]

LANGUAGE_CODE = 'en'

# The secret key is loaded from the store database by config.load.
SECRET_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

PASSWORD_HASHERS = [
    "django.contrib.auth.hashers.PBKDF2PasswordHasher",
    "userauth.LdapSha1PasswordHasher",
]

MIDDLEWARE_CLASSES = (
    "django.middleware.common.CommonMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.locale.LocaleMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "middleware.ExceptionScrubberMiddleware",
)

ROOT_URLCONF = "settings.urls"

SESSION_COOKIE_PATH = "/"
SESSION_EXPIRE_AT_BROWSER_CLOSE = True
SESSION_COOKIE_AGE = 7 * 86400
SESSION_SERIALIZER = "django.contrib.sessions.serializers.PickleSerializer"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [os.path.join(PROJECT_ROOT, "templates")],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.contrib.messages.context_processors.messages",
                "django.template.context_processors.request",
                "django.template.context_processors.i18n",
                "django.contrib.auth.context_processors.auth",
            ]
        },
    }
]

INSTALLED_APPS = [
    "django.contrib.staticfiles",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "ui_tags",
    "ezidapp",
]

MESSAGE_STORAGE = "django.contrib.messages.storage.session.SessionStorage"

# EZID-specific settings...
# DAEMON_THREADS_ENABLED:
#   - True: Always enable daemon threads
#   - False: Always disable daemon threads
#   - 'auto': Enable daemon threads if, and only if, process is running under Apache /
#      mod_wsgi.
DAEMON_THREADS_ENABLED = True
assert DAEMON_THREADS_ENABLED in (True, False, 'auto')
if DAEMON_THREADS_ENABLED == 'auto':
    DAEMON_THREADS_ENABLED = os.environ.get('IS_RUNNING_UNDER_MOD_WSGI', False)
LOCALIZATIONS = { "default": ("ARKetype", ["alert@arketype.ch"]),"info": ("ARKetype", ["info@arketype.ch","rene.schneider@hesge.ch"]) }

# The following is a necessarily cockamamie scheme to get passwords
# and other sensitive information from the EZID configuration system
# into this file.  'injectSecrets' should be called by the settings
# module corresponding to the deployment level, after all settings are
# in place.

SECRET_PATHS = [
    ("DATABASES", "default", "HOST"),
    ("DATABASES", "default", "PASSWORD"),
    ("DATABASES", "default", "PORT"),
    ("DATABASES", "search", "HOST"),
    ("DATABASES", "search", "PASSWORD"),
    ("DATABASES", "search", "PORT"),
]

PROTO_SUPER_SHOULDER ={
    "doi:10.7286/":"doi:10.7286/V1", # doi:10.7286/
    "doi:10.4246/":"doi:10.4246/P6", # doi:10.4246/
    "ark:/88435/":"ark:/88435/dc", # ark:/88435/
    "doi:10.15697/":"doi:10.15697/FK2", # doi:10.15697/
    "ark:/12345/":"ark:/12345/fk8", # ark:/12345/
}


def injectSecrets(deploymentLevel):
    import config_loader

    config = config_loader.Config(
        SITE_ROOT,
        PROJECT_ROOT,
        EZID_CONFIG_FILE,
        EZID_SHADOW_CONFIG_FILE,
        deploymentLevel,
    )
    for path in SECRET_PATHS:
        o = sys.modules["settings.common"]  # this module
        for p in path[:-1]:
            if type(p) is str and hasattr(o, p):
                o = getattr(o, p)
            else:
                o = o[p]
        if type(path[-1]) is str and hasattr(o, path[-1]):
            setattr(o, path[-1], config.getOption(getattr(o, path[-1])))
        else:
            o[path[-1]] = config.getOption(o[path[-1]])
