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

namespace FastReport.Auth
{
    internal class TCPServerListener : IServerListener
    {
        private readonly HttpListener httpListener;
        private readonly string redirectURL;

        public string RedirectURL
        {
            get { return redirectURL; }
        }

        void Init()
        {

            httpListener.Prefixes.Clear();
            httpListener.Prefixes.Add(redirectURL);
        }

        public void Open()
        {
            Init();
            httpListener.Start();

            // Creates the OAuth 2.0 authorization request.

        }


        internal void Stop()
        {
            httpListener.Stop();
        }

        public void Close()
        {
            httpListener.Close();
        }

        public TCPServerListener()
        {
            httpListener = new HttpListener();
            redirectURL = string.Format("http://{0}:{1}/",
                IPAddress.Loopback,
                25042);
        }

        internal HttpListenerContext WaitConnect()
        {
            return httpListener.GetContext();
        }

        public void Dispose()
        {
            httpListener.Close();
        }

        //internal Task WaitConnectAsync()
        //{

        //}
    }
}
