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 CloudBuilder.Security.Data;
using CloudBuilder.Security.Entity;
using Microsoft.AspNetCore.Authorization;
namespace CloudBuilder.AI.Service
{
    public class AiSentenceService : IAiSentenceService
    {
        private readonly IApplicationService service;

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

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


        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Find)]
        public AiSentenceViewEntity[] FindByChapterGuid(string chapterGuid)
        {
            IRepository<AiSentenceViewEntity> repositoryAiSentenceViewEntity = repository.GetRepository<IRepository<AiSentenceViewEntity>>();

            return repositoryAiSentenceViewEntity.DetachedEntities.Where(x => x.ChapterGuid == chapterGuid).OrderBy(x => x.ParagraphIndex).ThenBy(x=>x.SentenceIndex).ToArray();
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Find)]
        public AiSentenceViewResultData FindBySearchCriteria(AiSentenceSearchCriteria criteria)
        {
            AiSentenceViewResultData data = new AiSentenceViewResultData();
            AiSentenceViewEntity[] list = null;
            IRepository<AiSentenceViewEntity> repositoryAiSentenceViewEntity = repository.GetRepository<IRepository<AiSentenceViewEntity>>();

            int count = 0;

            string sqlWhere = null;

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

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

            string err = null;
            try
            {
                list = repositoryAiSentenceViewEntity.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;
            data.Total = count;
            //data.Sql = sql;
            data.ErrorMessage = err;
            return data;
        }

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

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

        [PermissionOperate(name: AIPermissionPolicy.AiParagraph, operate: Operate.Insert)]
        [UnitOfWork]
        public void Inserts(AiSentenceEntity[] datas)
        {
            repository.InsertNow(datas);
        }

        [PermissionOperate(name: AIPermissionPolicy.AiBook, operate: Operate.Update)]
        [UnitOfWork]
        public AiSentenceEntity Update(AiSentenceEntity 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); }
        }
    }
}
