using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Foresight.Fleet.Services.Customer;
using IronIntel.Services;
using IronIntel.Services.Business.Admin;

namespace IronIntel.Contractor.MapView
{
    public class LocationManagement
    {
        public static CompanyLocationViewItem[] GetCompanyLocations(string companyid)
        {
            List<CompanyLocationViewItem> ls = new List<CompanyLocationViewItem>();
            if (string.IsNullOrWhiteSpace(companyid) || string.Compare(companyid, SystemParams.CompanyID, true) == 0)
            {
                GetCompanyLocations(SystemParams.CompanyID, ls);
                if (!SystemParams.IsDealer)
                {
                    Services.Customers.CustomerInfo dealer = SystemParams.GetFirstDealerInfo();
                    if (dealer != null)
                    {
                        GetCompanyLocations(dealer.ID, ls);
                    }
                }
            }
            return ls.ToArray();
        }

        private static void GetCompanyLocations(string companyid, List<CompanyLocationViewItem> ls)
        {
            CustomerLocation[] locations = FleetServiceClientHelper.CreateClient<CustomerProvider>(companyid, string.Empty).GetCustomerLocations(companyid);

            foreach (CustomerLocation loc in locations)
            {
                ls.Add(ConvertToViewItem(loc, companyid));
            }
        }

        private static CompanyLocationViewItem ConvertToViewItem(CustomerLocation loc, string companyid)
        {
            CompanyLocationViewItem li = new CompanyLocationViewItem();
            li.CompanyID = companyid;
            li.ID = loc.ID;
            li.LocationName = loc.Name;
            li.Latitude = loc.Latitude;
            li.Longitude = loc.Longitude;
            li.Notes = loc.Notes;

            return li;
        }
    }
}
