
# !page: admin_register
# !latest updated: 09Jul2021

from flask import render_template, request, Blueprint, jsonify
from modules.database_modules import db_connect
from functions.date_time_functions import current_day
from htmlmin.minify import html_minify


user_webview_lattan = Blueprint("user_webview_lattan", __name__, static_folder="static",
                                template_folder="templates")


@user_webview_lattan.route('/', methods=['GET'])
def lattanView():
    con = db_connect()
    cur = con.cursor()
    cur.execute('select * from lattan_question order by rand() limit 20')
    questions = cur.fetchall()
    return html_minify(render_template('lattan_webview_port.html', questions=questions, appTitle='Lattan BayDin'))


@user_webview_lattan.route('/search', methods=['POST'])
def lattanSearch():
    con = db_connect()
    cur = con.cursor(dictionary=True, buffered=True)
    searchData = request.form['searchData']
    if len(searchData) < 1:
        cur.execute('select * from lattan_question order by rand() limit 20')
    elif len(searchData) > 0:
        cur.execute(
            'select * from lattan_question where question_mm like %s', (searchData + '%',))
    # else:
    #     cur.execute('select * from lattan_question where question_mm like %s',
    #                 ('%' + searchData + '%',))
    searchQuestions = cur.fetchall()
    return jsonify(searchQuestions), 200


@user_webview_lattan.route("/result", methods=['GET'])
def lattanResult():
    header = request.headers
    authKey = header.get('Authorization')
    serverAuth = '88d464b2c2f13bcd7d1d648126e0e8aa5abeb5b1a209639f4e53d994696a422587b403fd69e1baa27b21deb03c2020fd3303e5cdc3ded8e0bc686b5dbbcd37ae'
    userID = header.get('xtenId')
    if authKey == serverAuth:
        con = db_connect()
        cur = con.cursor(buffered=True)
        currentDay = current_day()
        cur.execute('select id from lattan_days where days=%s', (currentDay,))
        currentDayID = cur.fetchone()
        qID = request.args.get('qID')
        cur.execute(
            'select word_count, question_mm, question_eng from lattan_question where id=%s', (qID,))
        q_Data = cur.fetchone()
        q_wordCount = q_Data[0]
        q_mm = q_Data[1]
        q_eng = q_Data[2]
        ans_dayID = currentDayID[0] + q_wordCount % 7
        if ans_dayID > 7:
            ans_dayID = ans_dayID - 7

        # user_email = 'phyowaiyantungamc@gmail.com'
        # cur.execute('select id from user_account where email=%s', (user_email,))
        # userID = cur.fetchone()
        genderID = 0
        if userID != None:
            cur.execute(
                'select gender from user_info where user_id=%s', (userID,))
            userGender = cur.fetchone()[0]
            cur.execute('select id from lattan_gender where gender=%s',
                        (userGender,))
            genderID = cur.fetchone()[0]
        cur.execute('select result_mm, result_eng from lattan_data where question_id=%s and day_id=%s and gender_id=%s',
                    (qID, ans_dayID, genderID))
        result = cur.fetchone()
        return html_minify(render_template('lattan_webview_result.html', result=result, q_mm=q_mm, q_eng=q_eng, appTitle='Result'))
    return jsonify({'message': 'unauthorized access'}), 401
