using CloudBuilder.AI.Data;
using CloudBuilder.AI.Entity;
using CloudBuilder.AI.Policy;
using CloudBuilder.AI.SearchCriteria;
using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.Service;
using Microsoft.AspNetCore.Authorization;
namespace CloudBuilder.AI.Service
{
    public class AiChapterService : IAiChapterService
    {
        private readonly IApplicationService service;

        private IRepository<AiChapterEntity> repository;
        public AiChapterService(IApplicationService service)
        {
            this.service = service;
            repository = service.GetRepository<IRepository<AiChapterEntity>>();
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Find)]
        public AiChapterEntity FindByKey(string key)
        {
            return repository.DetachedEntities.Where(x => x.Guid == key).FirstOrDefault()!;
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Find)]
        public AiChapterEntity[] FindByTitle(string title)
        {

            IRepository<AiBookEntity> repositoryAiBookEntity = repository.GetRepository<IRepository<AiBookEntity>>();
            AiBookEntity aiBookEntity = repositoryAiBookEntity.DetachedEntities.Where(x => x.Title == title).FirstOrDefault()!;
            if (aiBookEntity == null) return null;

            return repository.DetachedEntities.Where(x => x.BookGuid == aiBookEntity.Guid).ToArray();
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Find)]
        public AiChapterResultData FindBySearchCriteria(AiChapterSearchCriteria criteria)
        {
            AiChapterResultData data = new AiChapterResultData();
            AiChapterEntity[] list = null;

            int count = 0;

            string sqlWhere = null;

            if (criteria.Filter != null && criteria.Filter.Length > 0)
            {
                sqlWhere = SearchFilter.ComposeWhereClause(criteria.Filter);
            }

            AiChapterEntity entity = AiChapterEntity.GetInstance();
            string sql = entity.GetSelectTopFrom(criteria.limit, criteria.page, criteria.orderby.StringToDbField(), criteria.orderbytag, sqlWhere);

            string err = null;
            try
            {
                list = repository.FromSql(sql).ToArray();
            }
            catch (System.Exception ex)
            {
                err = ex.Message;
            }


            if (list != null && list.Length > 0)
            {
                sql = entity.GetSelectCountFrom(sqlWhere);
                count = (int)repository.ExecuteScalar(sql);
            }

            data.Limit = criteria.limit;
            data.Page = criteria.page;
            data.Datas = list.OrderBy(x=>x.ChapterIndex).ToArray();
            data.Total = count;
            //data.Sql = sql;
            data.ErrorMessage = err;
            return data;
        }

        //[PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Print)]
        public dynamic PrintBySearchCriteria(AiChapterSearchCriteria criteria)
        {
            throw new NotImplementedException();
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Insert)]
        [UnitOfWork]
        public AiChapterEntity Insert(AiChapterEntity data)
        {
            throw new NotImplementedException();
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Update)]
        [UnitOfWork]
        public AiChapterEntity Update(AiChapterEntity data)
        {
            throw new NotImplementedException();
        }


        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Delete)]
        [UnitOfWork]
        public void Delete(string key)
        {
            var ent = repository.DetachedEntities.Where(x => x.Guid == key).FirstOrDefault()!;
            if (ent != null) { repository.DeleteNow(ent); }
        }
    }
}
