using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using Foresight.Fleet.Services;
using Foresight.Standard;

namespace IronIntel.Contractor
{
    public static class FleetServiceClientHelper
    {
        private static string[] FleetAssetServiceAddresses = null;

        static FleetServiceClientHelper()
        {
            string addresses = ConfigurationManager.AppSettings["FleetAssetServiceAddress"];
            if (!string.IsNullOrWhiteSpace(addresses))
            {
                FleetAssetServiceAddresses = addresses.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                FleetAssetServiceAddresses = new string[0];
            }
        }

        public static T CreateClient<T>(string workingcompanyid, string sessionid) where T : RemoteClientBase
        {
            object[] args = new object[1];
            args[0] = FleetAssetServiceAddresses;

            T client = (T)Activator.CreateInstance(typeof(T), args);
            client.AppName = SystemParams.APPNAME;
            client.WorkingCompanyID = string.IsNullOrWhiteSpace(workingcompanyid) ? SystemParams.CompanyID : workingcompanyid;
            client.SessionID = sessionid;
            return client;
        }

        public static T CreateClient<T>(string sessionid) where T : RemoteClientBase
        {
            return CreateClient<T>(string.Empty, sessionid);
        }

        public static T CreateClient<T>() where T : RemoteClientBase
        {
            return CreateClient<T>(string.Empty, string.Empty);
        }
    }
}
