# -*- coding: utf-8 -*-
import os
from unipath import Path

import djcelery
djcelery.setup_loader()

# DJANGO ADMIN
ADMINS = (
    # ('Your Name', 'your_email@domain.com'),
)

MANAGERS = ADMINS

# Hosts/domain names that are valid for this site; required if DEBUG is False
# See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
ALLOWED_HOSTS = ['*']

# Hosts/domain names that can export books from the /<bookid>/_export/ path
EXPORT_ALLOWED_HOSTS = ['127.0.0.1']

DEFAULT_NOTIFICATION_FILTER = u"#* !* ~* \u212c*"

# You can customize the default publishing options this instance can use.
# Options are: mpdf, screenpdf, epub3, epub2, mobi, xhtml
# PUBLISH_OPTIONS = ("mpdf", "screenpdf", "epub3", "epub2")

# BOOKTYPE
PROFILE_ACTIVE = ''

BOOKTYPE_SITE_NAME = None
# This is how you can define name of your site
# BOOKTYPE_SITE_NAME = '##BOOKTYPE_SITE_NAME##'
BOOKTYPE_SITE_DIR = '##BOOKTYPE_SITE_DIR##'

# The hostname of the Booktype server (e.g. www.myserver.org, 192.168.1.10, booktype.myserver.org)
THIS_BOOKTYPE_SERVER = ''

# BOOKTYPE_ROOT = '##BOOKTYPE_ROOT##'
BOOKTYPE_ROOT = Path(os.path.abspath(__file__)).ancestor(3)

BOOKTYPE_URL = ''
# BOOKTYPE_URL = 'http://{}'.format(THIS_BOOKTYPE_SERVER)

# E-MAIL OPTIONS
DEFAULT_FROM_EMAIL = 'booktype@booktype.pro'
REPORT_EMAIL_USER = 'report@booktype.pro'

EMAIL_HOST = 'localhost'
EMAIL_PORT = 25
# EMAIL_HOST_USER = 'booktype@' + THIS_BOOKTYPE_SERVER
# EMAIL_HOST_PASSWORD = ''
# EMAIL_USE_TLS = False

# MOBI EXPORT
MOBI_CONVERT = "calibre"

# MPDF RENDERER SETTINGS

MPDF_DIR = ''
PHP_PATH = 'php'
MPDF_SCRIPT = '{}/booktype2mpdf.php'.format(BOOKTYPE_ROOT)

# PANDOC
# /usr/local/bin/pandoc - OSX
# /usr/bin/pandoc - linux
PANDOC_PATH = '/usr/local/bin/pandoc'
PANDOC_ICML_SCRIPT = '{}/epub2icml.sh'.format(BOOKTYPE_ROOT)
PANDOC_DOCX_SCRIPT = '{}/epub2docx.sh'.format(BOOKTYPE_ROOT)

# BOOKTYPE DIRECTORIES

# site_static
import booki

# static
STATIC_ROOT = BOOKTYPE_ROOT.child('static')
STATIC_URL = '{}/static/'.format(BOOKTYPE_URL)

# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    BOOKTYPE_ROOT.child(BOOKTYPE_SITE_DIR).child('static'),
)

# List of finder classes that know how to find static files in
# various locations.
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'booktype.apps.themes.finder.ThemeFinder',
    # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
    'compressor.finders.CompressorFinder'
)

# 3RD PARTY STATIC CACHE SYSTEMS LIKE CLOUDFLARE
EXTERNAL_STATIC_CACHE = False
EXTERNAL_STATIC_CACHE_KEY = 'v1'

# data
DATA_ROOT = BOOKTYPE_ROOT.child('data')
DATA_URL = '{}/data/'.format(BOOKTYPE_URL)

# profile images
PROFILE_IMAGE_UPLOAD_DIR = 'profile_images/'

# If you don't want to use default profile image you can set your own.
# Place the image inside of /static/images/ directory in your Booktype project directory.
# DEFAULT_PROFILE_IMAGE='anonymous.png'

# book cover images
COVER_IMAGE_UPLOAD_DIR = 'cover_images/'

