using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using Foresight.Security;
using IronIntel.Services;
using IronIntel.Services.Users;
using IronIntel.Services.Customers;

namespace IronIntel.Contractor.Site
{
    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 virtual string GetIronSystemServiceAddress()
        {
            return ConfigurationManager.AppSettings["syssvcaddress"];
        }

        public virtual IronSysServiceClient GetSystemServiceClient()
        {
            IronSysServiceClient ic = new IronSysServiceClient(GetIronSystemServiceAddress());
            if (LoginSession != null)
            {
                ic.AppName = LoginSession.AppName;
                ic.LoginSessionID = LoginSession.SessionID;
                ic.CurrentUserIID = LoginSession.User.UID;
            }
            return ic;
        }

        public virtual CustomerProvider GetCustomerProvider()
        {
            CustomerProvider ic = new CustomerProvider(GetIronSystemServiceAddress());
            if (LoginSession != null)
            {
                ic.AppName = LoginSession.AppName;
                ic.LoginSessionID = LoginSession.SessionID;
                ic.CurrentUserIID = LoginSession.User.UID;
            }
            return ic;
        }

        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
            {
                LoginProvider ic = new LoginProvider(GetIronSystemServiceAddress());
                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);
            }
        }
    }
}
