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

namespace IronIntel.Contractor.FICExtDataTable
{
    public abstract class FleetExtDataTable : IExtDataTable
    {
        protected FICContext Context { get; private set; }

        protected string UserIID
        {
            get
            {
                return Context == null ? string.Empty : Context.UserIID;
            }
        }

        public FleetExtDataTable()
        {

        }

        public abstract string ID { get; }
        public abstract string Name { get; }
        public abstract string Description { get; }
        public abstract string Version { get; }
        public abstract ExtDataTableParameter[] AvailableParameters { get; }

        public abstract DataTable GetData(int maxrows, KeyValuePair<string, object>[] parameters);
        public abstract DataTable GetSchemaTable();

        public void Init(FICContext context)
        {
            Context = context;
        }

        protected void WriteLog(string source, string message, string detail)
        {
            SystemParams.WriteLog("Error", "FleetExtDt", source, message, detail);
        }

        protected UserInfo GetCurrentUser()
        {
            if (Context == null)
            {
                return null;
            }
            try
            {
                UserQueryClient ic = FleetServiceClientHelper.CreateClient<UserQueryClient>();
                return ic.GetUserByIID(UserIID);
            }
            catch (Exception ex)
            {
                WriteLog(GetType().FullName + ".GetLoginSession()", "extdt=" + ID + ";useriid=" + UserIID, ex.ToString());
                return null;
            }
        }
    }
}
