from http.server import HTTPServer, BaseHTTPRequestHandler
import os.path as path
import threading
import socket
import errno
import logging

from .utils import OTA_ECP4_LOG, SCREEN_PORT, SCREEN_TEMPLATE, SCREEN_STATUS_FILE

logger = logging.getLogger(OTA_ECP4_LOG)


def setGPIO(path: str, value: str):
    try:
        with open(path, "w") as gpio:
            gpio.write(value)
    except (OSError, IOError) as e:
        logger.info(f"Error setting GPIO: {e}")


def provideFirmwareUpdateScreen(workdir: str, ipAddress: str) -> HTTPServer:

    setGPIO("/sys/class/gpio/gpio161/value", "1")
    setGPIO("/sys/class/gpio/gpio159/value", "1")
    setGPIO("/sys/class/gpio/gpio158/value", "1")

    httpServer = HTTPServer(("", SCREEN_PORT), createRequestHandler(workdir, ipAddress))

    screenThread = threading.Thread(target=httpServer.serve_forever)
    screenThread.daemon = True
    screenThread.start()

    return httpServer


def createRequestHandler(workdir: str, ipAddress: str) -> BaseHTTPRequestHandler:
    class RequestHandler(BaseHTTPRequestHandler):
        html = None

        def load_html(self):
            """Load the HTML file and replace placeholders."""

            sourcePath = path.join(path.dirname(__file__), "resources", SCREEN_TEMPLATE)
            if path.exists(sourcePath):
                with open(sourcePath, "r") as file:
                    self.html = file.read().replace(
                        "var URL = ''",
                        f"var URL = 'http://{ipAddress}:{SCREEN_PORT}/status'",
                    )

        def do_GET(self):
            if self.path in ["", "/", "/index.html", "/index.htm"]:
                if not self.html:
                    self.load_html()

                self.send_response(200)
                self.send_header("Access-Control-Allow-Origin", "*")
                self.send_header("Content-type", "text/html")
                self.end_headers()
                self.wfile.write(self.html.encode("utf-8"))

            elif self.path == "/status":
                filePath = path.join(workdir, SCREEN_STATUS_FILE)
                if path.isfile(filePath):
                    try:
                        self.send_response(200)
                        self.send_header("Access-Control-Allow-Origin", "*")
                        self.send_header("Content-type", "application/json")
                        self.end_headers()
                        with open(filePath, "rb") as f:
                            self.wfile.write(f.read())
                    except socket.error as e:
                        if e.errno == errno.EPIPE:
                            print("Client disconnected before full response.")
                        else:
                            raise
                else:
                    self._send404()
            else:
                self._send404()

        def _send404(self):
            """Send a 404 response."""
            self.send_response(404)
            self.end_headers()
            self.wfile.write(b"Resource not found.")

    return RequestHandler
