using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Xml;
using System.Web;
using IronIntel.Services;
using IronIntel.Contractor.Users;
using Foresight.ServiceModel;

namespace IronIntel.Contractor.Site.SystemSettings
{
    public class SystemSettingsBasePage : ContractorBasePage
    {
        protected void ProcessRequest(string methodName)
        {
            object result = null;
            try
            {
                if (methodName != null)
                {
                    switch (methodName.ToUpper())
                    {
                        case "GETCOMPANYUISTYLES":
                            result = GetCompanyUIStyles();
                            break;
                        case "SETCOMPANYUISTYLE":
                            result = SetCompanyUIStyle();
                            break;
                        case "DELETECOMPANYUISTYLE":
                            result = DeleteCompanyUIStyle();
                            break;
                        case "GETUSEROPTIONS":
                            result = GetUserOptions();
                            break;
                        case "SAVEUSEROPTIONS":
                            result = SaveUserOptions();
                            break;
                        case "GETTIMEZONES":
                            result = GetTimeZones();
                            break;
                        case "GETSYSTEMOPTIONS":
                            result = GetSystemOptions();
                            break;
                        case "SAVESYSTEMOPTIONS":
                            result = SaveSystemOptions();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                SystemParams.WriteLog("Error", "Settingcolor.ProcessRequest", ex.Message, ex.ToString());
            }

            string json = JsonConvert.SerializeObject(result);
            Response.Write(json);
            Response.End();
        }

        private object GetCompanyUIStyles()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    IronSysServiceClient ic = SystemParams.GetIronSystemServiceClient();
                    CustUIStyleList uis = ic.GetCompanyUIStyles(SystemParams.CompanyID);

                    List<CustUIStyleItem> list = new List<CustUIStyleItem>();
                    foreach (CustUIStyle ui in uis)
                    {
                        CustUIStyleItem item = new CustUIStyleItem();
                        Helper.CloneProperty(item, ui);
                        list.Add(item);
                    }
                    if (list.Count() > 0)
                        return list.ToArray();
                    else
                        return new CustUIStyleItem[0];
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string SetCompanyUIStyle()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    string uidata = Request.Params["ClientData"];
                    CustUIStyleItem item = JsonConvert.DeserializeObject<CustUIStyleItem>(uidata);
                    item.UIStyleName = HttpUtility.UrlDecode(item.UIStyleName);
                    CustUIStyle ui = new CustUIStyle();
                    Helper.CloneProperty(ui, item);
                    IronSysServiceClient ic = SystemParams.GetIronSystemServiceClient();
                    ic.SetCompanyUIStyle(SystemParams.CompanyID, ui);
                    return "OK";
                }
                else
                {
                    return "User being not logged in.";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string DeleteCompanyUIStyle()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    string uistyleid = HttpUtility.UrlDecode(Request.Params["ClientData"]);
                    int styleid = -1;
                    int.TryParse(uistyleid, out styleid);
                    IronSysServiceClient ic = SystemParams.GetIronSystemServiceClient();
                    ic.DeleteCompanyUIStyle(SystemParams.CompanyID, styleid);
                    return "OK";
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetUserOptions()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var up = UserParams.GetUserParams(session.SessionID, session.User.UID);
                    return up;
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object SaveUserOptions()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    string options = HttpUtility.UrlDecode(Request.Params["ClientData"]);
                    UserParamInfo upi = JsonConvert.DeserializeObject<UserParamInfo>(options);
                    UserParams.SetUserParams(session.User.UID, upi);
                    return "OK";
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetTimeZones()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    var timezones = SystemParams.GetTimeZones();
                    return timezones;
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetSystemOptions()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    string af = SystemParams.GetStringParam("AccuracyFilter");
                    double accuracyfilter = 0;
                    double.TryParse(af, out accuracyfilter);

                    SystemOptionInfo soi = new SystemOptionInfo();
                    soi.TimeZone = SystemParams.GetStringParam("CustomerTimeZone", false);
                    soi.AccuracyFilter = accuracyfilter;
                    soi.UnitOfOdometer = SystemParams.GetStringParam("UnitOfOdometer");
                    soi.AcknowledgingAlerts = SystemParams.GetStringParam("AcknowledgingAlerts");
                    return soi;
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object SaveSystemOptions()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    string options = HttpUtility.UrlDecode(Request.Params["ClientData"]);
                    SystemOptionInfo upi = JsonConvert.DeserializeObject<SystemOptionInfo>(options);

                    SystemParams.SetStringParam("CustomerTimeZone", upi.TimeZone);
                    SystemParams.SetStringParam("CustomerTimeZoneOffset", upi.Offset.ToString());
                    SystemParams.SetStringParam("AccuracyFilter", upi.AccuracyFilter.ToString());
                    SystemParams.SetStringParam("UnitOfOdometer", upi.UnitOfOdometer);
                    SystemParams.SetStringParam("AcknowledgingAlerts", upi.AcknowledgingAlerts);

                    return "OK";
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private class CustUIStyleItem
        {
            public int UIStyleID { get; set; }
            public string UIStyleName { get; set; }
            public bool IsDefault { get; set; }
            public string TitleBarColor { get; set; }
            public string MenuBackgroundColor { get; set; }
            public string ChartTitleBackgroundColor { get; set; }
            public string ChartBorderColor { get; set; }
        }

        private class SystemOptionInfo
        {
            public string TimeZone { get; set; }
            public int Offset { get; set; }
            public double AccuracyFilter { get; set; }
            public string UnitOfOdometer { get; set; }
            public string AcknowledgingAlerts { get; set; }
        }

    }
}