#!/usr/bin/python
# -*- coding: UTF-8 -*-

from MDmisc.database import Database
from PiAnoS import Database as DatabasePiAnoS
import os

from PIL import Image
import gnupg
import redis

import utils

baseurl = os.environ.get( "BASEURL", "" )
envtype = os.environ.get( "ENVTYPE", "" )

SECRET_KEY = os.environ.get( "SECRET_KEY", utils.rand.random_data( 20 ) )

MAX_CONTENT_LENGTH = 500 * 1024 * 1024

EMAIL_NB_ITERATIONS = 50 * 1000
PASSWORD_NB_ITERATIONS = 50 * 1000
DEK_NB_ITERATIONS = 500 * 1000
CF_NB_ITERATIONS = 100 * 1000

EMAIL_SALT_LENGTH = 20
PASSWORD_SALT_LENGTH = 20
DEK_SALT_LENGTH = 20
DEK_CHECK_SALT_LENGTH = 20

SESSION_TYPE = "redis"
SESSION_PERMANENT = False
SESSION_REFRESH_EACH_REQUEST = True

session_timeout = int( os.environ.get( "SESSION_TIMEOUT", 15 * 60 ) )
PERMANENT_SESSION_LIFETIME = session_timeout

redis_url = os.environ.get( "REDIS_URL", "redis://redis:6379/0" )
SESSION_REDIS = redis.from_url( redis_url )

TOTP_VALIDWINDOW = 5
TOTP_MAX_VALIDWINDOW = 1000

PROXY = os.environ.get( "BEHIND_PROXY", True )

try:
    pianosurl = os.environ.get( "DB_PIANOS_URL", "pgsql://pianos:pianos@pianos/pianos" )
    pianosdb = DatabasePiAnoS( pianosurl )
    pianosendpoint = os.environ.get( "PIANOS_ENDPOINT", "/pianos" )
except:
    pianosdb = None
    pianosendpoint = None

dburl = os.environ.get( "DB_URL", "pgsql://icnml:icnml@db:5432/icnml" )
db = Database( dburl )

redis_url2 = os.environ.get( "REDIS_URL_SHARED", "redis://redis:6379/1" )
redis_shared = redis.from_url( redis_url2 )

CACHE_TYPE = "redis"
CACHE_REDIS_URL = redis_url2

smtpserver = os.environ.get( "SMTP_SERVER", "smtpauth.unil.ch" )
smtpport = os.environ.get( "SMTP_PORT", 587 )
smtpuser = os.environ.get( "SMTP_USERNAME", "username" )
smtppassword = os.environ.get( "SMTP_PASSWORD", "password" )
sender = os.environ.get( "SMTP_SENDER", "icnml@unil.ch" )

POPPLER_PATH = os.environ.get( "POPPLER_PATH", "" )

if envtype.upper() != "DEV":
    SESSION_COOKIE_SECURE = True
    SESSION_COOKIE_SAMESITE = "Strict"
    domain = "https://icnml.unil.ch"
    RP_ID = "icnml.unil.ch"

else:
    domain = os.environ.get( "DOMAIN", "http://localhost" )
    RP_ID = os.environ.get( "RPID", "localhost" )

baseurl = os.environ.get( "BASEURL", "" )
fulldomain = domain + baseurl
cdn = fulldomain + "/cdn"

ORIGIN = domain
rp_name = "ICNML"

gpg_key = ( "FB15B70D1507B18B", )
keys_folder = os.environ.get( "KEYS_FOLDER", "/keys" )
gnupg._parsers.Verify.TRUST_LEVELS[ "ENCRYPTION_COMPLIANCE_MODE" ] = 23

Image.MAX_IMAGE_PIXELS = 1 * 1024 * 1024 * 1024

NIST_file_extensions = [
    ".nist", ".nst",
    ".an2", ".an2k",
    ".xml",
    ".lffs", ".lff",
    ".lfis", ".lfi",
    ".irq", ".irr", ".isr",
    ".srl",
    ".lsmq", ".lsmr",
    ".lpnq", ".lpn", ".lpnr",
    ".ulac", ".uld",
    ".ular", ".uldr", ".uuld",
    ".errl", ".erri", ".erra", ".err"
]

all_fpc = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 25, 27, 22, 24 ]
finger_fpc = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ]
palm_fpc = [ 25, 27, 22, 24 ]

cdnjs = [
    cdn + "/jquery/js/jquery-3.1.0.js",
    cdn + "/jquery/js/jquery-ui-1.12.1.min.js",
    cdn + "/toastr/toastr.min.js",
    cdn + "/misc/md5-min.js",
    cdn + "/sha512.js",
    cdn + "/moment.min.js",
    cdn + "/base64.js",
    cdn + "/otplib-browser.js",
    cdn + "/dropzone/dropzone.js",
    cdn + "/crypto-js/rollups/aes.js",
    cdn + "/crypto-js/rollups/pbkdf2.js",
    cdn + "/underscore-min.js",
    cdn + "/chosen/chosen.js",
    cdn + "/rangeslider/rangeslider.min.js"
]

cdncss = [
    cdn + "/jquery/css/base/jquery-ui.min.css",
    cdn + "/toastr/toastr.min.css",
    cdn + "/dropzone/dropzone.css",
    cdn + "/loadingcss/loading-btn.css",
    cdn + "/loadingcss/loading.css",
    cdn + "/chosen/chosen.css",
    cdn + "/rangeslider/rangeslider.min.css"
]

misc = {
    "jquery_ui_white_bg_icons": "/cdn/jquery/css/base/images/ui-icons_ffffff_256x240.png"
}

gpg_options = {
    "binary": os.environ.get( "GPG_BIN", "gpg" ),
    "homedir": os.environ.get( "GPG_HOMEDIR", "/tmp/gpg" )
}

gpg = gnupg.GPG( **gpg_options )

for key_file in os.listdir( keys_folder ):
    with open( keys_folder + "/" + key_file, "r" ) as fp:
        gpg.import_keys( fp.read() )

