using Foresight.Fleet.Services.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace IronIntel.Contractor.iisitebase
{

    public class IronIntelHttpHandlerBase : IDisposable
    {
        public static string LocalHostName
        {
            get { return IronIntelBasePage.LocalHostName; }
        }

        public HttpContext Context { get; private set; }
        protected LoginSession LoginSession { get; private set; }

        protected int ClientTimeOffset { get; private set; }

        public IronIntelHttpHandlerBase(HttpContext context)
        {
            Context = context;
            try
            {
                LoginSession = GetCurrentLoginSession();
            }
            catch
            {
                LoginSession = null;
            }
            ClientTimeOffset = GetClientTimeOffset();
        }


        public LoginSession GetCurrentLoginSession()
        {
            HttpCookie cookie = Context.Request.Cookies[IronIntelBasePage.LOGINSESSION_COOKIENAME];
            if (cookie == null)
            {
                return null;
            }
            if (string.IsNullOrWhiteSpace(cookie.Value))
            {
                return null;
            }
            string sessionid = IronIntelBasePage.DecryptString(cookie.Value);
            try
            {
                UserQueryClient ic = FleetServiceClientHelper.CreateClient<UserQueryClient>();
                return ic.GetLoginSession(sessionid);
            }
            catch
            {
                return null;
            }
        }

        private int GetClientTimeOffset()
        {
            HttpCookie cookie = Context.Request.Cookies[IronIntelBasePage.CLIENT_TIMEOFFSET_COOKIENAME];
            if (cookie == null)
            {
                return IronIntelBasePage.LocalTimeOffset;
            }
            if (string.IsNullOrWhiteSpace(cookie.Value))
            {
                return IronIntelBasePage.LocalTimeOffset;
            }
            int n = 0;
            if (int.TryParse(cookie.Value, out n))
            {
                return n;
            }
            else
            {
                return IronIntelBasePage.LocalTimeOffset;
            }
        }

        public static string ReadTextFromStream(System.IO.Stream stream)
        {
            using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
            {
                return sr.ReadToEnd();
            }
        }

        public virtual void ProcessRequest()
        {

        }

        private bool disposed = false;
        protected void Dispose(bool disposed)
        {
            Context = null;
            LoginSession = null;
        }

        public void Dispose()
        {
            if (!disposed)
            {
                Dispose(true);
                disposed = true;
            }
        }

        public string UserHostAddress
        {
            get
            {
                return IronIntelBasePage.GetUserHostAddress(Context.Request);
            }
        }
        protected string GetLanguageCookie()
        {
            HttpCookie cookie = Context.Request.Cookies[IronIntelBasePage.LANGUAGE_COOKIENAME];
            if (cookie == null)
            {
                return "en-us";
            }
            if (string.IsNullOrWhiteSpace(cookie.Value))
            {
                return "en-us";
            }
            return cookie.Value;
        }
    }
}
