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

"""
Stashr - [description]
"""

"""
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
-------------------------------------------------------------------------------------------"""

""" --- PYTHON IMPORTS --- """
import os, io, shutil, uuid, base64

from configobj import ConfigObj
from validate import Validator

""" --- STASHR DEPENDENCY IMPORTS --- """
""" --- STASHR CORE IMPORTS --- """
# from stashr import log

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

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


class StashrConfig(ConfigObj):

    configspec = u"""
[APP]
server_port = integer(default=5002)
log_level = option('CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG', default='INFO')
open_registration = boolean(default=False)
page_length = integer(default=30)
first_run = boolean(default=True)
comicvine_api_key = string(default='')
[DIRECTORY]
temp = string(default='temp')
comics = string(default='comics')
log = string(default='log')
backup = string(default='backup')
plugins = string(default='plugins')
images = string(default='images')
[API]
comicvine_api_key = string(default='')
[SECURITY]
cookie_secret = string(default='')
[MAIL]
mail_username = string(default="")
mail_password = string(default="")
mail_default_sender = string(default="")
mail_server = string(default="")
mail_port = integer(default=0)
mail_use_ssl = boolean(default=False)
[NAMING]
file = string(default="{volume_name} - {issue_number:0>3}")
folder = string(default="{volume_name} ({volume_year}) [{volume_id}]")
        """

    def __init__(self):
        super(StashrConfig, self).__init__()

        if not os.path.exists('configspec.ini'):
            with open('configspec.ini', 'w') as fd:
                shutil.copyfileobj(io.StringIO(StashrConfig.configspec), fd)

        self.filename = os.path.join('config.ini')
        self.configspec = os.path.join('configspec.ini')
        self.encoding = "UTF8"

        tmp = ConfigObj(self.filename, configspec=self.configspec, encoding=self.encoding)
        validator = Validator()
        tmp.validate(validator, copy=True)

        if tmp['SECURITY']['cookie_secret'] == '':
            tmp['SECURITY']['cookie_secret'] = base64.b64encode(uuid.uuid4().bytes + uuid.uuid4().bytes);

        self.merge(tmp)

        if not os.path.exists(self.filename):
            self.write()

"""-------------------------------------------------------------------------------------------
-- CONFIGURATION DEFINITION
-------------------------------------------------------------------------------------------"""

stashrconfig = StashrConfig()
