using Common.Dtos.Exper;
using Common.Dtos.Group;
using System.Net.Http.Json;

namespace HushianWebApp.Service
{
    public class GroupService
    {
        private readonly BaseController _baseController;
        const string BaseRoute = "v1/Group/";

        public GroupService(BaseController baseController)
        {
            _baseController = baseController;
        }
       public async Task<bool> AddGroup(ADD_GroupDto Group)
        {
            var response = await _baseController.Post($"{BaseRoute}AddGroup",Group);
            return response.IsSuccessStatusCode;
        }

        public async Task<bool> UpdateGroup(Update_GroupDto Group)
        {
            var response = await _baseController.Put($"{BaseRoute}UpdateGroup", Group);
            return response.IsSuccessStatusCode;
        }
        public async Task<bool> JoinExperToGroup(int GroupID, int ExperID)
        {
            var response = await _baseController.Put($"{BaseRoute}JoinExperToGroup/{GroupID}/{ExperID}");
            return response.IsSuccessStatusCode;
        }
        public async Task<bool> UnJoinExperToGroup(int GroupID, int ExperID)
        {
            var response = await _baseController.Put($"{BaseRoute}UnJoinExperToGroup/{GroupID}/{ExperID}");
            return response.IsSuccessStatusCode;
        }
        public async Task<bool> DeleteGroupFromManager(int GroupID)
        {
            var response = await _baseController.Delete ($"{BaseRoute}DeleteGroup/{GroupID}");
            return response.IsSuccessStatusCode;
        }
        public async Task<List<Read_GroupDto>?> GetGroups()
        {
            var response = await _baseController.Get($"{BaseRoute}GetGroups");
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadFromJsonAsync<List<Read_GroupDto>>();
            }
            return null;
        }
        public async Task<List<Read_GroupDto>?> GetGroupsCompany()
        {
            var response = await _baseController.Get($"{BaseRoute}GetGroupsCompany");
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadFromJsonAsync<List<Read_GroupDto>>();
            }
            return null;
        }
        public async Task<List<Read_GroupDto>> GetGroupsCompany(int CompanyID)
        {
            var response = await _baseController.Get($"{BaseRoute}GetGroupsCompany?CompanyID={CompanyID}");
            if (response.IsSuccessStatusCode)
            {
                var data= await response.Content.ReadFromJsonAsync<List<Read_GroupDto>>();
                return data==null ?new() : data;
            }
            return new();
        }
        public async Task<List<Read_GroupDto>?> GetGroupsFromExperID(int ExperID)
        {
            var response = await _baseController.Get($"{BaseRoute}GetGroupsFromExperID?ExperID={ExperID}");
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadFromJsonAsync<List<Read_GroupDto>>();
            }
            return new();
        }
        public async Task<List<Read_ExperDto>> GetExpersFromGroupID(int GroupID)
        {
            var response = await _baseController.Get($"{BaseRoute}GetExpersFromGroupID/{GroupID}");
            if (response.IsSuccessStatusCode)
            {
                return await response.Content.ReadFromJsonAsync<List<Read_ExperDto>>();
            }
            return new();
        }
        public async Task<bool> ChangeAvailableGroupFromManager(int GroupID, bool Available)
        {
            var response = await _baseController.Put($"{BaseRoute}ChangeAvailableGroupFromManager/{GroupID}?Available={Available}");
            return response.IsSuccessStatusCode;
        }

        //public async Task<List<HushianUserDto>?> GetOnlineExpersFromGroupID(int GroupID)
        //{
        //    var response = await _baseController.Get($"{BaseRoute}GetOnlineExpersFromGroupID/{GroupID}");
        //    if (response.IsSuccessStatusCode)
        //    {
        //        return await response.Content.ReadFromJsonAsync<List<HushianUserDto>>();
        //    }
        //    return null;
        //}
    }
}
