from flask import flash, render_template, Blueprint, current_app, redirect, request, url_for, session, g
from datetime import timezone
from ownchatbot.db import get_db, reread_goals, reread_votes, rem_vote, reset_vote, reset_goal, clear_fulfilled_rewards, clear_reward_queue, rem_cool, rem_from_queue
from ownchatbot.reward_handlers import all_active_votes, all_active_goals, all_active_rewards, get_queue, fulfill_reward, save_rewards, activate_category, deactivate_category, refund_reward, reread_categories, save_config
from ownchatbot.user_handlers import get_all_users, get_all_users_by_name, refund_points, adjust_points, change_email, get_email_code, del_email_code
from ownchatbot.bot_messages import save_announce
from ownchatbot.owncast_com import send_private_chat
import json
import emoji
from ownchatbot.kofi_handlers import save_kofi_settings, kofi_pngs
import random

ocb = Blueprint('web_panels', __name__)


@ocb.route('/mgmt', methods=['GET'])  # The streamer's management panel
def mgmt():
    auth_code = request.args.get('auth')
    if auth_code == current_app.config['MGMT_AUTH']:
        session['auth_code'] = auth_code  # Store auth code in session
    else:
        return "Not authorized", 403
    db = get_db()
    users = get_all_users(db)
    utc_timezone = timezone.utc
    rewards = current_app.config['REWARDS']
    active_rewards = []
    for each_reward in all_active_rewards():  # Get the name of all active rewards
        active_rewards.append(each_reward)
    active_categories = current_app.config['ACTIVE_CAT']
    inactive_categories = current_app.config['INACTIVE_CAT']
    all_cats = current_app.config['ALL_CAT']
    mgmt_auth = current_app.config['MGMT_AUTH']
    points_interval = current_app.config['POINTS_INTERVAL']
    points_award = current_app.config['POINTS_AWARD']
    gunicorn_logging = current_app.config['GUNICORN']
    prefix = current_app.config['PREFIX']
    access_token = current_app.config['ACCESS_TOKEN']
    owncast_url = current_app.config['OWNCAST_URL']
    kofi_token = current_app.config['KOFI_TOKEN']
    kofi_integration = current_app.config['KOFI_INTEGRATION']
    kofi_logos = kofi_pngs()
    announce_enable = current_app.config['ANNOUNCE_ENABLE']
    announce_interval = current_app.config['ANNOUNCE_INTERVAL']
    announcements = current_app.config['ANNOUNCEMENTS']
    settings_info = [
        mgmt_auth,
        points_interval,
        points_award,
        gunicorn_logging,
        prefix,
        access_token,
        owncast_url,
        kofi_token,
        kofi_integration,
        announce_enable,
        announce_interval
        ]
    
    return render_template('mgmt.html',
                           queue=get_queue(db),
                           votes=all_active_votes(db),
                           goals=all_active_goals(db),
                           rewards=rewards,
                           active_rewards=active_rewards,
                           prefix=current_app.config['PREFIX'],
                           kofi_settings=current_app.config['KOFI_SETTINGS'],
                           kofi_integration=kofi_integration,
                           kofi_logos=kofi_logos,
                           announcements=announcements,
                           users=users,
                           utc_timezone=utc_timezone,
                           active_categories=active_categories,
                           inactive_categories=inactive_categories,
                           settings_info=settings_info)


@ocb.route('/userpanel', methods=['GET'])  # The viewers panel
def user_panel():
    db = get_db()
    instance = request.args.get('instance')
    all_rewards = rewards = current_app.config['REWARDS']
    username = request.args.get('username')
    points_interval = current_app.config['POINTS_INTERVAL']
    points_award = current_app.config['POINTS_AWARD']
    if username is not None:
        users = get_all_users_by_name(db, username)
    else:
        users = []
    utc_timezone = timezone.utc
    return render_template('userpanel.html',
                           queue=get_queue(db),
                           votes=all_active_votes(db),
                           goals=all_active_goals(db),
                           rewards=all_active_rewards(),
                           all_rewards=all_rewards,
                           prefix=current_app.config['PREFIX'],
                           kofi_settings=current_app.config['KOFI_SETTINGS'],
                           kofi_integration=current_app.config['KOFI_INTEGRATION'],
                           points_interval=points_interval,
                           points_award=points_award,
                           username=username,
                           users=users,
                           instance=instance,
                           utc_timezone=utc_timezone)


