using InABox.Clients;
using System;
using System.Collections.Generic;
using System.Text;

namespace InABox.Core
{
    public interface IConfigurationProvider<T>
        where T : Entity, IRemotable, IPersistent, new()
    {
        string UserID { get; }

        T[] Load(Filter<T> filter);

        CoreTable Query(Filter<T> filter, Columns<T> columns, SortOrder<T>? sort = null);

        void Save(T entity);

        void Save(IEnumerable<T> entities);

        void Delete(T entity, Action<Exception?>? callback = null);
    }
    public class ClientConfigurationProvider<T> : IConfigurationProvider<T>
        where T : Entity, IRemotable, IPersistent, new()
    {
        public static ClientConfigurationProvider<T> Provider = new ClientConfigurationProvider<T>();

        public string UserID => ClientFactory.UserID;

        public T[] Load(Filter<T> filter)
        {
            return Client.Query(filter, null).ToArray<T>();
        }

        public CoreTable Query(Filter<T> filter, Columns<T> columns, SortOrder<T>? sort)
        {
            return Client.Query(filter, columns, sort);
        }

        public void Save(T entity)
        {
            Client.Save(entity, "", (o, e) => { });
        }

        public void Save(IEnumerable<T> entities)
        {
            Client.Save(entities, "", (o, e) => { });
        }

        public void Delete(T entity, Action<Exception?>? callback = null)
        {
            if(callback != null)
            {
                Client.Delete(entity, "", (o, e) => { callback(e); });
            }
            else
            {
                Client.Delete(entity, "");
            }
        }
    }
}
