from screen import screenlive
import json, argparse
from flask import Flask, json
import threading
from time import sleep
from websockets.sync.server import serve
import websockets
import array
import time

api = Flask(__name__)
watchers_last_seen = {}
stream = False
debug = False

def current_time():
    return str(int(time.time()))

@api.route("/off", methods=["GET"])
def streaOff():
    global stream
    stream=False
    return "True"

@api.route("/on", methods=["GET"])
def streamOn():
    global stream
    stream=True
    return "True"

@api.route("/views", methods=["GET"])
def views():
    global watchers_last_seen
    return str(len(watchers_last_seen))

@api.route("/mode", methods=["GET"])
def mode():
    global watchers_last_seen
    return str(stream)


def ws_server(websocket):
    global stream, watchers_last_seen
    if debug: print("WebSocket: Server Started.")
    try:
        while True:
            req=websocket.recv()
            if "%" in req:
                args= req.split("%")[1]
                args = dict(x.split("=") for x in args.split(";"))
                req = req.split("%")[0]
            if debug: print(req,"-----",args)
            match (req):
                case ("get_stream"):
                    if stream:
                        watchers_last_seen[args["uuid"]] = current_time()
                        websocket.send(f""+str(json.dumps([True, screenlive.gen()])))
                    else:
                        websocket.send(f"null")
                case (_):
                    websocket.send(f"null")
    except websockets.exceptions.ConnectionClosedOK:
        if debug: print("hungup")
    except websockets.ConnectionClosedError:
        if debug: print("Internal Server Error.")

def run_ws_server():
        with serve(ws_server, "0.0.0.0", 5621) as server:
            server.serve_forever()

def timeout_task():
    while True:
        for key,value in watchers_last_seen.items():
            if debug: print(key+"||||"+value)
            if (int(current_time())-int(value)) > 5:
                if debug: print(key+"is timed out")
                watchers_last_seen.pop(key)
                break
        if debug: print("time -",current_time())
        if debug: print("watchers -",watchers_last_seen.keys())
        sleep(10)

def run_flask_server():
    api.run(host="0.0.0.0", port=5622)

def main():
    t1 = threading.Thread(target=run_ws_server)
    t2 = threading.Thread(target=run_flask_server)
    t3 = threading.Thread(target=timeout_task)

    t1.start()
    t2.start()
    t3.start()

    t1.join()
    t2.join()
    t3.join()
 
if __name__ == "__main__":
    main()
