using System.Net;
using System.Text;

namespace TimelogBackend;

class Program
{
    static void HandleMissingPath(HttpListenerResponse response)
    {
        response.StatusCode = 404;
        string errorMessage = "Not Found";
        byte[] buffer = Encoding.UTF8.GetBytes(errorMessage);
        response.ContentType = "text/plain";
        response.ContentLength64 = buffer.Length;
        response.OutputStream.Write(buffer, 0, buffer.Length);
        response.OutputStream.Write(buffer, 0, buffer.Length);
    }

    static void Main()
    {
        // create server
        HttpListener listener = new();
        // routes need to be added first
        listener.Prefixes.Add("http://localhost:5000/api/getall/");
        listener.Prefixes.Add("http://localhost:5000/api/gettopten/");
        listener.Prefixes.Add("http://localhost:5000/api/getuser/");
        listener.Prefixes.Add("http://localhost:5000/api/reset/");
        listener.Prefixes.Add("http://localhost:5000/api/createp/");
        listener.Prefixes.Add("http://localhost:5000/api/register/");
        listener.Prefixes.Add("http://localhost:5000/api/login/");
        listener.Prefixes.Add("http://localhost:5000/api/createlog/");

        // listen
        listener.Start();
        Console.WriteLine("Server is listening on http://localhost:5000/");
        while (true)
        {
            HttpListenerContext context = listener.GetContext();
            HttpListenerRequest request = context.Request;
            HttpListenerResponse response = context.Response;
            response.Headers.Add("Access-Control-Allow-Origin", "http://localhost:5173");
            response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
            response.Headers.Add("Access-Control-Allow-Headers", "Content-Type, Authorization");

            // url after localhost:5000/
            string uri;
            if (request != null && request.Url != null)
                uri = request.Url.AbsolutePath;
            else
                return;
            switch (request.HttpMethod)
            {
                case "GET":
                    switch (uri)
                    {
                        case "/api/reset":
                            Reset.HandleRequest(response);
                            break;
                        case "/api/getall":
                            Getall.HandleRequest(request, response);
                            break;
                        case "/api/gettopten":
                            Gettopten.HandleRequest(request, response);
                            break;
                        case "/api/getuser":
                            Getuser.HandleRequest(request, response);
                            break;
                        case "/api/createp":
                            CreateProcedure.HandleRequest(response);
                            break;
                        default:
                            HandleMissingPath(response);
                            break;
                    }
                    break;
                case "POST":
                    if (request.HasEntityBody)
                        switch (uri)
                        {
                            case "/api/register":
                                Register.HandleRequest(request, response);
                                break;
                            case "/api/login":
                                Login.HandleRequest(request, response);
                                break;
                            case "/api/createlog":
                                CreateLog.HandleRequest(request, response);
                                break;
                            default:
                                HandleMissingPath(response);
                                break;
                        }
                    else
                    {
                        HandleMissingPath(response);
                    }
                    break;
                default:
                    HandleMissingPath(response);
                    break;
            }
        }
    }
}