@ocb.route('/mgmt/fulfill', methods=['GET'])
def fulfilled():
    db = get_db()
    reward_id = request.args.get('reward_id')
    username = request.args.get('username')
    fulfill_reward(db, reward_id)
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/refund', methods=['GET'])
def refund():
    db = get_db()
    reward_id = request.args.get('reward_id')
    reward = request.args.get('reward')
    rewards = current_app.config['REWARDS']
    points = rewards[reward]['price']
    username = request.args.get('username')
    user_id = request.args.get('rewarder_id')
    refund_points(db, user_id, points)  # resets points
    refund_reward(db, reward_id)  # marks the reward as refunded
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/edit_account/<user_id>', methods=['GET', 'POST'])  # Streamer manually edit user's account
def edit_account(user_id):
    if 'auth_code' not in session:
        return "Not authorized", 403
    db = get_db()
    name = request.args.get('name')
    points = request.args.get('points')
    email = request.args.get('email')
    if request.method == 'POST':
        user_id = request.form['user_id']
        name = request.form['name']
        newpoints = request.form['newpoints']
        adjust_points(db, user_id, newpoints)
        newemail = request.form['newemail']
        if newemail == 'None':
            current_app.logger.info(f'No email change requested')
        else:
            if change_email(db, user_id, newemail):
                if newemail == '':
                    current_app.logger.info(f'Removed {name}\'s email')
                else:
                    current_app.logger.info(f'Changed {name}\'s email to {newemail}')
        return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))

    return render_template('edit_account.html',
                           name=name,
                           user_id=user_id,
                           points=points,
                           email=email)


@ocb.route('/mgmt/delete/<reward_name>', methods=['GET', 'POST'])
def delete(reward_name):
    del_reward = current_app.config['REWARDS']
    del_reward.pop(reward_name)
    if save_rewards(del_reward):
        if rem_cool(reward_name):
            rem_from_queue(reward_name)
            if reread_votes():
                if reread_goals():
                    pass
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/edit/<reward_name>', methods=['GET', 'POST'])
def edit(reward_name):
    if 'auth_code' not in session:
        return "Not authorized", 403
    active_categories = current_app.config['ACTIVE_CAT']
    all_the_rewards = current_app.config['REWARDS']
    reward_data = all_the_rewards[reward_name]
    all_cats = current_app.config['ALL_CAT']
    
    if request.method == 'POST':
        reward_data['cooldown'] = int(request.form['cooldown'])
        reward_data['type'] = request.form['type']
        if reward_data['type'] == 'goal':
            reward_data['target'] = int(request.form['target'])
            if "milestones" not in reward_data:  # If using old rewards.py, and no milestones key exists, create one
                reward_data["milestones"] = {"milestone1": [], "milestone2": [], "milestone3": []}
            
            if request.form['milestone1_points'] == '':
                reward_data['milestones']['milestone1'] = []
            else:
                milestone1_points = int(request.form['milestone1_points'])
                reward_data['milestones']['milestone1'] = [request.form['milestone1_desc'], milestone1_points]
            
            if request.form['milestone2_points'] == '':
                reward_data['milestones']['milestone2'] = []
            else:
                milestone2_points = int(request.form['milestone2_points'])
                reward_data['milestones']['milestone2'] = [request.form['milestone2_desc'], milestone2_points]
            
            if request.form['milestone3_points'] == '':
                reward_data['milestones']['milestone3'] = []
            else:
                milestone3_points = int(request.form['milestone3_points'])
                reward_data['milestones']['milestone3'] = [request.form['milestone3_desc'], milestone3_points]
        else:
            reward_data['price'] = int(request.form['price'])
        reward_data['info'] = emoji.demojize(request.form['info'])
        if reward_data['type'] == 'special':
            reward_data['cmd'] = request.form['cmd']
        reward_data['categories'] = request.form.getlist('category')
        reward_data['cooldown'] = int(request.form['cooldown'])
        all_the_rewards[reward_name] = reward_data
        save_rewards(all_the_rewards)
        if reward_data['type'] == 'goal':  # Sync goals and votes in the db with rewards.py
            reread_goals()
        if reward_data['type'] == 'vote':
            reread_votes()
        return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
    
    return render_template('edit.html',
                           all_cats=all_cats,
                           reward_name=reward_name,
                           active_categories=active_categories,
                           reward_data=reward_data)


