import flask
from flask import Flask, Blueprint,send_file
from flask_sslify import SSLify
import os
 
from werkzeug.exceptions import HTTPException

from .extensions import db

from .views.main import main
from .views.P1MT import P1MT
from .views.Parzibot import Parzibot
from .views.MTools import MTools
from .views.SecurePass import SecurePass
from .views.LinuxSetup import LinuxSetup
from .views.Shorten import Shorten

BLUEPRINTS = (main, Parzibot, MTools, P1MT, SecurePass, LinuxSetup, Shorten)

def create_app():
	"""Create Flask Application"""
	app = Flask(__name__)

	app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL')  # Get link on database from server
	app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

	db.init_app(app)
	
	sslify = SSLify(app)
	
	with app.app_context():
		init_db()

	blueprints_factory(app, BLUEPRINTS)

	@app.errorhandler(HTTPException)
	def error_handler(e):
   	 	return flask.render_template('main/error.html', error_code=e.code), e.code

	return app

def blueprints_factory(app, blueprints):
	"""Registering blueprints"""
	for blueprint in blueprints:
		app.register_blueprint(blueprint)

def init_db():
	"""Init database"""
	db.create_all()