from hmac import new
from unicodedata import category
from flask import Blueprint, render_template, request, flash, redirect, url_for
from flask_login import login_user, login_required, logout_user, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from .models import User
from . import db

accounts = Blueprint('accounts', __name__)

@accounts.route('/login', methods=['GET', 'POST'])
def login():
    if request.method == 'POST':
        email = request.form.get('email')
        challenge_passwd = request.form.get('passwd_login')

        user = User.query.filter_by(email=email).first()
        if user:
            if check_password_hash(user.password, challenge_passwd):
                flash('Successful Login!', category='success')
                login_user(user, remember=True)
                return redirect(url_for('dashboards.market'))
            else:
                flash('Unsucessful Login!', category='error')
        else:
            flash('Unsucessful Login!', category='error')
    
    return render_template('login.html', user=current_user)
    
@accounts.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('accounts.login'))

@accounts.route('/register', methods=['GET', 'POST'])
def register():
    pass_list = list()
    if request.method == 'POST':
        email = request.form.get('email') 
        username = request.form.get('username')
        passwd_1 = request.form.get('passwd_1')
        passwd_2 = request.form.get('passwd_2')

        # Basic User Input Checks
        email_check = User.query.filter_by(email=email).first()
        if len(email) < 1:
            flash('Your Email must be longer than 0 characters.', category='error')
        elif email_check:
            flash('This Email is already taken', category='error')
        else:
            pass_list.append('p')
    
        if len(username) < 1:
            flash('Username must be something', category='error')
        else:
            pass_list.append('p')

        if len(passwd_1) < 8 or len(passwd_2) < 8:
            flash('Your Password must be longer than or equal to 8 characters.', category='error')
        else:
            if passwd_1 != passwd_2:
                flash('Your Passwords must match!', category='error')
            else:
                if len(pass_list) == 2:
                    new_user = User(email=email, 
                                    username=username,
                                    password=generate_password_hash(passwd_2, method='sha256')
                                    )
                    db.session.add(new_user)
                    db.session.commit()
                    flash('Account Registration Successful!', category='success')
                    return redirect(url_for('dashboards.market'))
                else:
                    flash('Registration Failed', category='error')
    return render_template("register.html", user=current_user)



