"""
Django settings for hello project.

Generated by 'django-admin startproject' using Django 1.11.7.

For more information on this file, see
https://docs.djangoproject.com/en/1.11/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.11/ref/settings/
"""

import os
import sys
import re
from django.utils.translation import ugettext_lazy as _
from django.urls import reverse_lazy

SPLITTER = "-------------------------"

PROJECT_NAME = "Words"

IGNORED_PATTERN = re.compile(r"^([a-zA-Z]{1})$|^exit$")

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

GITHUB_URL = 'https://github.com/StevenBaby/words'

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.11/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'd#u0!e*tve5@v4(&%83402&9z(q6t1@@3vroz42pl4b_vi-z41'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
PROFILING = False


ALLOWED_HOSTS = [
    "127.0.0.1",
    "localhost",
    "words.local",
    "steven-arch.local",
    "words.ccyg.studio",
]


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    "words",
    "viewer",
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]


ROOT_URLCONF = 'settings.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'settings.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases

DATABASE_DIR = os.path.join(BASE_DIR, "local")
if not os.path.exists(DATABASE_DIR):
    os.makedirs(DATABASE_DIR)

DATABASE_PATH = os.path.join(DATABASE_DIR, "words.db")

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': DATABASE_PATH,
    }
}

# phonetic

PHONETIC_PATH_UK = os.path.join(DATABASE_DIR, "phonetic", "UK")
if not os.path.exists(PHONETIC_PATH_UK):
    os.makedirs(PHONETIC_PATH_UK)
PHONETIC_PATH_US = os.path.join(DATABASE_DIR, "phonetic", "US")
if not os.path.exists(PHONETIC_PATH_US):
    os.makedirs(PHONETIC_PATH_US)

SETTINGS_PATH = os.path.join(DATABASE_DIR, "settings.json")

BACKUP_PATH = os.path.join(DATABASE_DIR, "backup")
if not os.path.exists(BACKUP_PATH):
    os.makedirs(BACKUP_PATH)

# CONFIG LOG
LOG_PATH = os.path.join(DATABASE_DIR, "log")
if not os.path.exists(LOG_PATH):
    os.makedirs(LOG_PATH)

LOG_FILE = os.path.join(LOG_PATH, "words.log")
BACKUP_COUNT = 10

LOGGING = {
    'version': 1,
    # 'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '[%(asctime)s] [%(module)s] [%(lineno)d] [%(levelname)s] | %(message)s'
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose',
            "level": "DEBUG",
            # "stream": sys.stdout
        },
        'null': {
            'class': 'logging.NullHandler',
        },
        'words': {
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'formatter': 'verbose',
            'filename': LOG_FILE,
            'when': "MIDNIGHT",
            "level": "INFO",
            "backupCount": BACKUP_COUNT,
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console', ],
            'level': "INFO",
            'propagate': False,
        },
        'words': {
            'handlers': ['console', "words", ],
            'level': "DEBUG",
            'propagate': False,
        },
    },
}


# Password validation
# https://docs.djangoproject.com/en/1.11/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LOGIN_REDIRECT_URL = reverse_lazy("index")
LOGIN_URL = reverse_lazy("login")
LOGOUT_REDIRECT_URL = reverse_lazy("index")

# Internationalization
# https://docs.djangoproject.com/en/1.11/topics/i18n/

LANGUAGE_CODE = 'zh-hans'

LANGUAGES = (
    ('en-us', _('English')),  # en_US
    ('zh-hans', _('Simplified Chinese')),  # zh_Hans
)

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = False

USE_TZ = True

DATETIME_FORMAT = 'Y-m-d H:i:s'
DATETIME = "%Y-%m-%d %H:%I:%S"

DATE_FORMAT = 'Y-m-d'
DATE = "%Y-%m-%d"

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.11/howto/static-files/

STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
    os.path.join(BASE_DIR, "viewer", "static"),
    os.path.join(BASE_DIR, "local"),
]

MEDIA_ROOT = os.path.join(BASE_DIR, "static", 'media')
# MEDIA_URL = '/static/media/'


if PROFILING:
    MIDDLEWARE.append('pyinstrument.middleware.ProfilerMiddleware')

    PYINSTRUMENT_PROFILE_DIR = os.path.join(DATABASE_DIR, 'profiles')
    if not os.path.exists(PYINSTRUMENT_PROFILE_DIR):
        os.makedirs(PYINSTRUMENT_PROFILE_DIR)
