"""
Django settings for webproj project.

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

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

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

import os
from pathlib import Path

from django.utils.translation import ugettext_lazy as _
from telegram.bot import Bot

from . import fsconfig

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

DEVELOPING = os.uname()[1] == 'forest'

SITE_NAME = fsconfig.SITE_NAME
BASE_DIR = fsconfig.BASE_DIR
SECRET_KEY = fsconfig.SECRET_KEY

DEBUG = fsconfig.DEBUG and DEVELOPING

ALLOWED_HOSTS = [
    *fsconfig.SITE_DOMAIN,
    'localhost',
    '127.0.0.1',
]

ALLOWED_HOST_FOR_MAIL = ALLOWED_HOSTS[0]

if DEBUG and DEVELOPING:
    ALLOWED_HOSTS = '*'

if not DEBUG:
    SECURE_BROWSER_XSS_FILTER = True
    SECURE_HSTS_INCLUDE_SUBDOMAINS = True
    SECURE_HSTS_PRELOAD = True
    SECURE_HSTS_SECONDS = 31536000

    # CORS_ORIGIN_WHITELIST = ["https://"+x for x in fsconfig.SITE_DOMAIN]
    CSRF_TRUSTED_ORIGINS = fsconfig.SITE_DOMAIN

    # SECURE_SSL_REDIRECT = True and not DEBUG
    SECURE_CONTENT_TYPE_NOSNIFF = True
    CSRF_COOKIE_SECURE = True
    SESSION_COOKIE_SECURE = True
    X_FRAME_OPTIONS = 'DENY'

SASS_PRECISION = 13
SASS_OUTPUT_STYLE = 'compact'
SASS_PROCESSOR_AUTO_INCLUDE = True
SASS_PROCESSOR_ENABLED = True
SASS_PROCESSOR_INCLUDE_DIRS = []

SASS_PROCESSOR_INCLUDE_DIRS.append(os.path.join(BASE_DIR, 'static'))

# Application definition

SITE_ID = 1

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'ckeditor',
    'hamlpy',
    'method_override',
    'background_task',
    'sass_processor',
    'webproj.captcher',
    'webproj.bot',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'method_override.middleware.MethodOverrideMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'htmlmin.middleware.HtmlMinifyMiddleware',
    'htmlmin.middleware.MarkRequestMiddleware',
]

ROOT_URLCONF = 'webproj.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        # 'APP_DIRS': True,
        'OPTIONS': {
            'loaders': [
                'hamlpy.template.loaders.HamlPyFilesystemLoader',
                'hamlpy.template.loaders.HamlPyAppDirectoriesLoader',
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader'
            ],
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
                'django_settings_export.settings_export',
            ],
        },
    },
]

TEMPLATES[0]['DIRS'].append(os.path.join(BASE_DIR, 'webproj', 'templates'))

WSGI_APPLICATION = 'webproj.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': str(Path(BASE_DIR).joinpath('db.sqlite3')),
        'OPTIONS': {
            'timeout': 20,
        },
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.0/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',
    },
]


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

# LANGUAGES = [(iso, _(lang)) for iso, lang in fsconfig.LANGUAGES]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# if LANGUAGE_CODE not in LANGUAGES:
#     LANGUAGES.append(LANGUAGE_CODE)

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

HTML_MINIFY = not DEBUG

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'webproj', 'static_source'),
]

SASS_PROCESSOR_INCLUDE_DIRS.append(
    os.path.join(BASE_DIR, 'webproj', 'templates')
)

for STATICFILES_DIR in STATICFILES_DIRS:
    SASS_PROCESSOR_INCLUDE_DIRS.append(STATICFILES_DIR)
    del STATICFILES_DIR

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

if DEBUG and DEVELOPING:
    EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

DEFAULT_EXCEPTION_REPORTER_FILTER = 'django.views.debug.SafeExceptionReporterFilter'
DEFAULT_EXCEPTION_REPORTER = 'django.views.debug.ExceptionReporter'
ADMINS = [(f'Domain admin', 'admin@furmeet.app')]

DEFAULT_FROM_EMAIL = f'{SITE_NAME} <noreply@{ALLOWED_HOSTS[0]}>'

RANGE_1_UNTIL_13 = range(1, 13)

BOT_NAME = fsconfig.TELEGRAM_NAME
BOT_TOKEN = fsconfig.TELEGRAM_TOKEN

SETTINGS_EXPORT = [
    'SITE_NAME',
    'RANGE_1_UNTIL_13',
    'BOT_NAME',
    'DEBUG',
]

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'sass_processor.finders.CssFinder',
]

REST_FRAMEWORK = {
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    )
}

ROSETTA_WSGI_AUTO_RELOAD = True

DEFAULT_FROM_EMAIL = f'{SITE_NAME} <noreply@{ALLOWED_HOST_FOR_MAIL}>'

ACCOUNT_ACTIVATION_DAYS = 7
REGISTRATION_DEFAULT_FROM_EMAIL = f'{SITE_NAME} Registration <registration@{ALLOWED_HOST_FOR_MAIL}>'
REGISTRATION_EMAIL_HTML = True
REGISTRATION_AUTO_LOGIN = True
REGISTRATION_OPEN = fsconfig.REGISTRATION_OPEN

CORS_URLS_REGEX = r'^/api/.*$'
# CORS_ORIGIN_REGEX_WHITELIST = [r'^.*$']
CORS_ORIGIN_ALLOW_ALL = True

FIRST_DOMAIN = ALLOWED_HOST_FOR_MAIL
TELEGRAM_TOKEN = fsconfig.TELEGRAM_TOKEN

ENTER_P = 1  # default
ENTER_BR = 2
ENTER_DIV = 3

CKEDITOR_CONFIGS = {
    'default': {
        'toolbar': 'Custom',
        'toolbar_Custom': [
            ['Cut', 'Copy', 'Paste'],
            ['Undo', 'Redo'],
            ['Find', 'Replace', 'SelectAll'],
            '/',
            ['Bold', 'Italic', 'Underline', 'Strike'],
            ['Link', 'Unlink'],
            ['RemoveFormat'],
            ['Source'],
        ],
        'enterMode': ENTER_BR,
        'entities': False,
        'entities_latin': False,
        'entities_greek': False,
    },
}


exec('from server_secrets.extraconfig import *')
