#!/usr/bin/python3
"""
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
__author__ = "n0251281"

import re
from datetime import datetime

import pandas as pd
from flask import Flask, redirect, render_template, request, session
# from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy
# from flask_wtf import FlaskForm
# from wtforms import (SelectField, SelectMultipleField, StringField,
#                      SubmitField, widgets)
# from wtforms.fields.html5 import DateField
# from wtforms.validators import DataRequired

from itsdangerous import URLSafeTimedSerializer
from recovery.blueprints.page import page
from recovery.blueprints.recover import recover
from flask_bootstrap import Bootstrap

import redis
from flask_session import Session
from recovery.lib.common.connecter import Connecter
# from recovery.lib.fconfig import DevConfig
# from recovery.lib.function.Outputter import Output
# from recovery.lib.function.pgfunctions import execute_batch
# from recovery.lib.function.Writer import csvwrite
# from recovery.lib.sql.Queries import sfquery

from recovery.extensions import (
    debug_toolbar,
    #mail,
    csrf,
    #db,
    #login_manager
)

def create_app(settings_override=None):
    """
    Create a Flask application using the app factory pattern.

    :param settings_override: Override settings
    :return: Flask app
    """
    
    app = Flask(__name__, instance_relative_config=True)

    app.config.from_object('config.settings')
    app.config.from_pyfile('settings.py', silent=True)

    app.config["SECRET_KEY"] = "dfjjflsfjlskjdk"
    app.config["SESSION_COOKIE_DOMAIN"] = "localhost.localdomain"
    app.config["SESSION_COOKIE_SECURE"] = False
    app.config["SESSION_PERMANENT"] = False
    app.config['SESSION_USE_SIGNER'] = True
    app.config["SESSION_TYPE"] = "redis"
    app.config['SESSION_REDIS'] = redis.from_url('redis://redis:6379')
    app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False

    app.register_blueprint(page)
    app.register_blueprint(recover)
    extensions(app)
    Bootstrap(app)
    Session(app)
    #authentication(app, User)
    connect = Connecter()
    #conn = connect.connect(sfparams)
    db = SQLAlchemy(app)

    #if settings_override:
    #    app.config.update(settings_override)


    return app

def extensions(app):
    """
    Register 0 or more extensions (mutates the app passed in).

    :param app: Flask application instance
    :return: None
    """
    debug_toolbar.init_app(app)
    #mail.init_app(app)
    #csrf.init_app(app)
    #db.init_app(app)
    #login_manager.init_app(app)

    return None

# def data_retrieve(queryvalue, tablename, columnlist, sessioninfo):
#     (sfdatabase, schemaname, conn) = sfconnect(sessioninfo)
#     cur = conn.cursor()
#     queryname = sfquery.generate_query(
#         queryvalue,
#         schemaname,
#         tablename,
#         session["startdate"],
#         session["enddate"],
#         # session["use_daterange"],
#         "1",
#         sfdatabase,
#         columnlist,
#     )
#     cur.execute(queryname)
#     rows = 0
#     cur.get_results_from_sfqid(cur.sfqid)
#     results = cur.fetchall()
#     return results

