using FI.FIC;
using Foresight.Fleet.Services.Asset;
using Foresight.Fleet.Services.JobSite;
using Foresight.Fleet.Services.User;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IronIntel.Contractor
{
    public class FICSpecialGolbalFilterManager
    {
        const string ASSETID = "00000000-0000-0000-0000-000000000001";
        const string JOBSITEID = "00000000-0000-0000-0000-000000000002";

        public static readonly SpecialGlobalFilterInfo[] GlobalFilters = new SpecialGlobalFilterInfo[] {
            new SpecialGlobalFilterInfo(){IID=ASSETID,DisplayName="Available Assets",Description="Available Assets",DataType=FilterDataType.dtInteger},
            new SpecialGlobalFilterInfo(){IID=JOBSITEID,DisplayName="Available Jobsites",Description="Available Jobsites",DataType=FilterDataType.dtInteger}
        };

        private static bool IsValidId(string id)
        {
            foreach (var f in GlobalFilters)
            {
                if (string.Equals(id, f.IID, StringComparison.OrdinalIgnoreCase))
                {
                    return true;
                }
            }
            return false;
        }

        public static object[] GetSpecialGlobalFilterValuesByUser(string useriid, string filterid)
        {
            if (!IsValidId(filterid))
            {
                return new object[0];
            }
            if (string.IsNullOrWhiteSpace(useriid))
            {
                return new object[0];
            }

            if (string.Equals(filterid, ASSETID, StringComparison.OrdinalIgnoreCase))
            {
                return GetAvailableAssets(useriid);
            }
            else if (string.Equals(filterid, JOBSITEID, StringComparison.OrdinalIgnoreCase))
            {
                return GetAvailableJobSites(useriid);
            }

            return new object[0];
        }

        private static object[] GetAvailableAssets(string useriid)
        {
            //DateTime dt = DateTime.Now;
            AssetQueryClient client = FleetServiceClientHelper.CreateClient<AssetQueryClient>();
            long[] assetids = client.GetAvailableAssetsForUsers(SystemParams.CompanyID, useriid);
            object[] result = new object[assetids.Length];
            for (int i = 0; i < assetids.Length; i++)
            {
                result[i] = assetids[i];
            }
            //TimeSpan sp = DateTime.Now - dt;
            //SystemParams.WriteLog("DEBUG", "FICSpecialGolbalFilterManager", string.Format("Assets: {0}, {1}",result.Length,sp.ToString()), useriid);
            return result;
        }

        private static object[] GetAvailableJobSites(string useriid)
        {
            //DateTime dt = DateTime.Now;
            JobSiteProvider client = FleetServiceClientHelper.CreateClient<JobSiteProvider>();
            var jobsites = client.GetUserJobsites(SystemParams.CompanyID, useriid);
            object[] results = new object[jobsites.Length];
            for (int i = 0; i < jobsites.Length; i++)
            {
                results[i] = jobsites[i].ID;
            }
            //TimeSpan sp = DateTime.Now - dt;
            //SystemParams.WriteLog("DEBUG", "FICSpecialGolbalFilterManager", string.Format("Jobsites: {0}, {1}", results.Length, sp.ToString()), useriid);
            return results;
        }

        public static FilterTemplateInfo[] GetFilterTemplates(string useriid)
        {
            UserFilterProvider f = FleetServiceClientHelper.CreateClient<UserFilterProvider>();
            var items = f.GetFilterTemplates(SystemParams.CompanyID, useriid);
            List<FilterTemplateInfo> filters = new List<FilterTemplateInfo>();
            foreach (var item in items)
            {
                FilterTemplateInfo t = new FilterTemplateInfo();
                t.IID = item.Key;
                t.DisplayName = item.Value;
                t.DataType = FilterDataType.dtInteger;
                filters.Add(t);
            }

            return filters.ToArray();
        }

        public static object[] GetFilterValues(string useriid, int filterid)
        {
            UserFilterProvider f = FleetServiceClientHelper.CreateClient<UserFilterProvider>();
            long[] assets = f.GetFilteredAssets(SystemParams.CompanyID, filterid, useriid);
            if (assets == null || assets.Length == 0)
            {
                return new object[0];
            }
            object[] results = new object[assets.Length];
            for (int i = 0; i < assets.Length; i++)
            {
                results[i] = assets[i];
            }
            return results;
        }
    }
}
