using Foresight.Fleet.Services.Customer;
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.SystemSettings
{
    public class CustomerProviderBasePage : ContractorBasePage
    {
        protected void ProcessRequest(string methodName)
        {
            object result = null;
            try
            {
                if (methodName != null)
                {
                    switch (methodName.ToUpper())
                    {
                        case "GETCUSTOMERLOCATIONS":
                            result = GetCustomerLocations();
                            break;
                        case "SAVECUSTOMERLOCATION":
                            result = SaveCustomerLocation();
                            break;
                        case "DELETECUSTOMERLOCATION":
                            result = DeleteCustomerLocation();
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                result = ex.Message;
                SystemParams.WriteLog("Error", "CustomerProvider.ProcessRequest", ex.Message, ex.ToString());
            }

            string json = JsonConvert.SerializeObject(result);
            Response.Write(json);
            Response.End();
        }
        private object GetCustomerLocations()
        {
            try
            {
                var session = GetCurrentLoginSession();
                if (session != null)
                {
                    CustomerLocation[] locations = CreateClient<CustomerProvider>().GetCustomerLocations(SystemParams.CompanyID);

                    List<CustomerLocationItem> list = new List<CustomerLocationItem>();
                    foreach (CustomerLocation lc in locations)
                    {
                        CustomerLocationItem item = new CustomerLocationItem();
                        Helper.CloneProperty(item, lc);
                        list.Add(item);
                    }
                    if (list.Count() > 0)
                        return list.ToArray().OrderBy(m => m.Name);
                    else
                        return new CustomerLocationItem[0];
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
        private object SaveCustomerLocation()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    string data = HttpUtility.UrlDecode(Request.Params["ClientData"]);
                    HttpPostedFile uploadFile = null;
                    byte[] logobyte = null;
                    if (Request.Files.Count > 0)
                    {
                        uploadFile = Request.Files[0];
                        logobyte = ConvertFile2bytes(uploadFile);
                    }

                    CustomerLocationItem item = JsonConvert.DeserializeObject<CustomerLocationItem>(data);
                    CustomerLocation location = new CustomerLocation();
                    Helper.CloneProperty(location, item);
                    var cp = CreateClient<CustomerProvider>();
                    int locationid = location.ID;
                    if (locationid == -1)
                        locationid = cp.AddCustomerLocation(SystemParams.CompanyID, location);
                    else
                        cp.UpdateCustomerLocation(location);

                    if (logobyte != null)
                        cp.ChangeLocationLOGO(locationid, logobyte);
                    return "OK";
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        private string DeleteCustomerLocation()
        {
            try
            {
                if (GetCurrentLoginSession() != null)
                {
                    string id = HttpUtility.UrlDecode(Request.Params["ClientData"]);
                    int locationid = -1;
                    int.TryParse(id, out locationid);
                    CreateClient<CustomerProvider>().DeleteCustomerLocation(locationid);
                    return "OK";
                }
                else
                {
                    throw new Exception("not login.");
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public class CustomerLocationItem
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public double Latitude { get; set; }
            public double Longitude { get; set; }
            public string Address { get; set; }
            public string Notes { get; set; }
        }
    }
}