#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Stashr - App Definitions
"""

"""
MIT License

Copyright (c) 2020 Andrew Vanderbye

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""

"""-------------------------------------------------------------------------------------------
-- IMPORTS
-------------------------------------------------------------------------------------------"""

""" --- HUEY IMPORT --- """
from huey import SqliteHuey
huey = SqliteHuey()

""" --- PYTHON IMPORTS --- """
import os

""" --- STASHR DEPENDENCY IMPORTS --- """
""" --- STASHR CORE IMPORTS --- """
from stashr import paths, log, comicvine, config, api
from stashr import tasks

""" --- FLASK IMPORT --- """
from flask import Flask

""" --- FLASK EXTENSION IMPORTS --- """
from itsdangerous import URLSafeTimedSerializer

from flask_login import LoginManager
# from flask_principal import Principal, Permission, RoleNeed
from flask_mail import Mail
from flask_pluginkit import PluginManager
from flask.signals import Namespace
from flask_cors import CORS

from flasgger import Swagger, APISpec
# from apispec.ext.marshmallow import MarshmallowPlugin

""" --- CONFIG IMPORTS --- """
from stashr.config import stashrconfig

"""-------------------------------------------------------------------------------------------
-- SETUP
-------------------------------------------------------------------------------------------"""

""" --- CREATE LOGGER --- """
logger = log.stashr_logger(__name__)

""" --- CREATE SIGNALS --- """
namespace = Namespace()
stashr_notification = namespace.signal('stashr_notification')
stashr_new_releases_update = namespace.signal('stashr_new_releases_update')

""" --- CREATE READING LIST --- """
# reading_image_list = []

""" --- GET CONFIGURATION --- """

"""-------------------------------------------------------------------------------------------
-- CONFIGURATION
-------------------------------------------------------------------------------------------"""


class FlaskConfig(object):

    # FLASK SETTINGS
    DEBUG = False
    SECRET_KEY = stashrconfig['SECURITY']['cookie_secret']
    CSRF_ENABLED = True

    JSON_SORT_KEYS = False

    # FLASK MAIL SETTINGS
    MAIL_SERVER = stashrconfig['MAIL']['mail_server']
    MAIL_PORT = stashrconfig['MAIL']['mail_port']
    MAIL_USE_SSL = stashrconfig['MAIL']['mail_use_ssl']
    MAIL_USE_TLS = False
    MAIL_USERNAME = stashrconfig['MAIL']['mail_username']
    MAIL_PASSWORD = stashrconfig['MAIL']['mail_password']
    MAIL_DEFAULT_SENDER = 'Stashr Admin'

    # FLASK PRINCIPAL
    # SKIP_STATIC = True


"""-------------------------------------------------------------------------------------------
-- CONFIGURATION
-------------------------------------------------------------------------------------------"""

""" --- CREATE FLASK APP --- """
app = Flask(__name__)

""" --- REGISTER API BLUEPRINT --- """
app.register_blueprint(api.api, url_prefix='/api')

""" --- CONFIGURE FLASK --- """
app.config.from_object(__name__+'.FlaskConfig')

template = {
  "swagger": "2.0",
  "info": {
    "title": "Stashr",
    "description": "API to access Stashr Installations",
    "version": "0.0.1"
  },
  "basePath": "/api",  # base bash for blueprint registration
  "schemes": [
    "http",
    "https"
  ],
  "operationId": "getmyData"
}

swagger = Swagger(app, template=template)

""" --- INITIALIZE FLASK EXTENSIONS --- """


# ITSDANGEROUS
ts = URLSafeTimedSerializer(app.config["SECRET_KEY"])
EMAIL_CONFIRM_KEY = stashrconfig['SECURITY']['cookie_secret']

# FLASK_LOGIN
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login_page"

# INITIALIZE FLASK PRINCIPAL
# principals = Principal(app, skip_static=True)
# permission_admin = Permission(RoleNeed('admin'))
# permission_reader = Permission(RoleNeed('reader'))

# FLASK MAIL
mail = Mail(app)

# FLASK CORS
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})

# PLUGIN MANAGER
plugin_folder = os.path.join(
    paths.base_path,
    stashrconfig['DIRECTORY']['plugins']
)

pm = PluginManager(app, plugins_base=paths.base_path, plugins_folder=stashrconfig['DIRECTORY']['plugins'])