"""Common settings for the Kasu homepage, it trys to load local_settings for
settings that differ between deployment and development, like passwords."""
from os import path


def gettext(string):
    """A dummy gettext to allow lazy i18n in the language selector."""
    return string


ADMINS = (('Max Mustermann', 'email@example.com'),)
ALLOWED_HOSTS = ['.kasu.at']
DEBUG = False
FORCE_SCRIPT_NAME = ""
INTERNAL_IPS = ('127.0.0.1',)
MANAGERS = ADMINS
ROOT_URLCONF = 'kasu.urls'
SECRET_KEY = 'set in local_settings'
SITE_ID = 1

# Path Info
PROJECT_PATH = path.abspath(path.join(path.dirname(__file__), '..', '..'))
LOCALE_PATHS = (path.join(PROJECT_PATH, 'kasu', 'locale/'),)
MEDIA_ROOT = path.join(PROJECT_PATH, 'media')
MEDIA_URL = '/media/'
STATIC_ROOT = path.join(PROJECT_PATH, 'static')
STATIC_URL = '/static/'
STATICFILES_DIRS = ()
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'compressor.finders.CompressorFinder',
)

# Apps
PREREQ_APPS = [
    'django.contrib.admin',
    'django.contrib.admindocs',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.humanize',
    'django.contrib.messages',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.sitemaps',
    'django.contrib.staticfiles',
    'django_comments',
    'captcha',
    'ckeditor',
    'ckeditor_uploader',
    'easy_thumbnails',
    'social_django',
]
PROJECT_APPS = [
    'kasu',
    'content',
    'events',
    'membership',
    'maistar_ranking',
    'mahjong_ranking',
]
INSTALLED_APPS = PROJECT_APPS + PREREQ_APPS

# Caches
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
    }
}

# Request Middleware
MIDDLEWARE_CLASSES = [
    'csp.middleware.CSPMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'utils.middleware.SetRemoteAddrFromForwardedFor',
    'mahjong_ranking.middleware.DenormalizationUpdateMiddleware',
]

# Template Settings
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [path.join(PROJECT_PATH, 'templates/')],
        'OPTIONS': {
            'context_processors': [
                'content.context_processors.content_menus',
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.csrf',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.request',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
                'django.contrib.messages.context_processors.messages',
                'events.context_processors.events_overview',
                'social_django.context_processors.backends',
                'social_django.context_processors.login_redirect'
            ],
            'loaders': [
                ('django.template.loaders.cached.Loader', [
                    'django.template.loaders.filesystem.Loader',
                    'django.template.loaders.app_directories.Loader',
                ]),
            ],
        },
    },
]

SESSION_COOKIE_DOMAIN = 'kasu.at'  # Die ganze Domain Kasu
SESSION_COOKIE_AGE = 15768000  # Session dauer: 4 Wochen
SESSION_COOKIE_SECURE = True

# E-Mail Settings (set in local_settings)
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = ""
EMAIL_PORT = ""
EMAIL_HOST_USER = ""
EMAIL_HOST_PASSWORD = ""
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = ""

# Login Settings
ACCOUNT_ACTIVATION_DAYS = 5
AUTH_USER_MODEL = 'membership.Membership'
AUTHENTICATION_BACKENDS = ('social_core.backends.facebook.FacebookOAuth2',
                           'social_core.backends.google.GoogleOAuth2',
                           'social_core.backends.twitter.TwitterOAuth',
                           'django.contrib.auth.backends.ModelBackend',)
LOGIN_URL = '/membership/login/'
LOGIN_ERROR_URL = '/membership/login/error/'
LOGIN_REDIRECT_URL = '/users/'
SOCIAL_AUTH_CHANGE_SIGNAL_ONLY = False
SOCIAL_AUTH_ENABLED_BACKENDS = ('facebook-oauth2', 'google-oauth2', 'twitter')
SOCIAL_AUTH_NEW_USER_REDIRECT_URL = '/users/'
SOCIAL_AUTH_SLUGIFY_USERNAMES = True
SOCIAL_AUTH_FACEBOOK_KEY = ''
SOCIAL_AUTH_FACEBOOK_SECRET = ''
SOCIAL_AUTH_FACEBOOK_SCOPE = ['user_about_me', 'email']
SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = ''
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = ''
SOCIAL_AUTH_TWITTER_KEY = ''
SOCIAL_AUTH_TWITTER_SECRET = ''

