# coding: utf-8
from flask import Flask
from flask.logging import default_handler
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from flask_mail import Mail
from flask_bcrypt import Bcrypt

import logging
import sys
from .config import Config

#if sys.version_info.major < 3:
#    reload(sys)
#sys.setdefaultencoding('utf8')

db = SQLAlchemy()
bcrypt = Bcrypt()
mail = Mail()

login_manager = LoginManager()
login_manager.login_view = 'main.login'
login_manager.login_message = u'Para continuar, ingrese su nombre de usuario y contraseña.'
login_manager.login_message_category = 'info'




def create_app(config_class=Config):
	app = Flask(__name__)

	app.config.from_object(config_class)

	if app.debug:
		app.logger.setLevel(logging.DEBUG)
	else:
		app.logger.setLevel(logging.INFO)

	db.init_app(app)
	login_manager.init_app(app)
	mail.init_app(app)


	from webinterface.content.errors.handlers import errors
	app.register_blueprint(errors)

	from webinterface.content.main import main
	app.register_blueprint(main)

	from webinterface.content.gestion.routes import gestion
	app.register_blueprint(gestion)

	from webinterface.content.gtfs.routes import gtfs
	app.register_blueprint(gtfs)

	from webinterface.content.docs.routes import docs
	app.register_blueprint(docs)

	return app
