using Foresight.Fleet.Services.User;
using Foresight.ServiceModel;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace IronIntel.Contractor.Site.Maintenance
{
    public class MaintenanceBasePage : ContractorBasePage
    {
        protected void ProcessRequest(string methodName)
        {
            object result = null;

            if (methodName != null)
            {
                switch (methodName.ToUpper())
                {
                    case "GETNAVS":
                        result = GetNavigations();
                        break;
                }
            }

            string json = JsonConvert.SerializeObject(result);
            Response.Write(json);
            Response.End();
        }
        private MaintenanceNavigateItem[] GetNavigations()
        {
            List<MaintenanceNavigateItem> list = GetNavigateItems();
            Foresight.Fleet.Services.Customer.LicenseInfo license = SystemParams.GetLicense();
            if (license != null && license.Items.Count > 0)
            {
                var woitem = license.Items.FirstOrDefault(m => m.Key == "WorkOrder");
                if (woitem == null || !Helper.IsTrue(woitem.Value))
                {
                    MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_workorder");
                    list.Remove(item);
                    item = list.FirstOrDefault(m => m.ID == "nav_workorderhis");
                    list.Remove(item);
                }
                var alitem = license.Items.FirstOrDefault(m => m.Key == "AlertsManagement");
                if (alitem == null || !Helper.IsTrue(alitem.Value))
                {
                    MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_alertsmanagement");
                    list.Remove(item);
                    item = list.FirstOrDefault(m => m.ID == "nav_alertsmappings");
                    list.Remove(item);

                    MaintenanceNavigateItem item1 = list.FirstOrDefault(m => m.ID == "nav_alertsmappings");
                    list.Remove(item1);
                }
                var fuelitem = license.Items.FirstOrDefault(m => m.Key == "FuelRecords");
                if (fuelitem == null || !Helper.IsTrue(fuelitem.Value))
                {
                    MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_fuelrecord");
                    list.Remove(item);
                }
                var critem = license.Items.FirstOrDefault(m => m.Key == "CustomerRecord");
                if (critem == null || !Helper.IsTrue(critem.Value))
                {
                    MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_customerrecord");
                    list.Remove(item);
                    item = list.FirstOrDefault(m => m.ID == "nav_customersatisfactionsurveys");
                    list.Remove(item);
                    item = list.FirstOrDefault(m => m.ID == "nav_surveymanagementresult");
                    list.Remove(item);
                    item = list.FirstOrDefault(m => m.ID == "nav_wosurveytemplate");
                    list.Remove(item);
                    item = list.FirstOrDefault(m => m.ID == "nav_wosurveytemplatereport");
                    list.Remove(item);
                }
                var pmitem = license.Items.FirstOrDefault(m => m.Key == "PreventativeMaintenance");
                if (pmitem == null || !Helper.IsTrue(pmitem.Value))
                {
                    MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_maintenanceschedule");
                    list.Remove(item);
                }
                var mritem = license.Items.FirstOrDefault(m => m.Key == "MaintenanceRecord");
                if (mritem == null || !Helper.IsTrue(mritem.Value))
                {
                    MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_record");
                    list.Remove(item);
                }
            }

            var user = GetCurrentUser();
            if (user.UserType != Users.UserTypes.SupperAdmin)
            {
                MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_alertsmappings");
                if (item != null)
                    list.Remove(item);
            }
            if (user.UserType == Users.UserTypes.Common)
            {
                var client = CreateClient<PermissionProvider>();
                Tuple<Feature, Permissions>[] pmss = client.GetUserPermissions(SystemParams.CompanyID, user.IID);
                if (pmss.Length > 0)
                {
                    Tuple<Feature, Permissions> wopm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.WORK_ORDER);
                    if (wopm == null)
                    {
                        MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_workorder");
                        list.Remove(item);
                    }
                    Tuple<Feature, Permissions> wohispm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.WORKORDERHISTORY);
                    if (wohispm == null)
                    {
                        MaintenanceNavigateItem itemhis = list.FirstOrDefault(m => m.ID == "nav_workorderhis");
                        list.Remove(itemhis);
                    }
                    Tuple<Feature, Permissions> alertpm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.ALERTS_MANAGEMENT);
                    if (alertpm == null)
                    {
                        MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_alertsmanagement");
                        list.Remove(item);
                        item = list.FirstOrDefault(m => m.ID == "nav_alertsmappings");
                        list.Remove(item);

                        MaintenanceNavigateItem item1 = list.FirstOrDefault(m => m.ID == "nav_alertsmappings");
                        list.Remove(item1);
                    }
                    Tuple<Feature, Permissions> pmpm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.PREVENTATIVE_MAINTENANCE);
                    if (pmpm == null)
                    {
                        MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_maintenanceschedule");
                        list.Remove(item);
                        item = list.FirstOrDefault(m => m.ID == "nav_record");
                        list.Remove(item);
                    }
                    Tuple<Feature, Permissions> fuelpm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.FUEL_RECORDS);
                    if (fuelpm == null)
                    {
                        MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_fuelrecord");
                        list.Remove(item);
                    }
                    Tuple<Feature, Permissions> crpm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.CUSTOMER_RECORD);
                    if (crpm == null)
                    {
                        MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_customerrecord");
                        list.Remove(item);
                    }
                    Tuple<Feature, Permissions> wosurpm = pmss.FirstOrDefault(m => m.Item1.Id == Feature.WORKORDERSURVEYS);
                    if (crpm == null || wosurpm == null)
                    {
                        MaintenanceNavigateItem item = list.FirstOrDefault(m => m.ID == "nav_customersatisfactionsurveys");
                        list.Remove(item);
                        item = list.FirstOrDefault(m => m.ID == "nav_surveymanagementresult");
                        list.Remove(item);
                        item = list.FirstOrDefault(m => m.ID == "nav_wosurveytemplate");
                        list.Remove(item);
                        item = list.FirstOrDefault(m => m.ID == "nav_wosurveytemplatereport");
                        list.Remove(item);
                    }
                }
            }
            else if (user.UserType == Users.UserTypes.Readonly)
                list.Clear();

            return list.ToArray();
        }


        private static List<MaintenanceNavigateItem> GetNavigateItems()
        {
            List<MaintenanceNavigateItem> list = new List<MaintenanceNavigateItem>();

            MaintenanceNavigateItem item2 = new MaintenanceNavigateItem();
            item2.ID = "nav_workorder";
            item2.Title = "Work Order";
            item2.Url = "WorkOrderMaintenance.aspx";
            item2.IconPath = "img/workorder.png";
            list.Add(item2);

            MaintenanceNavigateItem itemhis = new MaintenanceNavigateItem();
            itemhis.ID = "nav_workorderhis";
            itemhis.Title = "Work Order History";
            itemhis.Url = "WorkOrderHistory.aspx";
            itemhis.IconPath = "img/workorder.png";
            list.Add(itemhis);

            MaintenanceNavigateItem item1 = new MaintenanceNavigateItem();
            item1.ID = "nav_alertsmanagement";
            item1.Title = "Alerts Management";
            item1.Url = "AlertsManagement.aspx";
            item1.IconPath = "img/alert.png";
            list.Add(item1);

            MaintenanceNavigateItem alertmappingsitem = new MaintenanceNavigateItem();
            alertmappingsitem.ID = "nav_alertsmappings";
            alertmappingsitem.Title = "Alert Mappings";
            alertmappingsitem.Url = "AlertMappingManagement.aspx";
            alertmappingsitem.IconPath = "img/alert.png";
            list.Add(alertmappingsitem);

            MaintenanceNavigateItem item3 = new MaintenanceNavigateItem();
            item3.ID = "nav_maintenanceschedule";
            item3.Title = "Maintenance Schedules";
            item3.Url = "MaintenanceSchedulesManagement.aspx";
            item3.IconPath = "img/preventative.png";
            list.Add(item3);

            MaintenanceNavigateItem item4 = new MaintenanceNavigateItem();
            item4.ID = "nav_record";
            item4.Title = "Maintenance Record";
            item4.Url = "MaintanceRecordsManagement.aspx";
            item4.IconPath = "img/record.png";
            list.Add(item4);

            MaintenanceNavigateItem item5 = new MaintenanceNavigateItem();
            item5.ID = "nav_fuelrecord";
            item5.Title = "Fuel Records";
            item5.Url = "FuelRecordManagement.aspx";
            item5.IconPath = "img/fuelrecord.png";
            list.Add(item5);

            MaintenanceNavigateItem item6 = new MaintenanceNavigateItem();
            item6.ID = "nav_customerrecord";
            item6.Title = "Customer Record";
            item6.Url = "CustomerRecordManagement.aspx";
            item6.IconPath = "img/customerrecord.png";
            list.Add(item6);

            MaintenanceNavigateItem item7 = new MaintenanceNavigateItem();
            item7.ID = "nav_customersatisfactionsurveys";
            item7.Title = "Customer Satisfaction Surveys";
            //item7.Url = "SurveyTemplateManagement.aspx";
            item7.IconPath = "img/customerrecord.png";
            list.Add(item7);

            MaintenanceNavigateItem item8 = new MaintenanceNavigateItem();
            item8.ID = "nav_surveymanagementresult";
            item8.Title = "Survey Management/Result";
            item8.Url = "SurveyManagement.aspx";
            list.Add(item8);

            MaintenanceNavigateItem item9 = new MaintenanceNavigateItem();
            item9.ID = "nav_wosurveytemplate";
            item9.Title = "Templates";
            item9.Url = "SurveyTemplateManagement.aspx";
            list.Add(item9);

            MaintenanceNavigateItem item10 = new MaintenanceNavigateItem();
            item10.ID = "nav_wosurveytemplatereport";
            item10.Title = "Reports";
            item10.Url = "SurveyTemplateReport.aspx";
            list.Add(item10);

            return list;
        }
        public class MaintenanceNavigateItem
        {
            public string ID { get; set; }
            public string Title { get; set; }
            public string Url { get; set; }
            public string IconPath { get; set; }
        }
    }
}
