using System.Net;
using System.Text;
using MySql.Data.MySqlClient;
/* using System.Threading; */


namespace Server
{
    class Program
    {
        static void Main()
        {
            // create server
            HttpListener listener = new HttpListener();
            // 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)
            {
                // wait for request
                HttpListenerContext context = listener.GetContext();
                // get response and request
                HttpListenerRequest request = context.Request;
                HttpListenerResponse response = context.Response;
                // mysql connection
                string connectionString = "server=127.0.0.1;uid=monty;pwd=some_pass;database=timelog";
                MySqlConnection conn = new MySqlConnection(connectionString);

                // url after localhost:5000/
                // i think the validation is unnecessry but the compiler doesn't
                string uri;
                if (request != null && request.Url != null)
                {
                    uri = request.Url.AbsolutePath;
                }
                else
                {
                    return;
                }
                switch (uri)
                {
                    case "/api/reset":
                        Reset.run(conn, request, response);
                        break;
                    case "/api/getall":
                        Getall.run(conn, request, response);
                        break;
                    case "/api/gettopten":
                        Gettopten.run(conn, request, response);
                        break;
                    case "/api/getuser":
                        Getuser.run(conn, request, response);
                        break;
                    case "/api/createp":
                        CreateProcedure.run(conn, request, response);
                        break;
                    case "/api/register":
                        Register.run(conn, request, response);
                        break;
                    case "/api/login":
                        Console.WriteLine("111");
                        Login.run(conn, request, response);
                        break;
                    case "/api/createlog":
                        CreateLog.run(conn, request, response);
                        break;
                    default:
                        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);
                        break;
                }
                // send the response
                response.OutputStream.Close();
            }
        }
    }
}
