using DbConnectionLibrairie;
using Entities;
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 QuestionDataManager(MyDbContext dbContext) : IQuestionManager<Question>
    {

        private QuestionEntityManager manager = new QuestionEntityManager(dbContext);

        public async Task<Question> addQuestion(Question question)
        {
            return await Task.FromResult<Question>((await manager.addQuestion(question.ToEntity())).ToModel());
        }

        public async Task<IEnumerable<Question>> addQuestions(IEnumerable<Question> questions)
        {
            var tmp = new List<QuestionEntity>();
            foreach (var question in questions)
            {
                tmp.Add(question.ToEntity());
            }
            var res = await manager.addQuestions(tmp);
            var tmp2 = new List<Question>();
            foreach (var item in res)
            {
                tmp2.Add(item.ToModel());
            }
            return tmp2;
        }

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

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

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

        public async Task<(int nbPages, IEnumerable<Question>? questions)?> getQuestionsByChapterAndDifficulty(int idChapter, int difficulty, int nb, int count, QuestionOrderCriteria orderCriteria = QuestionOrderCriteria.ById)
        {
            List<Question>? tmp = new List<Question>();
            var res = await manager.getQuestionsByChapterAndDifficulty(idChapter, difficulty, nb, count, orderCriteria);
            if (res == null) return await Task.FromResult<(int nbPages, IEnumerable<Question>? questions)?>(null);
            if (res.Value.questions == null) tmp = null;
            else
            {
                foreach (var item in res.Value.questions)
                {
                    tmp.Add(item.ToModel());
                }
            }
            return await Task.FromResult<(int nbPages, IEnumerable<Question>? questions)>((res.Value.nbPages, tmp));
        }

        public async Task<Question?> removeQuestion(Question question)
        {
            return (await manager.removeQuestion(question.ToEntity()))?.ToModel();
        }

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

        public async Task<Question?> updateQuestion(uint id, Question question)
        {
            return (await manager.updateQuestion(id, question.ToEntity()))?.ToModel();
        }

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