"""
Django settings for djangobackend project.

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

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 datetime
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

# 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 = '!lz+vnnaph3%-yrtgso#m&a%-071t6k6ln=0r1u3c!s)b^v3t_'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
# if os.environ['DJANGOENV'] == 'prod':
#     DEBUG = False

ALLOWED_HOSTS = ['*']

BASE_CAMP = 'local'
try:
    if os.environ['DJANGOENV'] == 'prod':
        BASE_CAMP = 'prod'
    if os.environ['DJANGOENV'] == 'qa':
        BASE_CAMP = 'qa'
except Exception as e:
    print 'Running locally'

print BASE_CAMP

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'corsheaders',
    'rest_framework',
    # 'rest_framework_jwt',
    'rest_framework.authtoken',
    'djoser',
    'stockapi',
    'util',
    'rest_framework_tracking'
]

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',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware'
]

ROOT_URLCONF = 'djangobackend.urls'
CORS_ORIGIN_ALLOW_ALL = True


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 = 'djangobackend.wsgi.application'

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.BasicAuthentication',
    ),
}

# Database
# https://docs.djangoproject.com/en/1.11/ref/settings/#databases
#PRODUCTION
if BASE_CAMP == 'prod':
    print 'running PROD'
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'd9aigals9rapf5',
            'USER': 'lzxjlsyjaxpyyj',
            'PASSWORD': '19c915a470a3c43c8dde4e398d787ecb770a9bbd43ebffc5e18741accf178ad5',
            'HOST': 'ec2-184-72-255-211.compute-1.amazonaws.com',
            'PORT': '5432',
        }
    }
    DJOSER = {
        'ACTIVATION_URL': '#!/activateUser/?uid={uid}&token={token}',
        'SEND_ACTIVATION_EMAIL': True,
        'PASSWORD_VALIDATORS': [],
        'LOGOUT_ON_PASSWORD_CHANGE': True,
        'SERIALIZERS': {},
        'PASSWORD_RESET_CONFIRM_URL': '#/password-reset?uid={uid}&token={token}',
        'SITE_NAME': 'Infinv Investments',
        'DOMAIN': 'analysestock-prod.herokuapp.com'
    }

#QA
if BASE_CAMP == 'qa':
    print 'running QA'
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'd97kd12l7m174t',
            'USER': 'kkppagovrjzfwt',
            'PASSWORD': '705c84b75f16eb5deee3400c041f1063c0adff2ec46ac5e254de1416f781098c',
            'HOST': 'ec2-107-21-205-25.compute-1.amazonaws.com',
            'PORT': '5432',
        }
    }
    DJOSER = {
        'ACTIVATION_URL': '#!/activateUser/?uid={uid}&token={token}',
        'SEND_ACTIVATION_EMAIL': True,
        'PASSWORD_VALIDATORS': [],
        'LOGOUT_ON_PASSWORD_CHANGE': True,
        'SERIALIZERS': {},
        'PASSWORD_RESET_CONFIRM_URL': '#/password-reset?uid={uid}&token={token}',
        'SITE_NAME': 'Infinv Investments',
        'DOMAIN': 'analysestock-qa.herokuapp.com'
    }

#LOCAL
if BASE_CAMP == 'local':
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql',
            'NAME': 'infinv',
            'USER': 'postgres',
            'PASSWORD': 'root',
            'HOST': 'localhost',
            'PORT': '5432',
        }
    }
    DJOSER = {
        'ACTIVATION_URL': '#!/activateUser/?uid={uid}&token={token}',
        'SEND_ACTIVATION_EMAIL': True,
        'PASSWORD_VALIDATORS': [],
        'LOGOUT_ON_PASSWORD_CHANGE': True,
        'SERIALIZERS': {},
        'PASSWORD_RESET_CONFIRM_URL': '#/password-reset?uid={uid}&token={token}',
        'SITE_NAME': 'Infinv Investments',
        'DOMAIN': 'localhost:8000'
    }
    # removing validation checks
    # REST_FRAMEWORK = {}

# 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',
    },
]

EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_USE_TLS = True
EMAIL_HOST = 'smtp.sendgrid.net'
EMAIL_PORT = 25
EMAIL_HOST_USER = 'apikey'
EMAIL_HOST_PASSWORD = os.environ['SENDGRID_API_TOKEN']

# DJOSER = {
#     'ACTIVATION_URL': '#!/activateUser/?uid={uid}&token={token}',
#     'SEND_ACTIVATION_EMAIL': True,
#     'PASSWORD_VALIDATORS': [],
#     'LOGOUT_ON_PASSWORD_CHANGE': True,
#     'SERIALIZERS': {},
#     'PASSWORD_RESET_CONFIRM_URL': '#/password-reset?uid={uid}&token={token}',
#     'SITE_NAME': 'Infinv Investments',
#     'DOMAIN': 'analysestock-qa.herokuapp.com'
# }

JWT_AUTH = {
    'JWT_ENCODE_HANDLER':
    'rest_framework_jwt.utils.jwt_encode_handler',

    'JWT_DECODE_HANDLER':
    'rest_framework_jwt.utils.jwt_decode_handler',

    'JWT_PAYLOAD_HANDLER':
    'rest_framework_jwt.utils.jwt_payload_handler',

    'JWT_PAYLOAD_GET_USER_ID_HANDLER':
    'rest_framework_jwt.utils.jwt_get_user_id_from_payload_handler',

    'JWT_RESPONSE_PAYLOAD_HANDLER':
    'rest_framework_jwt.utils.jwt_response_payload_handler',

    'JWT_GET_USER_SECRET_KEY': None,
    'JWT_PUBLIC_KEY': None,
    'JWT_PRIVATE_KEY': None,
    'JWT_ALGORITHM': 'HS256',
    'JWT_VERIFY': True,
    'JWT_VERIFY_EXPIRATION': True,
    'JWT_LEEWAY': 0,
    'JWT_EXPIRATION_DELTA': datetime.timedelta(hours=3),
    'JWT_AUDIENCE': None,
    'JWT_ISSUER': None,

    'JWT_ALLOW_REFRESH': True,
    'JWT_REFRESH_EXPIRATION_DELTA': datetime.timedelta(days=7),

    'JWT_AUTH_HEADER_PREFIX': 'JWT',
    'JWT_AUTH_COOKIE': None,

}


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

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

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

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