@ocb.route('/mgmt/settings', methods=['GET', 'POST'])  # OwnchatBot settings panel
def settings():
    points_interval = int(request.form['points_interval'])
    points_award = int(request.form['points_award'])
    gunicorn_logging = 'gunicorn_logging' in request.form
    prefix = request.form['prefix']
    access_token = request.form['access_token']
    owncast_url = request.form['owncast_url']
    mgmt_auth = request.form['mgmt_auth']
    kofi_integration = 'kofi_integration' in request.form
    kofi_token = request.form['kofi_token']
    config_dict = {
        'MGMT_AUTH': mgmt_auth,
        'POINTS_INTERVAL': points_interval,
        'POINTS_AWARD': points_award,
        'GUNICORN': gunicorn_logging,
        'PREFIX': prefix,
        'ACCESS_TOKEN': access_token,
        'OWNCAST_URL': owncast_url,
        'KOFI_TOKEN': kofi_token,
        'KOFI_INTEGRATION': kofi_integration
        }
    if save_config(config_dict):  # Save new config.py
        current_app.logger.info('Saved new config.')
        
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/announcements', methods=['GET', 'POST'])  # OwnchatBot settings panel
def announcements():
    announce_enable = 'announce_enable' in request.form
    announce_interval = int(request.form['announce_interval'])
    new_announcements = []
    new_announcements = request.form['announcements'].strip().split('\n')
    announce_dict = {
        'ANNOUNCEMENTS': new_announcements,
        'ANNOUNCE_ENABLE': announce_enable,
        'ANNOUNCE_INTERVAL': announce_interval
        }
    if save_announce(announce_dict):  # Save new announce.py
        current_app.logger.info('Saved new announcements.')
        
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/ksettings', methods=['GET', 'POST'])  # OwnchatBot settings panel
def ksettings():
    kofi_settings_dict = current_app.config['KOFI_SETTINGS']
    if request.method == 'POST':
        enable_donations = 'enable_donations' in request.form
        set_donation_points = request.form['set_donation_points']
        enable_subs = 'enable_subs' in request.form
        sub_points = int(request.form['sub_points'])
        kofi_url = request.form['kofi_url']
        kofi_logo = request.form.get('kofi_logo')
        
        kofi_settings_dict['donations'] = enable_donations
        kofi_settings_dict['subs'] = enable_subs
        kofi_settings_dict['sub_points'] = sub_points
        kofi_settings_dict['kofi_url'] = kofi_url
        kofi_settings_dict['kofi_logo'] = kofi_logo
        if save_kofi_settings(kofi_settings_dict):
            current_app.logger.info(f'Saved Kofi settings')
    
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/add/<reward_type>', methods=['GET', 'POST'])
def add(reward_type):
    if 'auth_code' not in session:
        return "Not authorized", 403
    all_cats = current_app.config['ALL_CAT']
    active_categories = current_app.config['ACTIVE_CAT']
    all_the_rewards = current_app.config['REWARDS']
    
    if request.method == 'POST':
        name = request.form['name']
        name = name.lower()  # Force the name to all lower case
        name = emoji.demojize(name)  # Remove any emojis
        name = name.replace(" ", "")  # Remove any spaces from the name
        type = request.form['type']
        
        if name in all_the_rewards:  # Check for duplicate reward names
            flash("A reward with this name already exists.", "error")  # Flash an error message
            return redirect(url_for('web_panels.add', reward_type=reward_type))  # Redirect back to the add page
        
        if type != 'category':  # If we're only adding a category, skip all of this
            cooldown = int(request.form['cooldown'])
            if type == 'redeem' or type == 'special' or type == 'vote':
                price = int(request.form['price'])
            if type == 'goal':
                target = int(request.form['target'])
                milestone1_desc = request.form['milestone1_desc']
                if request.form['milestone1_points'] == '':
                    milestone1_points = ''
                else:
                    milestone1_points = int(request.form['milestone1_points'])
                milestone2_desc = request.form['milestone2_desc']
                if request.form['milestone2_points'] == '':
                    milestone2_points = ''
                else:
                    milestone2_points = int(request.form['milestone2_points'])
                milestone3_desc = request.form['milestone3_desc']
                if request.form['milestone3_points'] == '':
                    milestone3_points = ''
                else:
                    milestone3_points = int(request.form['milestone3_points'])
            info = request.form['info']
            info = emoji.demojize(info)  # Remove any emojis
            if type == 'special':
                cmd = request.form['cmd']
            categories = request.form.getlist('category')
            if type == 'redeem':
                if categories == ['']:
                    all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cooldown': cooldown}
                else:
                    all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown}
            if type == 'goal':
                if categories == ['']:
                    all_the_rewards[name] = {'target': target, 'type': type, 'info': info, 'cooldown': cooldown}
                else:
                    all_the_rewards[name] = {'target': target, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown}
                all_the_rewards[name]["milestones"] = {"milestone1": [], "milestone2": [], "milestone3": []}  # Create empty milestones key
                if milestone1_points:
                    all_the_rewards[name]["milestones"]["milestone1"] = [milestone1_desc, milestone1_points]
                if milestone2_points:
                    all_the_rewards[name]["milestones"]["milestone2"] = [milestone2_desc, milestone2_points]
                if milestone3_points:
                    all_the_rewards[name]["milestones"]["milestone3"] = [milestone3_desc, milestone3_points]
            if type == 'vote':
                if categories == ['']:
                    all_the_rewards[name] = {'price': price, 'type': type, 'info': info}
                else:
                    all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'categories': categories, 'cooldown': cooldown}
            if type == 'special':
                if categories == ['']:
                    all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cmd': cmd, 'cooldown': cooldown}
                else:
                    all_the_rewards[name] = {'price': price, 'type': type, 'info': info, 'cmd': cmd, 'categories': categories, 'cooldown': cooldown}
            save_rewards(all_the_rewards)
            if type == 'goal':  # Remove old goals and votes from the database
                reread_goals()
            if type == 'vote':
                reread_votes()
        else:  # If we're only adding a category
            inactive_categories = current_app.config['INACTIVE_CAT']
            inactive_categories.append(name)  # Add it to the INACTIVE_CAT variable
            reread_categories()  # Write it to categories.py
        return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))

    return render_template('add.html',
                           all_cats=all_cats,
                           reward_type=reward_type,
                           active_categories=active_categories)