# Localization
USE_I18N = True
USE_L10N = True
LANGUAGE_CODE = 'de-at'
LANGUAGES = (('de', gettext('German')), ('en', gettext("English")))
TIME_ZONE = 'Europe/Vienna'
USE_TZ = True

# CK Editor
CKEDITOR_UPLOAD_PATH = "uploads/"
CKEDITOR_IMAGE_BACKEND = 'pillow'
CKEDITOR_JQUERY_URL = '/static/js/jquery.min.js'
CKEDITOR_CONFIGS = {
    'default': {
        'autoGrow_minHeight': 480,
        'autoGrow_maxHeight': 960,
        'autoGrow_onStartup': True,
        'contentsCss': '/static/css/kasu.css',
        'width': '100%',
    }
}

# credentials for facebook feed synchronisation (set in local_settings)
FACEBOOK_APP_ID = ''
FACEBOOK_ACCESS_TOKEN = ''

# crendetials for reCaptcha (set in local_settings)
NOCAPTCHA = True
RECAPTCHA_USE_SSL = True
RECAPTCHA_PUBLIC_KEY = ''
RECAPTCHA_PRIVATE_KEY = ''
RECAPTCHA_THEME = 'red'

# Settings for the Thumbnailer
THUMBNAIL_ALIASES = {'': {
    'avatar': {'size': (70, 70), 'crop': 'smart', 'replace_alpha': '#ffffff'},
    'article': {'size': (200, 120), 'crop': 'smart',
                'replace_alpha': '#ffffff'},
    'callout': {'size': (940, 300), 'crop': 'smart', 'upscale': True,
                'replace_alpha': '#ffffff'},
    'display': {'size': (940, 940), 'replace_alpha': '#ffffff'},
    'thumbnail': {'size': (140, 140), 'crop': 'smart',
                  'replace_alpha': '#ffffff'},
}}
THUMBNAIL_BASEDIR = 'thumbs'

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'filters': {
        'require_debug_false': {'()': 'django.utils.log.RequireDebugFalse'}
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level': 'INFO',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': True,
        },
        'django.request': {
            'handlers': ['console', 'mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'kasu': {
            'handlers': ['console', 'mail_admins'],
            'level': 'INFO',
            'propagate': False,
        },

    }
}

################################
# Settings for mahjong_ranking #
################################

MIN_HANCHANS_FOR_LADDER = 5
RANKING_EXPORT_PATH = path.join(PROJECT_PATH, 'backup', 'mahjong_ranking')

# Old Tournament System
TOURNAMENT_POINT_SYSTEM = True
TOURNAMENT_WIN_BONUSPOINTS = 4
TOURNAMENT_FLAWLESS_VICTORY_BONUSPOINTS = 8

# Old Dan System
DAN_3_WINS_IN_A_ROW = True
DAN_ALLOW_DROP_DOWN = True

DAN_RANKS = (
    (80, 9),
    (70, 8),
    (60, 7),
    (50, 6),
    (40, 5),
    (30, 4),
    (20, 3),
    (10, 2),
    (-1, 1),
)

KYU_RANKS = (
    (45, 1),
    (40, 2),
    (35, 3),
    (30, 4),
    (25, 5),
    (20, 6),
    (15, 7),
    (10, 8),
    (5, 9),
    (-1, 10),
)

try:
    from .local_settings import *  # Ignore PyLintBear (W0401, W0614)
except ImportError:
    pass

if DEBUG:
    TEMPLATES[0]['OPTIONS']['loaders'] = [
        'django.template.loaders.filesystem.Loader',
        'django.template.loaders.app_directories.Loader',
    ]
