using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using Rest.Net;

namespace Rest.Net.Example
{
    public class Program
    {
        public class User
        {
            public string Name = null;

            public User(string name)
            {
                this.Name = name;
            }

            public static explicit operator User(string input)
            {
                return new User(input.ToString());
            }
        }

        public static void Main(string[] args)
        {
            var listener = new RouteListener(8080,
                new Route(HttpRequestMethod.Get, "/status", Status),
                new Route<double, double>(HttpRequestMethod.Post, "/add/{a}/{b}", Add),
                new Route<User>(HttpRequestMethod.Post, "/signup/{username}", Signup)
            );

            listener.Start();

            Console.WriteLine($"Rest api server running at http://localhost:{listener.Port}");

            listener.Block();
        }

        public static HttpListenerResponse Status(HttpListenerRequest request, HttpListenerResponse response)
        {
            return response.WithStatus(HttpStatusCode.OK).WithText("Everything is operational.");
        }

        public static HttpListenerResponse Add(HttpListenerRequest request, HttpListenerResponse response, double a, double b)
        {
            return response.WithStatus(HttpStatusCode.OK).WithText((a + b).ToString());
        }

        public static HttpListenerResponse Signup(HttpListenerRequest request, HttpListenerResponse response, User user)
        {
            return response.WithStatus(HttpStatusCode.OK).WithText("User:" + user.Name);
        }
    }
}
