import flask
from flask import request

import tts

import config
import utilities_script as utility

import os
import praxis_logging
praxis_logger_obj = praxis_logging.praxis_logger()
praxis_logger_obj.init(os.path.basename(__file__))
praxis_logger_obj.log("\n -Starting Logs: " + os.path.basename(__file__))

api = flask.Flask(__name__)
# enable/disable this to get web pages of crashes returned
api.config["DEBUG"] = False

def init():
    praxis_logger_obj.log("init stuff")

def isTTS_URL_Check(message):
    isNotBlocked = True
    is_ttsEnabled = config.is_tts_Speaker_Enabled
    is_tts_URL_Allowed = not config.is_tts_URL_Blocked
    has_URL = False
    if utility.contains_url(message):
            has_URL = True

    if is_tts_URL_Allowed:
        if has_URL:
            has_URL = False
    if has_URL:
        isNotBlocked = False
    if not is_ttsEnabled:
        isNotBlocked = False

    return not isNotBlocked

def try_TTS(tts_sender, tts_text):
    text_to_say: str = "%s says, %s" % (tts_sender, tts_text)

    #tts.tts(str(text_to_say))
    #tts.tts(str(tts_text))
    if tts_sender == "":
        text_to_say = tts_text

    if isTTS_URL_Check(tts_text):
        if not utility.contains_slur(tts_sender):
            if not utility.contains_slur(text_to_say):
                tts.tts(str(text_to_say))

    return flask.make_response('', 200)

@api.route('/api/v1/tts/speech', methods=['GET'])
def tts_speech():
    if 'tts_sender' not in request.args:
        tts_sender = ""
    else:
        tts_sender = request.args['tts_sender']
    if 'tts_text' not in request.args:
        return flask.make_response('{\"text\":"Argument \'tts_text\' not in request"}', 400)

    return try_TTS(tts_sender, request.args['tts_text'])

if __name__ == '__main__':
    #init()
    api.run(host='0.0.0.0', port=40085)