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 AnswerDataManager(MyDbContext dbContext) : IAnswerManager<Answer>
    {

        private AnswerEntityManager manager = new AnswerEntityManager(dbContext);

        public async Task<Answer> addAnswer(Answer answer)
        {
            return await Task.FromResult<Answer>((await manager.addAnswer(answer.ToEntity())).ToModel());
        }

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

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

        public async Task<IEnumerable<Answer>?> getAnswersByIdQuestion(uint id)
        {
            var tmp = await manager.getAnswersByIdQuestion(id);
            List<Answer>? answer = new List<Answer>();
            if (tmp == null) answer = null;
            else
            {
                foreach (var item in tmp)
                {
                    answer.Add(item.ToModel());
                }
            }
            return await Task.FromResult<List<Answer>?>(answer);
        }

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

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

        public async Task<Answer?> removeAnswer(Answer answer)
        {
            return (await manager.removeAnswer(answer.ToEntity()))?.ToModel();
        }

        public async Task<Answer?> updateAnswer(uint id, Answer answer)
        {
            return (await manager.updateAnswer(id, answer.ToEntity()))?.ToModel();
        }
    }
}
