
# !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 upload_time
from htmlmin.minify import html_minify


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


def sessionAuth(function):
    def wrapper(*args, **kwargs):
        header = request.headers
        authKey = header.get('Authorization')
        serverAuth = '88d464b2c2f13bcd7d1d648126e0e8aa5abeb5b1a209639f4e53d994696a422587b403fd69e1baa27b21deb03c2020fd3303e5cdc3ded8e0bc686b5dbbcd37ae'
        if authKey == serverAuth:
            return function()
        else:
            return jsonify({'message': 'unauthorized access'}), 401

    wrapper.__name__ = function.__name__
    return wrapper


@user_webview_hawsartan.route("/", methods=['GET'])
def tarotView():
    # if authKey == serverAuth:
    # qtype = request.arg.get('qtype')
    lang = request.args.get('lang')
    # lang = 'my'
    # lang = 'en'
    con = db_connect()
    cur = con.cursor(dictionary=True, buffered=True)
    scriptCategory = int(request.args.get('sType'))
    dayID = int(request.args.get('dayID'))
    # dayID = 0
    todayDate = upload_time()
    scriptTitle = ''
    scriptResult = None
    if scriptCategory == 0:
        scriptTitle = 'Weekly Script'
        cur.execute(
            'select script_mm, script_eng from script_week where start_date < %s and end_date > %s and day_id=%s', (todayDate, todayDate, dayID))
        scriptResult = cur.fetchone()
    #! due to the workflow the following functions will be temporary disabled
    # elif scriptCategory == 1:
    #     scriptTitle = 'Monthly Script'
    #     cur.execute(
    #         'select script_mm, script_eng from script_month where start_date < %s and end_date > %s', (todayDate, todayDate))
    #     scriptResult = cur.fetchone()
    # elif scriptCategory == 2:
    #     scriptTitle = 'Yearly Script'
    #     cur.execute(
    #         'select script_mm, script_eng from script_year where start_date < %s and end_date > %s', (todayDate, todayDate))
    #     scriptResult = cur.fetchone()
    return html_minify(render_template('hawsartan_webview_port.html', scriptResult=scriptResult, scriptTitle=scriptTitle, lang=lang, dayID=dayID))


# *************************************************************************************************************************************************