# group images
GROUP_IMAGE_UPLOAD_DIR = 'group_images/'


# obsolete
MEDIA_ROOT = DATA_ROOT
MEDIA_URL = DATA_URL

ADMIN_MEDIA_PREFIX = '{}/media/'.format(BOOKTYPE_URL)

# who gets credited as publisher if not otherwise specified
DEFAULT_PUBLISHER = "Unknown"


# REDIS STUFF
REDIS_HOST = 'localhost'
REDIS_PORT = 6379
REDIS_DB = 0
REDIS_PASSWORD = None

# DJANGO STUFF
AUTH_PROFILE_MODULE = 'account.UserProfile'

TIME_ZONE = 'Europe/Berlin'

LANGUAGE_CODE = 'en-us'

gettext = lambda s: s

LANGUAGES = (
    ('en', gettext('American English')),
    ('en-gb', gettext('British English')),
    ('ca', gettext('Català')),
    ('cs', gettext('Česky')),
    ('de', gettext('Deutsch')),
    ('de-at', gettext('Österreichisches Deutsch')),
    ('el', gettext('Ελληνικά')),
    ('es', gettext('Español')),
    ('fr', gettext('Français')),
    ('it', gettext('Italiano')),
    ('ja', gettext('日本語')),
    ('ko-kr', gettext('한국어')),
    ('hu', gettext('Magyar')),
    ('nl', gettext('Nederlands')),
    ('nb', gettext('Norsk (Bokmål)')),
    ('pl', gettext('Polski')),
    ('pt', gettext('Português')),
    ('pt-br', gettext('Português do Brasil')),
    ('ru', gettext('Русский')),
    ('sq', gettext('Shqipe')),
    ('fi', gettext('Suomi')),
    ('tr', gettext('Türkçe')),
    ('uk-ua', gettext('українська мова')),
)

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# If you set this to False, Django will not format dates, numbers and
# calendars according to the current locale.
USE_L10N = True

# If you set this to False, Django will not use timezone-aware datetimes.
USE_TZ = False

LOCALE_PATHS = (
    BOOKTYPE_ROOT.child(BOOKTYPE_SITE_DIR).child('locale'),
    Path(booki.__file__).parent.child('locale'),
    Path(booki.__file__).ancestor(2).child('booktype').child('locale'),
    Path(booki.__file__).ancestor(2).child('booktypecontrol').child('locale'),
)

# Make this unique, and don't share it with anybody.
SECRET_KEY = '##SECRET_KEY##'

# Storage for messages framework
MESSAGE_STORAGE = 'django.contrib.messages.storage.session.SessionStorage'

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'booktype.api.middleware.APILoggingMiddleware',
    'booktype.apps.core.middleware.StrictAuthentication',
    'booktype.apps.core.middleware.SecurityMiddleware',
)

# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = '{}.wsgi_dev.application'.format('##BOOKTYPE_SITE_DIR##')

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BOOKTYPE_ROOT.child(BOOKTYPE_SITE_DIR).child('templates')
        ],
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.template.context_processors.request',
                'django.contrib.messages.context_processors.messages',
                'django.template.context_processors.csrf',
                'booktype.apps.core.context_processors.settings_variables'
            ],
            'loaders': [
                'django.template.loaders.filesystem.Loader',
                'django.template.loaders.app_directories.Loader',
                'django.template.loaders.eggs.Loader',
                'booktype.apps.themes.loaders.Loader'
            ],
            'debug': False
        },
    },
]


TEMPLATE_VISIBLE_SETTINGS = (
    'BOOKTYPE_URL',
    'REST_FRAMEWORK_DEFAULT_VERSION',
    'STATIC_ROOT',
    'STATIC_URL',
    'DATA_ROOT',
    'DATA_URL',
    'MEDIA_ROOT',
    'MEDIA_URL',
    'EXTERNAL_STATIC_CACHE',
    'EXTERNAL_STATIC_CACHE_KEY'
)

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

    'compressor',
    'djcelery',
    'rest_framework',
    'rest_framework.authtoken',
    'rest_framework_swagger',

    # list of booki apps
    'booki.editor',

    # needed for translation engine
    'booktype',

    # list of booktype apps
    'booktype.api',
    'booktype.apps.core',
    'booktype.apps.portal',
    'booktype.apps.loadsave',
    'booktype.apps.importer',
    'booktype.apps.convert',
    'booktype.apps.edit',
    'booktype.apps.reader',
    'booktype.apps.account',
    'booktype.apps.themes',
    'booktype.apps.export',
    'booktype.apps.reports',

    # to be removed
    'booki.messaging',

    'sputnik',
    'booktypecontrol'
)

