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

namespace ServiceManagers
{
    public class ChapterServiceManager(MyDbContext dbContext) : IChapterManager<ChapterDto>
    {
        private ChapterDataManager manager = new ChapterDataManager(dbContext);
        
        public async Task<ChapterDto> addChapter(ChapterDto chapterdto)
        {
            return await Task.FromResult<ChapterDto>((await manager.addChapter(chapterdto.ToModel())).ToDto());
        }

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

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

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

        public async Task<ChapterDto?> removeChapter(ChapterDto chapterdto)
        {
            return (await manager.removeChapter(chapterdto.ToModel()))?.ToDto();
        }

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