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

        public async Task<Lobby> addLobby(Lobby lobby)
        {
            return await Task.FromResult<Lobby>((await manager.addLobby(lobby.ToEntity())).ToModel());
        }

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

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

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

        public async Task<Lobby?> removeLobby(Lobby lobby)
        {
            return (await manager.removeLobby(lobby.ToEntity()))?.ToModel();
        }

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