BOOKTYPE_CONVERTER_MODULES = (
    'booktype.convert.converters',
)

REST_FRAMEWORK_DEFAULT_VERSION = 'v1'

REST_FRAMEWORK = {
    'DEFAULT_AUTHENTICATION_CLASSES': (
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),

    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.IsAuthenticated',
        'booktype.api.security.BooktypeSecurity',
        'booktype.api.security.WhitelistIPPermission'
    ),

    'DEFAULT_FILTER_BACKENDS': (
        'django_filters.rest_framework.DjangoFilterBackend',
    ),

    'DEFAULT_VERSION': REST_FRAMEWORK_DEFAULT_VERSION,
    'DEFAULT_VERSIONING_CLASS': 'rest_framework.versioning.NamespaceVersioning',
    'DEFAULT_METADATA_CLASS': 'rest_framework.metadata.SimpleMetadata',
    'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
    'PAGE_SIZE': 100
}


BROKER_URL = 'amqp://guest:guest@localhost:5672/'
CELERY_ACCEPT_CONTENT = ['pickle', 'json', 'msgpack', 'yaml']

# set of default roles and corresponding permissions
BOOKTYPE_DEFAULT_ROLES = {
    'anonymous_users': [
        'reader.can_view_full_page',
        'reader.can_view_draft'
    ],
    'registered_users': [
        'reader.can_view_full_page',
        'reader.can_view_draft',
        'account.can_upload_book',
    ]
}

# Configuration for the invite functionality
BOOKTYPE_INVITE_SUBJECT = gettext('You\'ve been invited to collaborate')
BOOKTYPE_DEFAULT_INVITE_MESSAGE = gettext('You\'ve been invited to collaborate')

# Whitelist of allowed ips to access api resources
# booktype.api.security.WhitelistIPPermission is required in rest_framework conf
BOOKTYPE_API_ALLOWED_IPS = ['*']

# DEPRECATED CONFIG

BOOKTYPE_NAME = BOOKTYPE_SITE_NAME
BOOKI_NAME = BOOKTYPE_NAME
BOOKI_ROOT = BOOKTYPE_ROOT
BOOKI_URL = BOOKTYPE_URL
THIS_BOOKI_SERVER = THIS_BOOKTYPE_SERVER
BOOKI_MAINTENANCE_MODE = False


FILE_UPLOAD_HANDLERS = (
    'django.core.files.uploadhandler.MemoryFileUploadHandler',
    'django.core.files.uploadhandler.TemporaryFileUploadHandler'
)

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
        'LOCATION': 'unique-booktype-id'
    }
}

# LOGGING
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': "[%(asctime)s] %(levelname)s [%(name)s:%(lineno)s] %(message)s",  # noqa
            'datefmt': "%d/%b/%Y %H:%M:%S"
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
        },
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'formatter': 'standard'
        },
        'logfile': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': BOOKTYPE_ROOT + "/logs/booktype.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'sputniklogfile': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': BOOKTYPE_ROOT + "/logs/sputnik.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        },
        'apilogfile': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': BOOKTYPE_ROOT + "/logs/api.log",
            'maxBytes': 50000,
            'backupCount': 2,
            'formatter': 'standard',
        }
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
            'propagate': True,
            'level': 'WARN',
        },
        'django.db.backends': {
            'handlers': ['console'],
            'level': 'DEBUG',
            'propagate': False,
        },
        'django.request': {
            'handlers': ['console'],
            'level': 'ERROR',
            'propagate': True,
        },
        'booktype': {
            'handlers': ['console'],
            'level': 'DEBUG'
        },
        'sputnik': {
            'handlers': ['console'],
            'level': 'DEBUG'
        },
        'api': {
            'handlers': ['console'],
            'level': 'DEBUG'
        }
    }
}

