using domain.Models.ResponseModels;
using presence.data.LocalData.Entity;
using presence.data.RemoteData.RemoteDataBase.DAO;
using presence.data.Repository;
using presence.domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace presence.domain.UseCase
{
    public class UserUseCase
    {
        
        
        private readonly IUserRepository _repositoryUserImpl;
        private readonly IGroupRepository _repositoryGroupImpl;

        public UserUseCase(IUserRepository repositoryImpl, IGroupRepository repositoryGroupImpl)
        {
            _repositoryUserImpl = repositoryImpl;
            _repositoryGroupImpl = repositoryGroupImpl;
        }

      private List<Group> GetAllGroups() => _repositoryGroupImpl.GetAllGroup()
            .Select(it => new Group { Id  = it.Id, Name = it.Name}).ToList();
      public List<UserResponse> GetAllUsers() => _repositoryUserImpl.GetAllUser()
            .Join(_repositoryGroupImpl.GetAllGroup(),
            user => user.GroupId,
            group => group.Id,
            (user, group) => 
            new UserResponse { FIO = user.FIO, 
                Id = user.UserId, 
                Group = new GroupResponse{Id = group.Id, Name = group.Name}}
            ).ToList();
            

        public bool RemoveUserById(int userId) {
           return _repositoryUserImpl.RemoveUserById(userId);
        }
        public bool UpdateUser(User user) {
            UserDao userDao = new UserDao { FIO = user.FIO, GroupId = user.GroupId.Id};
            return _repositoryUserImpl.UpdateUser(userDao);
        }

        public User GetUserById(int userId) {
            return new User
            {
                FIO = _repositoryUserImpl.GetUserById(userId).FIO,
                Id = _repositoryUserImpl.GetUserById(userId).UserId,
                GroupId = new Group { Id = _repositoryUserImpl.GetUserById(userId).GroupId, Name = _repositoryGroupImpl.GetGroupById(userId).Name }
            };
        }

        public bool UpdateUserById(int userId, String fio, int groupId) {
            UserDao userDao = new UserDao { FIO = fio, GroupId = groupId};
            return _repositoryUserImpl.UpdateUser(userDao);
        }
    }
}