import flask
from flask import request

import obsWebSocket

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

possibleRequests = []

def init():
    possibleRequests = obsWebSocket.getRequests()
    for r in possibleRequests:
        print("requestname: "+r)
    #obsWebSocket.makeRequest("ToggleStudioMode", {'source':'tinycam', 'render':'True'})
    #obsWebSocket.makeRequest("SetSourceRender", data={'source':"tinycam", 'render': False, 'scene-name':"Cam feed (main) INFOBOX"})
    #obsWebSocket.makeRequest("SetSourceRender", data={'source':"tinycam", 'render': True, 'scene-name':"Cam feed (main) INFOBOX"})

    #obsWebSocket.listenForData()

def do_request(requestName, data):
    if requestName in possibleRequests:
        obsWebSocket.makeRequest(requestName, data)

@api.route('/api/v1/obs/websocket/makeRequest', methods=['GET'])
def makeRequest():
    if 'request_name' not in request.args:
        return flask.make_response('{\"text\":"Argument \'request_name\' not in request"}', 400)
    if 'request_data' not in request.args:
        data = None
    else:
        data = request.args['request_data']
    #possibleRequests = obsWebSocket.getRequests()
    do_request(request.args['request_name'], data)

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