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 LobbyServiceManager(MyDbContext dbContext) : ILobbyManager<LobbyDto>
    {
        private LobbyDataManager manager = new LobbyDataManager(dbContext);

        public async Task<LobbyDto> addLobby(LobbyDto lobbydto)
        {
            return await Task.FromResult<LobbyDto>((await manager.addLobby(lobbydto.ToModel())).ToDto());
        }

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

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

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

        public async Task<LobbyDto?> removeLobby(LobbyDto lobbydto)
        {
            return (await manager.removeLobby(lobbydto.ToModel()))?.ToDto();
        }

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