from flask import Flask, render_template, request, redirect, session
from flask_session import Session
import utils
import database

app = Flask(__name__);
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)

@app.route("/")
def home():
    return (render_template("home.html"));


@app.route("/connected")
def connected():
    if (not session.get("email")):
        return (redirect("/login"))    
    return (render_template("connected.html"));

@app.route("/login")
def login():
    return (render_template("login.html"));

@app.route('/login', methods=['POST'])
def login_post():
    email = request.form.get('email')
    password = request.form.get('password')
    if (not database.user_exist(email)):
        return (render_template("login.html", error="Email ou mot de passe faux"))
    if (not database.check_password(email, password)):
        return (render_template("login.html", error="Email ou mot de passe faux"))
    session["email"] = email
    return (redirect("/connected"))

@app.route('/logout')
def logout():
    session.pop('email', None)
    return redirect("/login")

@app.route("/signin")
def signin():
    return (render_template("signin.html"));

@app.route('/signin', methods=['POST'])
def signup_post():
    email = request.form.get('email')
    password = request.form.get('password')
    repassword = request.form.get('repassword')
    if (password != repassword):
        return (render_template("signin.html", error="Les deux mots de passe sont differents"))
    if (utils.check_email(email)):
        return (render_template("signin.html", error="Votre email n'est pas valdie"))
    if (database.user_exist(email)):
        return (render_template("signin.html", error="Email deja utilisé"))
    database.add_user(email, password);
    return (redirect("/connected"))

@app.route("/forgot")
def forgot():
    return (render_template("forgot.html"));

@app.route("/reset/<uuid>")
def reset(uuid):
    return ("bozo")

@app.route("/join/<uuid>")
def join(uuid):
    return ("bozo")

if __name__ == '__main__':
        app.run(host='0.0.0.0', port=5000, debug=1)
