# -*- coding: utf-8 -*-
# Copyright (C) 2018 Freetech Solutions

# This file is part of OMniLeads

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3, as published by
# the Free Software Foundation.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.

# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see http://www.gnu.org/licenses/.
#

# Módulo para configurar settings de addons

import os
from ominicontacto.settings.defaults import BASE_DIR
from django.utils.timezone import datetime, timedelta
from datetime import timezone

# TODO: Ver si es posible Moverlo a defaults.py

CONSTANCE_ADDITIONAL_FIELDS = {
    'disabled_str': ['django.forms.fields.CharField', {
        'widget': 'django.forms.PasswordInput',
        'disabled': True,
        'required': False,
    }],
    'char_field': ['django.forms.fields.CharField', {
        'disabled': False,
        'required': True,
    }],
}

CONSTANCE_CONFIG = {
    # Key Server data:
    'KEYS_SERVER_HOST': ('https://keys-server.freetech.com.ar', 'KEYS_SERVER_HOST', str),
    'SSL_CERT_FILE': ('/opt/omnileads/cert', 'SSL_CERT_FILE', str),
    'CLIENT_NAME': ('', 'CLIENT_NAME', str),
    'CLIENT_KEY': ('', 'CLIENT_KEY', 'disabled_str'),
    'CLIENT_PASSWORD': ('', 'CLIENT_PASSWORD', str),
    'CLIENT_EMAIL': ('', 'CLIENT_EMAIL', str),
    'CLIENT_PHONE': ('', 'CLIENT_PHONE', str),

    # OMniLeads:
    'WOMBAT_DIALER_ALLOW_REFRESH': (False, 'WOMBAT_DIALER_ALLOW_REFRESH', bool),
    'WOMBAT_DIALER_STATE': ('READY', 'WOMBAT_DIALER_STATE', str),
    'WOMBAT_DIALER_UPDATE_DATETIME': (datetime(2022, 2, 2, 22, 2, 22, 2222, timezone(timedelta(0))),
                                      'WOMBAT_DIALER_UPDATE_DATETIME', datetime),
    'EXTERNAL_AUTH_TYPE': ('0', 'EXTERNAL_AUTH_TYPE', str),
    'EXTERNAL_AUTH_SERVER': ('', 'EXTERNAL_AUTH_SERVER', str),
    'EXTERNAL_AUTH_DN': ('', 'EXTERNAL_AUTH_DN', str),
    'EXTERNAL_AUTH_ACTIVATION': ('', 'EXTERNAL_AUTH_ACTIVATION', str),
    'EXTERNAL_AUTH_MS_SIMPLE_AUTH': (False, 'EXTERNAL_AUTH_MS_SIMPLE_AUTH', bool),

    # Addons configs
    'WEBPHONE_CLIENT_ENABLED': (False, 'WEBPHONE_CLIENT_ENABLED', bool),
    'WEBPHONE_CLIENT_TTL': (1200, 'WEBPHONE_CLIENT_TTL', int),
    'WEBPHONE_VIDEO_DOMAIN': ('meet.jit.si', 'WEBPHONE_VIDEO_DOMAIN', str),
    'LIMIT_USERS_ACTIVE': (False, 'LIMIT_USERS_ACTIVE', bool),
    'LIMIT_USERS_TO': (2, 'LIMIT_USERS_TO', int),
    'LIMIT_AGENTS_CONCURRENT_ACTIVE': (False, 'LIMIT_AGENTS_CONCURRENT_ACTIVE', bool),
    'LIMIT_AGENTS_CONCURRENT_NUMBER': (1, 'LIMIT_AGENTS_CONCURRENT_NUMBER', int),

    # white-label
    "ASTERISK_TM": ("Asterisk", "", "char_field"),
    "OMNILEADS_TM": ("OMniLeads", "", "char_field"),
    "SUGGEST_REGISTER": (True, "", bool),
    "WOMBAT_TM": ("Wombat", "", "char_field"),
    "PRIMARY_COLOR": ("#F71883", "", "char_field"),
    "PRIMARY_LIGHT_COLOR": ("#FA4DA0", "", "char_field"),
    "SECONDARY_COLOR": ("#89468B", "", "char_field"),
    "FAVICON": ("ominicontacto/Img/favicon.png", "", "char_field"),
    "IC_LOGO": ("ominicontacto/Img/ic_logo.png", "", "char_field"),
    "IC_LOGO_FULL": ("ominicontacto/Img/ic_logo_full.png", "", "char_field"),
    "IC_LOGO_SYMBOL": ("ominicontacto/Img/ic_logo_symbol.png", "", "char_field"),

}

