using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using IronIntel.Services;
using IronIntel.Site;
using IronIntel.Contractor.Users;
using IronIntel.Services.Customers;

namespace IronIntel.Contractor.Site
{
    public class CommonHttpRequestHandler : IronIntelHttpHandlerBase
    {
        public static byte[] GetCompanyLOGO(string companyid)
        {
            if (string.IsNullOrWhiteSpace(companyid))
            {
                return SystemParams.GetCompanyLOGO(CompanyInfo.FORESIGHT);
            }
            else
            {
                return SystemParams.GetCompanyLOGO(companyid);
            }
        }

        public static byte[] GetForesightLOGOInMainStyle()
        {
            return SystemParams.GetForesightLOGOInMainStyle();
        }

        public static byte[] GetCompanyLocationLOGO(string companyid)
        {
            return SystemParams.GetCompanyLocationLOGO(companyid);
        }
        public static byte[] GetLocationLOGO(string companyid)
        {
            return SystemParams.GetCompanyLocationLOGO(companyid);
        }

        public static byte[] GetCustomerLocationLOGO(int locationid)
        {
            CustomerProvider ic = SystemParams.GetCustomerProvider();
            return ic.GetCustomerLocationLOGO(locationid);
        }
        public override string GetIronSystemServiceAddress()
        {
            return SystemParams.SystemServiceAddresses[0];
        }

        public CommonHttpRequestHandler(HttpContext context)
            : base(context)
        {
        }

        public override void ProcessRequest()
        {
            string s = ReadTextFromStream(Context.Request.InputStream);
            if (string.IsNullOrWhiteSpace(s))
            {
                Context.Response.StatusCode = 204;
                Context.Response.End();
                return;
            }
            CommonRequestObject req = null;
            try
            {
                req = JsonConvert.DeserializeObject<CommonRequestObject>(s);
            }
            catch
            {
                Context.Response.StatusCode = 400;
                Context.Response.End();
                return;
            }
            if (req == null)
            {
                Context.Response.StatusCode = 204;
                Context.Response.End();
                return;
            }
            ProcessRequest(req);
        }

        private void ProcessRequest(CommonRequestObject req)
        {
            switch (req.Method)
            {
                case CommonRequestMethods.IamAlive:
                    IamAlive();
                    return;
                case CommonRequestMethods.GetAppModules:
                    GetAppModules();
                    return;
                case CommonRequestMethods.GetCurrentLoginName:
                    GetCurrentLoginName();
                    return;
                case CommonRequestMethods.AddLog:
                    AddLog(req.ClientData);
                    return;
                default:
                    Context.Response.StatusCode = 204;
                    Context.Response.End();
                    return;
            }
        }

        private void IamAlive()
        {
            if (LoginSession == null)
            {
                Context.Response.StatusCode = 401;
            }
            else
            {
                Context.Response.Write("\"OK\"");
                Context.Response.StatusCode = 200;
            }
            Context.Response.End();
        }

        private void GetAppModules()
        {
            if (LoginSession == null)
            {
                Context.Response.StatusCode = 401;
                Context.Response.End();
                return;
            }
            AppModuleInfo[] items = Acl.GetAvailableAppModuleInfos(LoginSession.User.UID);
            string json = JsonConvert.SerializeObject(items);
            Context.Response.Write(json);
            Context.Response.End();
        }

        private void GetCurrentLoginName()
        {
            string s = LoginSession == null ? string.Empty : LoginSession.User.Name;

            Context.Response.Write(s);
            Context.Response.End();
        }

        private void AddLog(string clientdata)
        {
            const char SPLITCHAR = (char)170;

            Context.Response.StatusCode = 200;
            try
            {
                string[] s = clientdata.Split(new char[] { SPLITCHAR });
                SystemParams.WriteLog(s[0], s[1], s[2], s[3]);
            }
            catch
            { }
            Context.Response.End();
        }
    }

    sealed class CommonRequestObject
    {
        public int MethodID { get; set; }
        public string ClientData { get; set; }
        public CommonRequestMethods Method
        {
            get { return (CommonRequestMethods)MethodID; }
        }
    }

    enum CommonRequestMethods
    {
        IamAlive = 0,
        GetAppModules = 1,
        GetCurrentLoginName = 2,
        AddLog = 3,
        GetMachineMapPinItem = 4,
        GetJobSiteMapItem = 5
    }
}