using DbConnectionLibrairie;
using EntityManagers;
using ExtensionsClassLibrairie;
using ManagerInterfaces;
using Model;
using OrderCriterias;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataManagers
{
    public class ChapterDataManager(MyDbContext dbContext) : IChapterManager<Chapter>
    {
        private ChapterEntityManager manager = new ChapterEntityManager(dbContext);
        
        public async Task<Chapter> addChapter(Chapter chapter)
        {
            return await Task.FromResult<Chapter>((await manager.addChapter(chapter.ToEntity())).ToModel());
        }

        public async Task<Chapter?> getChapter(uint id)
        {
            return await Task.FromResult<Chapter?>((await manager.getChapter(id))?.ToModel());
        }

        public async Task<(int nbPages, IEnumerable<Chapter>? chapters)> getChapters(int nb, int count, ChapterOrderCriteria orderCriteria = ChapterOrderCriteria.ById)
        {
            List<Chapter>? tmp = new List<Chapter>();
            var res = await manager.getChapters(nb, count, orderCriteria);
            if (res.chapters == null) tmp = null;
            else
            {
                foreach (var item in res.chapters)
                {
                    tmp.Add(item.ToModel());
                }
            }
            return await Task.FromResult<(int nbPages, IEnumerable<Chapter>? chapters)>((res.nbPages, tmp));
        }

        public int getNbElements()
        {
            return manager.getNbElements();
        }

        public async Task<Chapter?> removeChapter(Chapter chapter)
        {
            return (await manager.removeChapter(chapter.ToEntity()))?.ToModel();
        }

        public async Task<Chapter?> removeChapter(uint id)
        {
            return (await manager.removeChapter(id))?.ToModel();
        }
    }
}
