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

        public async Task<PlayerDto> addPlayer(PlayerDto playerdto)
        {
            return await Task.FromResult<PlayerDto>((await manager.addPlayer(playerdto.ToModel())).ToDto());
        }

        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<PlayerDto?> getPlayer(uint id)
        {
            return await Task.FromResult<PlayerDto?>((await manager.getPlayer(id))?.ToDto());
        }

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

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

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

        public async Task<PlayerDto?> removePlayer(PlayerDto playerdto)
        {
            return (await manager.removePlayer(playerdto.ToModel()))?.ToDto();
        }

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