# Setting to avoid warning message of new Django Test Runner
TEST_RUNNER = 'django.test.runner.DiscoverRunner'

# This is the same setting that will be available in Django 1.10
# Uncomment this line below if you want to redirect somewhere else after logout
# LOGOUT_REDIRECT_URL = '/'

# this is to create additional metadata fields for books
# we can use any of the fields of Django Forms module.
# I can be something like this:

# ADDITIONAL_METADATA = {
#     'field_name_one': {
#         'TYPE': 'ChoiceField',
#         'ATTRS': {
#             'choices': [
#                 ('option1', gettext('Trans String 1')),
#                 ('option2', gettext('Trans String 2'))
#             ]
#         }
#     },
#     'field_name_two': {
#         'TYPE': 'CharField',
#         'WIDGET': 'Textarea',
#         'WIDGET_ATTRS': {'class': 'css-class'}
#     }
# }

# This constant will be loaded into publish screen and passed to
# converter settings (pdfreactor only for now)
# Uncomment this (and adjust) if you want to enable icc profile selection
# ICC_PROFILES_CHOICES = (
#     ('https://example.com/myicc-profile.icc', 'My ICC Profile'),
#     ('https://example.com/myicc-profile-2.icc', 'Second Profile'),
# )

_ADDITIONAL_METADATA = [
    ('meta_subject', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Subject'
            },
        'WIDGET': 'TextInput'
    }),
    ('meta_keywords', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Keywords'
            },
        'WIDGET': 'TextInput'
    }),
    ('edited_by', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Edited by'
            },
        'WIDGET': 'TextInput'
    }),
    ('text_by', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Text by'},
        'WIDGET': 'TextInput'
    }),
    ('translation_by', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Translation by'},
        'WIDGET': 'TextInput'
    }),
    ('introduction_by', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Introduction by'},
        'WIDGET': 'TextInput'
    }),
    ('cover_design', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Cover design'},
        'WIDGET': 'TextInput'
    }),
    ('cover_photography', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Cover image'},
        'WIDGET': 'TextInput'
    }),
    ('photography', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Photography by'},
        'WIDGET': 'TextInput'
    }),
    ('illustration_by', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Illustration by'},
        'WIDGET': 'TextInput'
    }),
    ('research', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Research'},
        'WIDGET': 'TextInput'
    }),
    ('lectorate', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Lectorate'},
        'WIDGET': 'TextInput'
    }),
    ('proofreading', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Proofreading'},
        'WIDGET': 'TextInput'
    }),
    ('rights_clearing', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Rights clearing'},
        'WIDGET': 'TextInput'
    }),
    ('typeface', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Typeface'},
        'WIDGET': 'TextInput'
    }),
    ('printed_on', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Printed on'},
        'WIDGET': 'TextInput'
    }),
    ('paper_certification', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Paper certification'},
        'WIDGET': 'TextInput'
    }),
    ('bookbinder', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Bookbinder'},
        'WIDGET': 'TextInput'
    }),
    ('printer', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Printer company'},
        'WIDGET': 'TextInput'
    }),
    ('printer_country', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Printer country'},
        'WIDGET': 'TextInput'
    }),
    ('acknowledgement', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Acknowledgement'},
        'WIDGET': 'Textarea'}
    ),
    ('edition', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Edition'},
        'WIDGET': 'TextInput'
    }),
    ('url_publisher', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'URL Publisher'},
        'WIDGET': 'TextInput'
    }),
    ('url_author', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'URL Author'},
        'WIDGET': 'TextInput'
    }),
    ('bibliographic_information', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Bibliographic information'},
        'WIDGET': 'TextInput'
    }),
    ('dedication', {
        'TYPE': 'CharField',
        'ATTRS': {
            'required': False,
            'label': 'Dedication'},
        'WIDGET': 'Textarea'
    })
]

import collections

ADDITIONAL_METADATA = collections.OrderedDict(_ADDITIONAL_METADATA)
