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

namespace IronIntel.Contractor.Site
{
    public class MainBasePage : ContractorBasePage
    {
        protected void ProcessRequest()
        {
            string methidName = Request.Params["MethodName"];
            switch (methidName)
            {
                case "GetUserName":
                    GetUserName();
                    break;
                case "GetAppModules":
                    GetAppModules();
                    break;
                case "GetVersions":
                    GetVersions();
                    break;
                case "GetSiteHeaderNote":
                    GetSiteHeaderNote();
                    break;
                default:
                    break;
            }
            //TODO
        }

        private void GetUserName()
        {
            string userName = "";
            var session = GetCurrentLoginSession();
            if (session != null && session.User != null)
            {
                userName = session.User.Name;
            }

            userName = JsonConvert.SerializeObject(userName);
            Response.Write(userName);
            Response.End();
        }

        private void GetAppModules()
        {
            try
            {
                AppModuleInfo[] items = null;
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    List<AppModuleInfo> list = Acl.GetAvailableAppModuleInfos(session.User.UID).ToList();
                    LicenseInfo license = SystemParams.GetLicense();
                    if (license != null && license.Items.Count > 0)
                    {
                        LicenseItem lijl = license.Items.FirstOrDefault(m => m.Key == "JOBSITELIMIT");
                        if (lijl == null || !Helper.IsTrue(lijl.Value))
                        {
                            AppModuleInfo item = list.FirstOrDefault(m => m.ID == "JOBSITELIMIT");
                            list.Remove(item);
                        }
                    }
                    if (!session.User.IsForesightUser)
                    {
                        bool isallowed = UserManagement.CheckUserPermission(session.SessionID, session.User.UID, 30);
                        if (!isallowed)
                        {
                            AppModuleInfo item = list.FirstOrDefault(m => m.ID == "OTRConfig");
                            list.Remove(item);
                        }
                    }
                    items = list.ToArray();
                }
                else
                {
                    items = new AppModuleInfo[0];
                }
                string json = JsonConvert.SerializeObject(items);
                Response.Write(json);
            }
            catch (Exception ex)
            {
                AddLog("Error", "GetAppModules", ex.Message, ex.ToString());
                Response.Write(JsonConvert.SerializeObject(ex.Message));
            }
            Response.End();
        }

        private void GetVersions()
        {
            List<string> versions = new List<string>();
            var session = GetCurrentLoginSession();
            if (session != null && session.User != null)
            {
                versions.Add(SystemParams.GetVersion());
                versions.Add(SystemParams.GetFICVersion());
            }

            string json = JsonConvert.SerializeObject(versions.ToArray());
            Response.Write(json);
            Response.End();
        }
        private void GetSiteHeaderNote()
        {
            string siteheadernote = "";
            var session = GetCurrentLoginSession();
            if (session != null && session.User != null)
            {
                siteheadernote = UserManagement.GetSiteHeaderNote(session.User.UID);
                if (string.IsNullOrEmpty(siteheadernote))
                {
                    var cust = FleetServiceClientHelper.CreateClient<Foresight.Fleet.Services.Customer.CustomerProvider>().GetCustomerDetail(SystemParams.CompanyID);
                    if (cust != null)
                        siteheadernote = cust.SiteHeaderNotes;
                }
            }
            string json = JsonConvert.SerializeObject(siteheadernote);
            Response.Write(json);
            Response.End();
        }
    }
}