# Copyright 2016-2022 The Wazo Authors  (see the AUTHORS file)
# SPDX-License-Identifier: GPL-3.0-or-later

import logging
import os

from datetime import timedelta

from cheroot import wsgi
from flask import Flask
from flask_restful import Api
from flask_cors import CORS
from werkzeug.contrib.fixers import ProxyFix
from xivo import http_helpers
from xivo.http_helpers import ReverseProxied

from .http import (
    LegacyErrorCatchingResource,
    LegacyAuthResource,
    ErrorCatchingResource,
    AuthResource,
    auth_verifier,
)

# Compatibility for old plugins < 22.03
__all__ = [
    'LegacyErrorCatchingResource',
    'LegacyAuthResource',
    'ErrorCatchingResource',
    'AuthResource',
]

logger = logging.getLogger(__name__)

VERSION = 0.1
TEMPLATE_FOLDER = 'plugins/templates'

app = Flask('wazo-dird')
api = Api(app, prefix='/{}'.format(VERSION))


class CoreRestApi:
    def __init__(self, global_config):
        self.config = global_config['rest_api']
        app.config['auth'] = global_config['auth']
        http_helpers.add_logger(app, logger)
        app.before_request(http_helpers.log_before_request)
        app.after_request(http_helpers.log_request)
        app.secret_key = os.urandom(24)
        app.permanent_session_lifetime = timedelta(minutes=5)
        app.config.update(global_config)
        auth_verifier.set_config(global_config['auth'])
        self.load_cors()
        self.server = None
        self.app = app
        self.api = api

    def load_cors(self):
        cors_config = dict(self.config.get('cors', {}))
        enabled = cors_config.pop('enabled', False)
        if enabled:
            CORS(app, **cors_config)

    def run(self):
        bind_addr = (self.config['listen'], self.config['port'])

        wsgi_app = ReverseProxied(ProxyFix(wsgi.WSGIPathInfoDispatcher({'/': app})))
        self.server = wsgi.WSGIServer(bind_addr=bind_addr, wsgi_app=wsgi_app)
        if self.config['certificate'] and self.config['private_key']:
            logger.warning(
                'Using service SSL configuration is deprecated. Please use NGINX instead.'
            )
            self.server.ssl_adapter = http_helpers.ssl_adapter(
                self.config['certificate'], self.config['private_key']
            )

        logger.debug(
            'WSGIServer starting... uid: %s, listen: %s:%s',
            os.getuid(),
            bind_addr[0],
            bind_addr[1],
        )
        for route in http_helpers.list_routes(app):
            logger.debug(route)

        try:
            self.server.start()
        except KeyboardInterrupt:
            self.server.stop()

    def stop(self):
        if self.server:
            self.server.stop()
