# Site Back-End Logic
from hmac import new
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, Message
from . import db
import string, secrets

logic = Blueprint('logic', __name__)

@logic.route('/', methods=['GET', 'POST'])
@login_required
def profile():
    if request.method == "POST":
        new_desc = request.form.get('description_area')
        passwd_1 = request.form.get('passwd_1')
        passwd_2 = request.form.get('passwd_2')
        new_pic = request.files.get('profilepic_upload')


        if new_desc:
            new_desc_dbcall = User.query.filter_by(id=current_user.id).first()
            new_desc_dbcall.description = new_desc
            db.session.commit()
            flash('Hooray! A new description!', category='success')
        elif passwd_1 and passwd_2:
            if passwd_1 == passwd_2:
                new_passwd_dbcall = User.query.filter_by(id=current_user.id).first()
                new_passwd_dbcall.password = generate_password_hash(passwd_2, method='sha256')
                db.session.commit()
                flash('Nice! Updated your password!', category='success')
            else:
                flash('Oh no! Your passwords must match!', category='error')
        elif new_pic:
            # generate random filename for uploaded file 
            alphanumeric = string.ascii_letters + string.digits
            ralphanum = ''.join(secrets.choice(alphanumeric) for i in range(16))
            new_pic_dbcall = User.query.filter_by(id=current_user.id).first()
            if '.png' in new_pic.filename:
                new_pic.save(f'app/static/uploads/{ralphanum}.png')
                new_pic_dbcall.profile_image = f'{ralphanum}.png'
            elif '.jpg' in new_pic.filename or 'jpeg' in new_pic.filename:
                new_pic.save(f'app/static/uploads/{ralphanum}.jpeg')
                new_pic_dbcall.profile_image = f'{ralphanum}.jpeg'
            db.session.commit()



    return render_template("profile.html", user=current_user)

#recipient_id = None

@logic.route('/matchbook', methods=['GET', 'POST'])
@login_required
def matchbook():
    all_users = User.query.all()

    if request.method == 'POST':
        #global recipient_id
        recipient_id = request.form.get('message_recipient')
        fdbcall = User.query.filter_by(id=current_user.id).first()##
        fdbcall.focus = recipient_id##
        db.session.commit()##

        return redirect(url_for('logic.messaging'))

    return render_template("matchbook.html", user=current_user, userlist=all_users)

@logic.route('/messaging', methods=['GET', 'POST'])
@login_required
def messaging():
    recipient_id = current_user.focus##
    recipient = User.query.filter_by(id=recipient_id).first()

    sent_history = Message.query.filter_by(sender=current_user.id, recipient=recipient.id).all()
    recv_history = Message.query.filter_by(sender=recipient.id, recipient=current_user.id).all()
    joint_history = sent_history + recv_history
    joint_history.sort(key=lambda x: x.id) # add reverse=True for descending
    ## add filter to remove duplicate messages to self?
    if request.method == 'POST':
        message = request.form.get('message_box')
        new_msg_dbcall = Message(sender=current_user.id, recipient=recipient.id, message=message)
        db.session.add(new_msg_dbcall)
        db.session.commit()
        return redirect(url_for('logic.messaging'))
    return render_template("messaging.html", user=current_user, recipient=recipient, msg_hist=joint_history)


@logic.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('logic.profile'))
            else:
                flash('Unsucessful Login!', category='error')
        else:
            flash('Unsucessful Login!', category='error')
    
    return render_template("login.html", user=current_user)

@logic.route('/logout')
@login_required
def logout():
    logout_user()
    return redirect(url_for('logic.login'))

# Signup Route
@logic.route('/register', methods=['GET', 'POST'])
def register():
    pass_list = list()
    if request.method == 'POST':
        email = request.form.get('email') 
        firstname = request.form.get('firstname')
        lastname = request.form.get('lastname')
        age = request.form.get('age')
        gender = request.form.get('gender')
        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(firstname) < 1:
            flash('First name must be something', category='error')
        else:
            pass_list.append('p')

        if len(lastname) < 1:
            flash('Last name must be something', category='error')
        else:
            pass_list.append('p')

        if gender == 'M' or gender == 'F':
            pass_list.append('p')
        else:
            flash('Gender must be either M or F!', category='error')

        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) == 4:
                    new_user = User(email=email, firstname=firstname, lastname=lastname, age=age, gender=gender, 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('logic.profile'))
                else:
                    flash('Registration Failed', category='error')
    return render_template("register.html", user=current_user)