CONSTANCE_CONFIG_FIELDSETS = {
    'Registration': {
        'fields': ('KEYS_SERVER_HOST', 'SSL_CERT_FILE', 'CLIENT_NAME',
                   'CLIENT_KEY', 'CLIENT_PASSWORD', 'CLIENT_EMAIL',
                   'CLIENT_PHONE',
                   ),
        'collapse': False,
    },
    'Addons Options': {
        'fields': ('WEBPHONE_CLIENT_ENABLED', 'WEBPHONE_CLIENT_TTL', 'WEBPHONE_VIDEO_DOMAIN',
                   'LIMIT_USERS_ACTIVE', 'LIMIT_USERS_TO',
                   'LIMIT_AGENTS_CONCURRENT_ACTIVE', 'LIMIT_AGENTS_CONCURRENT_NUMBER',
                   ),
        'collapse': False
    },
    'Omnileads': {
        'fields': ('WOMBAT_DIALER_ALLOW_REFRESH', 'WOMBAT_DIALER_STATE',
                   'WOMBAT_DIALER_UPDATE_DATETIME',
                   'EXTERNAL_AUTH_TYPE', 'EXTERNAL_AUTH_SERVER', 'EXTERNAL_AUTH_DN',
                   'EXTERNAL_AUTH_ACTIVATION', 'EXTERNAL_AUTH_MS_SIMPLE_AUTH',
                   ),
        'collapse': True
    },
}

ADDONS_APPS = []
ADDONS_LOCALE_PATHS = ()
MIDDLEWARE_PREPPEND = []
MIDDLEWARE_APPEND = []
TEMPLATES_CONTEXT_PROCESORS_APPEND = []

ADDON_URLPATTERNS = []

if os.getenv('ENTERPRISE_VERSION'):
    ADDONS_APPS.append("enterprise_app.apps.AppConfig")
    ADDON_URLPATTERNS.append((r"enterprise", "enterprise_app.urls"))
    ADDONS_LOCALE_PATHS += (os.path.join(BASE_DIR, 'enterprise_app/locale'), )
if not os.getenv('LIMIT_USERS_VERSION', '') == '':
    ADDONS_APPS.append('limit_users_app.apps.LimitUsersAppConfig')
    MIDDLEWARE_APPEND.append('limit_users_app.middleware.limits.LimitUsersMiddleware')
if not os.getenv('PREMIUM_REPORTS_VERSION', '') == '':
    ADDONS_APPS.append('premium_reports_app.apps.PremiumReportsAppConfig')
    ADDON_URLPATTERNS.append((r'^', 'premium_reports_app.urls'))
    ADDON_URLPATTERNS.append((r"premium-reports", "premium_reports_app.api.urls"))
    ADDONS_LOCALE_PATHS += (os.path.join(BASE_DIR, 'premium_reports_app/locale'), )
if not os.getenv('SURVEY_VERSION', '') == '':
    ADDONS_APPS.append('survey_app.apps.SurveyAppConfig')
    ADDON_URLPATTERNS.append((r'^', 'survey_app.urls'))
    ADDONS_LOCALE_PATHS += (os.path.join(BASE_DIR, 'survey_app/locale'), )
if not os.getenv('WALLBOARD_VERSION', '') == '':
    ADDONS_APPS.append('wallboard_app.apps.WallboardAppConfig')
    ADDON_URLPATTERNS.append((r'^', 'wallboard_app.urls'))
    ADDONS_LOCALE_PATHS += (os.path.join(BASE_DIR, 'wallboard_app/locale'), )
if not os.getenv('WEBPHONE_CLIENT_VERSION', '') == '':
    ADDONS_APPS.append('webphone_client_app.apps.WebphoneClientAppConfig')
    ADDON_URLPATTERNS.append((r'^', 'webphone_client_app.urls'))
    ADDONS_LOCALE_PATHS += (os.path.join(BASE_DIR, 'webphone_client_app/locale'), )
if not os.getenv('BULK_MESSAGES_VERSION', '') == '':
    ADDONS_APPS.append('bulk_messages_app.apps.BulkMessagesAppConfig')
    ADDON_URLPATTERNS.append((r'^', 'bulk_messages_app.urls'))
    ADDONS_LOCALE_PATHS += (os.path.join(BASE_DIR, 'bulk_messages_app/locale'), )

#########################################
# django-cors-headers Custom config
""" Se deja acá porque modifica INSTALLED_APPS a traves de ADDONS_APPS """
CUSTOM_CORS_ALLOWED_ORIGINS = os.getenv('CUSTOM_CORS_ALLOWED_ORIGINS', '')
if not CUSTOM_CORS_ALLOWED_ORIGINS == '':
    ADDONS_APPS.append('corsheaders')
    MIDDLEWARE_PREPPEND.insert(0, 'corsheaders.middleware.CorsMiddleware')
    CORS_ALLOWED_ORIGINS = CUSTOM_CORS_ALLOWED_ORIGINS.split(',')

    CUSTOM_CORS_ALLOW_HEADERS = os.getenv('CUSTOM_CORS_ALLOW_HEADERS', '')
    if not CUSTOM_CORS_ALLOW_HEADERS == '':
        CUSTOM_CORS_ALLOW_HEADERS = CUSTOM_CORS_ALLOW_HEADERS.split(',')
        from corsheaders.defaults import default_headers
        CORS_ALLOW_HEADERS = (
            *default_headers,
            *CUSTOM_CORS_ALLOW_HEADERS,
        )
