"""
Django settings for etikiptype1 project.

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

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

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

import os
import sys

import django_heroku
from django.urls import reverse_lazy

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

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

# SECURITY WARNING: keep the secret key used in production secret!
#used for heroku local: (in .env file)
SECRET_KEY = os.getenv('SECRET_KEY', 'Optional default value')

db_user = os.getenv('DB_USER', 'Optional default value')
db_password = os.getenv('DB_PW', 'Optional default value')
db_host = os.getenv('DB_HOST', 'Optional default value')
db_port = os.getenv('DB_PORT', 'Optional default value')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
local_app = os.getenv('LOCAL_APP', False)

ALLOWED_HOSTS = []


# Application definition

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

    #third party apps
    'django_tables2',
    'bootstrap4',
    'bootstrap_datepicker_plus',
    'crispy_forms',

    # Local apps
    'etilog.apps.EtilogConfig',
    'etikicapture.apps.EtikicaptureConfig',
    'etikihead.apps.EtikiheadConfig',
    'impexport.apps.ImpexportConfig',
    'usermgmt.apps.UsermgmtConfig',

]

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

ROOT_URLCONF = 'etikiptype1.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')], #for custom templates
        '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 = 'etikiptype1.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'etiki-local', #'etikiprtype-1-prealpha', # 'etiki-local',
        'USER': db_user,
        'PASSWORD': db_password,
        'HOST': db_host,
        'PORT': db_port,
    }
}

# Password validation
# https://docs.djangoproject.com/en/2.2/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_URL = reverse_lazy('usermgmt:login')

LOGIN_REDIRECT_URL = 'etilog:home'


def get_caches():
    if local_app:
        return {

            'memcached': {
                'BACKEND': 'django_bmemcached.memcached.BMemcached',
                'TIMEOUT': 60 * 60,
                'LOCATION': os.environ['MEMCACHEDCLOUD_SERVERS'].split(','),
                'OPTIONS': {
                    'username': os.environ['MEMCACHEDCLOUD_USERNAME'],
                    'password': os.environ['MEMCACHEDCLOUD_PASSWORD'],
                }
            },
            'default': {
                'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
            },
            'database': {
                'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
                'LOCATION': 'cache_table',
            }
        }
    else:
        return {
            'default': {
                'BACKEND': 'django_bmemcached.memcached.BMemcached',
                'TIMEOUT': 60 * 60,
                'LOCATION': os.environ['MEMCACHEDCLOUD_SERVERS'].split(','),
                'OPTIONS': {
                    'username': os.environ['MEMCACHEDCLOUD_USERNAME'],
                    'password': os.environ['MEMCACHEDCLOUD_PASSWORD'],
                }
            },
        }


CACHES = get_caches()


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

LANGUAGE_CODE = 'de-ch'

TIME_ZONE = 'Europe/Zurich'

USE_I18N = True

USE_L10N = True

USE_TZ = True


DJANGO_TABLES2_TEMPLATE = "django_tables2/bootstrap4.html" #'django_tables2/bootstrap-responsive.html'
CRISPY_TEMPLATE_PACK = 'bootstrap4'

BOOTSTRAP4 = {
    #already includes jquery:
    'include_jquery': 'full',
    "css_url": {
        "href": "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css"
        },
    "javascript_url": {
        "url": "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.min.js"
        },
    "jquery_url": {
        "url": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"
        },
    #default:
    #"css_url": {
    #    "href": "https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css",
    #    "integrity": "sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB",
    #    "crossorigin": "anonymous",
    #},
}


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

STATIC_URL = '/static/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')  #/Users/…/etikiptype1/media
MEDIA_URL = '/media/'
#needed for debug = False -> test if needs to under heroku setting
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.ManifestStaticFilesStorage'

base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
spath_sys = os.path.join(base_dir, 'node_modules/ReadabiliPy')
sys.path.append(spath_sys)

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = os.getenv('EMAIL_HOST', 'email_host')
EMAIL_PORT = os.getenv('EMAIL_PORT', 'email_port')
EMAIL_HOST_USER = os.getenv('EMAIL_HOST_USER', 'email_host_user')
EMAIL_HOST_PASSWORD = os.getenv('EMAIL_HOST_PASSWORD', 'email_host_pw')
EMAIL_USE_TLS = True
EMAIL_USE_SSL = False
# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

#defines static root, logger, etc.
django_heroku.settings(locals())
