import flask
import string
import json

BASE64 = string.digits + string.ascii_letters + "-_"

app = flask.Flask(__name__)

with open("questions.json", "r", encoding="utf-8") as file_pointer:
    QUESTIONS = json.load(file_pointer)

def encode_state(game_encoding):
    characters = [game_encoding[i*6:i*6+6] for i in range(0,6)]
    return ''.join(
        [BASE64[sum([2**j for j in range(6) if not i[j]])] for i in characters]
    )


def decode_state(game_state):
    bit_string = ''.join([format(BASE64.index(s), '06b')[::-1] for s in game_state])
    return [x != "1" for x in bit_string]

@app.route('/board')
def main_page():
    game_encoding = flask.request.args.get("game_state")
    if game_encoding == ".":
        return flask.redirect("/")

    if game_encoding is None:
        game_encoding = "000000"
    game_state = decode_state(game_encoding)

    empty = 36-sum(game_state)

    return flask.render_template("main_board.html", categories=QUESTIONS["categories"], game_state=game_encoding, draw_cells=game_state, empty=empty)

@app.route('/final')
def final_jeopardy():
    question_text = QUESTIONS['final'][0]

    if question_text == "":
        return flask.redirect("/")
    elif question_text == "$":
        return flask.render_template("question picture.html", game_state=".")


    return flask.render_template("question.html", question_text=question_text, game_state=".")

@app.route('/final_answer')
def final_answer():
    answer_text = QUESTIONS["final"][1]

    if answer_text == "":
        return flask.redirect("/")
    elif answer_text == "$":
        return flask.render_template("answer picture.html", game_state=".")

    return flask.render_template("answer.html", answer_text=answer_text, game_state=".")

@app.route('/')
def empty_page():
    return flask.render_template("empty.html")

@app.route('/question')
def question():
    game_encoding = flask.request.args.get("game_state")
    question = flask.request.args.get("question")
    if game_encoding is None or question is None:
        return flask.redirect("/board")

    game_state = decode_state(game_encoding)
    empty = 36-sum(game_state)
    game_state[int(question)]  = False
    game_encoding = encode_state(game_state)
    question_text = QUESTIONS["questions"][int(question)%6][int(question)//6][0]

    if question_text == "":
        return flask.redirect("/board?game_state="+game_encoding)
    elif question_text == "$":
        return flask.render_template("question picture.html", question=int(question), game_state=game_encoding, empty=empty)


    return flask.render_template("question.html", question=int(question), question_text=question_text, game_state=game_encoding, empty=empty)

@app.route('/answer')
def answer():
    game_encoding = flask.request.args.get("game_state")
    game_state = decode_state(game_encoding)
    empty = 35-sum(game_state)
    question = flask.request.args.get("question")
    if game_encoding is None or question is None:
        return flask.redirect("/board")

    answer_text = QUESTIONS["questions"][int(question)%6][int(question)//6][1]

    if answer_text == "":
        return flask.redirect("/board?game_state="+game_encoding)
    elif answer_text == "$":
        return flask.render_template("answer picture.html", question=int(question), game_state=game_encoding, empty=empty)

    return flask.render_template("answer.html", question=int(question), answer_text=answer_text, game_state=game_encoding, empty=empty)

@app.route('/static/<path:path>')
def flag_image(path):
    return flask.send_from_directory("static", path)

if __name__ == "__main__":
    app.run(host='0.0.0.0', port=8001)