using Foresight.Fleet.Services.JobSite;
using Foresight.ServiceModel;
using IronIntel.Contractor.Contact;
using IronIntel.Contractor.JobSites;
using IronIntel.Contractor.Maintenance;
using IronIntel.Contractor.MapView;
using IronIntel.Contractor.Users;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace IronIntel.Contractor.Site.Contact
{
    public class ContactBasePage : ContractorBasePage
    {
        protected void ProcessRequest(string methodName)
        {
            object result = null;
            try
            {
                if (methodName != null)
                {
                    switch (methodName)
                    {
                        case "GetContacts":
                            result = GetContacts();
                            break;
                        case "SaveContact":
                            result = SaveContact();
                            break;
                        case "GetUsers":
                            result = GetUsers();
                            break;
                        case "DeleteContact":
                            result = DeleteContact();
                            break;
                        case "GetSelectedMachines":
                            result = GetSelectedMachines();
                            break;
                        case "SaveContactMachines":
                            result = SaveContactMachines();
                            break;
                        case "GetJobsiteList":
                            result = GetJobsiteList();
                            break;
                        case "GetSelectedJobsites":
                            result = GetSelectedJobsites();
                            break;
                        case "SaveContactJobsites":
                            result = SaveContactJobsites();
                            break;

                    }
                }
            }
            catch (Exception ex)
            {
                SystemParams.WriteLog("error", "ContactBasePage", ex.Message, ex.ToString());
                throw ex;
            }
            string json = JsonConvert.SerializeObject(result);
            Response.Write(json);
            Response.End();
        }

        private object GetContacts()
        {
            try
            {
                var session = GetCurrentLoginSession();
                ContactInfo[] items = null;
                if (session != null)
                {
                    var s = Request.Form["ClientData"];
                    s = HttpUtility.HtmlDecode(s);

                    items = ContactManagement.GetContacts(session.SessionID);
                }
                else
                {
                    items = new ContactInfo[0];
                }
                return items.OrderBy(m => m.ContactName);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string SaveContact()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    var s = Request.Form["ClientData"];
                    s = HttpUtility.HtmlDecode(s);

                    var ci = JsonConvert.DeserializeObject<ContactInfo>(s);
                    if (string.IsNullOrWhiteSpace(ci.ContactID))
                    {
                        ci.ContactID = Guid.NewGuid().ToString();
                    }

                    ContactManagement.SaveContact(ci, GetCurrentLoginSession().User.UID);

                    return "OK";
                }
                else
                {
                    return "Failed";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetUsers()
        {
            try
            {
                var session = GetCurrentLoginSession();
                UserInfo[] items = null;
                if (session != null)
                {
                    items = UserManagement.GetUsers();
                }
                else
                {
                    items = new UserInfo[0];
                }
                return items.OrderBy(m => m.ID);
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string DeleteContact()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    var contactid = Request.Form["ClientData"];
                    contactid = HttpUtility.HtmlDecode(contactid);
                    ContactManagement.DeleteContact(contactid);

                    return "OK";
                }
                else
                {
                    return "Failed";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private MaintenanceMachineInfo[] GetSelectedMachines()
        {
            var contactid = Request.Form["ClientData"];
            var machines = ContactManagement.GetContactMachinesByID(contactid);

            return machines.OrderBy(m => m.VIN).ToArray();
        }

        private string SaveContactMachines()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var contactid = HttpUtility.HtmlDecode(clientdata[0]);
                    var machineids = HttpUtility.HtmlDecode(clientdata[1]);

                    string[] ids = JsonConvert.DeserializeObject<string[]>(machineids);

                    ContactManagement.SaveContactMachines(contactid, SystemParams.CompanyID, ids);

                    return "OK";
                }
                else
                {
                    return "Failed";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private object GetJobsiteList()
        {
            try
            {
                JobSiteViewItem[] items = null;
                if (GetCurrentLoginSession() != null)
                {
                    var s = Request.Form["ClientData"];
                    s = HttpUtility.HtmlDecode(s);

                    var jss = CreateClient<JobSiteProvider>().GetJobSiteItems(SystemParams.CompanyID, "", false);
                    List<JobSiteViewItem> list = new List<JobSiteViewItem>();
                    foreach (var js in jss)
                    {
                        JobSiteViewItem item = new JobSiteViewItem();
                        item.ID = js.ID;
                        item.Name = js.Name;

                        list.Add(item);
                    }
                    items = list.ToArray();
                }
                else
                {
                    items = new JobSiteViewItem[0];
                }
                return items;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private object GetSelectedJobsites()
        {
            try
            {
                JobSiteViewItem[] items = null;
                if (GetCurrentLoginSession() != null)
                {
                    var contactid = Request.Form["ClientData"];
                    contactid = HttpUtility.HtmlDecode(contactid);

                    items = ContactManagement.GetContactJobsitesByID(contactid);
                }
                else
                {
                    items = new JobSiteViewItem[0];
                }
                return items;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string SaveContactJobsites()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    var clientdata = Request.Form["ClientData"].Split((char)170);
                    var contactid = HttpUtility.HtmlDecode(clientdata[0]);
                    var jobsiteids = HttpUtility.HtmlDecode(clientdata[1]);

                    string[] ids = JsonConvert.DeserializeObject<string[]>(jobsiteids);

                    ContactManagement.SaveContactJobsites(contactid, SystemParams.CompanyID, ids);

                    return "OK";
                }
                else
                {
                    return "Failed";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
    }

}
