from socket import *

serverPort = 7788
serverSocket = socket(AF_INET, SOCK_STREAM)
serverSocket.bind(("", serverPort))
serverSocket.listen(1)
print("The server is ready to receive")

while True:
    connectionSocket, addr = serverSocket.accept()
    sentence = connectionSocket.recv(1024).decode()
    print("IP: " + addr[0] + ", Port: " + str(addr[1]))
    print(sentence)
    ip = addr[0]
    port = addr[1]
    string_list = sentence.split(' ')  # Split request from spaces
    method = string_list[0]
    requestFile = string_list[1]
    file = requestFile.split('?')[0]  # After the "?" symbol not relevant here
    file = file.lstrip('/')

    if file == '' or file == 'index.html' or file == 'main_en.html' or file == 'en':
        connectionSocket.send(f"HTTP/1.1 200 OK\r\n".encode())
        connectionSocket.send(f"Content-Type: text/html\r\n".encode())
        connectionSocket.send(f"\r\n".encode())
        with open('main_en.html', 'r') as f:
            connectionSocket.send(f.read().encode())

    elif file == 'ar':
        # Read the HTML file with the correct encoding
        with open('main_ar.html', encoding='utf-8') as f:
            html = f.read()
        # Send the HTML file to the browser with the correct encoding
        connectionSocket.send(f"HTTP/1.1 200 OK\r\n".encode())
        connectionSocket.send(f"Content-Type: text/html; charset=UTF-8\r\n".encode())
        connectionSocket.send(f"\r\n".encode())
        connectionSocket.send(html.encode())

    elif file.endswith(".html"):
        connectionSocket.send(f"HTTP/1.1 200 OK\r\n".encode())
        connectionSocket.send(f"Content-Type: text/html\r\n".encode())
        connectionSocket.send(f"\r\n".encode())
        with open(file, 'r') as f:
            connectionSocket.send(f.read().encode())

    elif file.endswith(".css"):
        connectionSocket.send(f"HTTP/1.1 200 OK\r\n".encode())
        connectionSocket.send(f"Content-Type: text/css\r\n".encode())
        connectionSocket.send(f"\r\n".encode())
        with open(file, 'r') as f:
            connectionSocket.send(f.read().encode())

    elif file.endswith(".png"):
        connectionSocket.send(f"HTTP/1.1 200 OK\r\n".encode())
        connectionSocket.send(f"Content-Type: image/png \r\n".encode())
        connectionSocket.send(f"\r\n".encode())
        with open(file, 'rb') as f:
            connectionSocket.send(f.read())

    elif file.endswith(".jpg"):
        connectionSocket.send(f"HTTP/1.1 200 OK\r\n".encode())
        connectionSocket.send(f"Content-Type: image/jpeg \r\n".encode())
        connectionSocket.send(f"\r\n".encode())
        with open(file, 'rb') as f:
            connectionSocket.send(f.read())

    elif file == "go":
        connectionSocket.send(f"HTTP/1.1 307 Temporary Redirect\r\n".encode())
        connectionSocket.send(f"Location: https://www.google.com/ \r\n".encode())

    elif file == "so":
        connectionSocket.send(f"HTTP/1.1 307 Temporary Redirect\r\n".encode())
        connectionSocket.send(f"Location: https://stackoverflow.com/ \r\n".encode())

    elif file == 'bzu':
        connectionSocket.send(f"HTTP/1.1 307 Temporary Redirect\r\n".encode())
        connectionSocket.send(f"Location: https://www.birzeit.edu/en \r\n".encode())

    else:
        # When an exception handled, it will return a simple HTML with our IDs
        connectionSocket.send(f"HTTP/1.1 404 Not Found\r\n".encode())
        response = (
                '<html><title>Error</title><body><center><h1 style = "color : red ;">Error 404: The file is not '
                'found</h1><hr><p style= '
                '"font-weight: '
                'bold;">Zainab Jaradat - 1201766</p><p style="font-weight: bold;">Rawan Sous - '
                '1200129</p><hr><h2>IP: ' + str(ip) + ', Port: ' + str(port) +
                '</h2></center></body></html>').encode('utf-8')
        connectionSocket.send(f"\r\n".encode())
        connectionSocket.send(response)
        connectionSocket.close()