using System.Collections.Generic;

using Newtonsoft.Json.Linq;

using Vit.AutoTemp.DataProvider;
using Vit.Core.Module.Serialization;
using Vit.Core.Util.ComponentModel.Data;
using Vit.Extensions.Serialize_Extensions;
using Vit.Linq.ComponentModel;
using Vit.Linq.FilterRules.ComponentModel;


namespace Vit.AutoTemp.Repository
{
    public class RespositoryDataProvider<T> : IDataProvider
    {

        public IRepository<T> repository { get; private set; }

        public RespositoryDataProvider(IRepository<T> repository, string template, AutoTempControllerConfig controllerConfig = null)
        {
            this.template = template;
            this.repository = repository;

            this.controllerConfig = controllerConfig ?? AutoTempHelp.BuildControllerConfigByEntityType(typeof(T));
        }

        readonly AutoTempControllerConfig controllerConfig;

        public string template { get; private set; }
        public ApiReturn<AutoTempControllerConfig> getControllerConfig(object sender) => controllerConfig;


        public ApiReturn delete(object sender, JObject arg)
        {
            return repository.Delete(repository.GetModel(arg["id"].Value<string>()).data);
        }

        public ApiReturn getList(object sender, FilterRule filter, IEnumerable<OrderField> sort, PageInfo page, JObject arg)
        {
            return repository.GetList(filter, sort, page);
        }
        public ApiReturn getModel(object sender, string id)
        {
            return repository.GetModel(id);
        }
        public ApiReturn insert(object sender, JObject model)
        {
            return repository.Insert(model.ConvertBySerialize<T>());
        }
        public ApiReturn update(object sender, JObject model, string id)
        {
            return repository.Update(model.ConvertBySerialize<T>(), id);
        }
    }
}