@ocb.route('/set_viewer_email', methods=['GET', 'POST'])
def set_viewer_email():
    db = get_db()
    mail_reg_code = int(request.form['code'])
    user_id = request.form['user_id']
    db_mail_reg_code = get_email_code(db, user_id)
    new_email = request.form['new_email']
    instance = request.form['instance']
    user_name = request.form['user_name']
    if mail_reg_code == db_mail_reg_code:
        if change_email(db, user_id, new_email):
            del_email_code(db, user_id)
            flash(f"Email Address \"{new_email}\" successfully registered.", "success")
            send_private_chat(user_id, f'{user_name}, thanks for registering for Kofi perks! I appreciate your support!')
            current_app.logger.info(f'Changed {user_id}\'s email to {new_email}')
    else:
        flash(f"Incorrect code. Email Address \"{new_email}\" was not registered.", "failure")
        current_app.logger.info(f'The code entered, \"{mail_reg_code}\", does not match \"{db_mail_reg_code}\" found in database.')
    return redirect(url_for('web_panels.user_panel', instance=instance, username=user_name))


@ocb.route('/mgmt/activate/<category>', methods=['GET', 'POST'])
def activate(category):
    activate_category(category)
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/deactivate/<category>', methods=['GET', 'POST'])
def deactivate(category):
    deactivate_category(category)
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/delcat/<cat_name>/<cat_act>', methods=['GET', 'POST'])
def delcat(cat_name, cat_act):
    active_categories = current_app.config['ACTIVE_CAT']
    inactive_categories = current_app.config['INACTIVE_CAT']
    if cat_act == 'inactive':
        inactive_categories.remove(cat_name)
    else:
        active_categories.remove(cat_name)
    reread_categories()
    current_rewards = current_app.config['REWARDS']
    for reward, details in current_rewards.items():  # Remove from rewards.py as well
        if cat_name in details['categories']:
            details['categories'].remove(cat_name)
    save_rewards(current_rewards)
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/reset/<reward_name>/<reward_type>', methods=['GET', 'POST'])  # Reset votes and goals to zero
def reset(reward_name, reward_type):
    if reward_type == "goal":
        reset_goal(reward_name)
    if reward_type == "vote":
        reset_vote(reward_name)
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/rereadvotes', methods=['GET', 'POST'])
def rereadv():
    reread_votes()
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/clearfulfilled', methods=['GET', 'POST'])
def clearfulfilled():
    clear_fulfilled_rewards()
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))


@ocb.route('/mgmt/clearqueue', methods=['GET', 'POST'])
def clear_queue():
    clear_reward_queue()
    return redirect(url_for('web_panels.mgmt', auth=session['auth_code']))
