import http.server
from logging import PlaceHolder
from hls import HLS
import json
import socketserver

from urllib.parse import urlparse
from urllib.parse import parse_qs


class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        # print(self.path)
        # Sending an '200 OK' response
        self.send_response(200)

        # Setting the header    
        # self.send_header("Content-type", "application/json")

        # Whenever using 'send_header', you also have to call 'end_headers'
        # self.end_headers()
        lesson_id = 0
        # Extract query param
        query_components = parse_qs(urlparse(self.path).query)
        if 'lesson_id' in query_components:
            lesson_id = query_components["lesson_id"][0]
            kurs_id = query_components["kurs_id"][0]

            # Some custom HTML code, possibly generated by another function
            hls = HLS(int(lesson_id),int(kurs_id))

            # Writing the HTML contents with UTF-8
            # self.wfile.write(bytes(json.dumps(hls.getLessonData()), "utf8"))

            hls.runProcess()

        return


# Create an object of the above class
handler_object = MyHttpRequestHandler

PORT = 55577


my_server = socketserver.TCPServer(("", PORT), handler_object)


# Star the server
my_server.serve_forever()
