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 PlayerDataManager(MyDbContext dbContext) : IPlayerManager<Player>
    {
        private PlayerEntityManager manager = new PlayerEntityManager(dbContext);

        public async Task<Player> addPlayer(Player player)
        {
            return await Task.FromResult<Player>((await manager.addPlayer(player.ToEntity())).ToModel());
        }

        public async Task<uint?> getMaxScorePlayer(uint id, uint idChapter)
        {
            return await manager.getMaxScorePlayer(id, idChapter);
        }

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

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

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

        public async Task<Player?> getPlayer(string nickname)
        {
            return await Task.FromResult<Player?>((await manager.getPlayer(nickname))?.ToModel());
        }

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

        public async Task<IEnumerable<Player>?> getPlayersInALobby(uint idLobby)
        {
            List<Player>? tmp = new List<Player>();
            var res = await manager.getPlayersInALobby(idLobby);
            if (res == null) tmp = null;
            else
            {
                foreach (var item in res)
                {
                    tmp.Add(item.ToModel());
                }
            }
            return await Task.FromResult<IEnumerable<Player>?>(tmp);
        }

        public async Task<Player?> removePlayer(Player player)
        {
            return (await manager.removePlayer(player.ToEntity()))?.ToModel();
        }

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