using Common.Dtos.Exper;
using Common.Dtos.Group;
using Hushian.Application.Constants;
using Hushian.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Hushian.WebApi.Controllers.v1
{
    [Route("api/v1/[controller]")]
    [ApiController]
    public class GroupController : ControllerBase
    {
        private readonly GroupService _groupService;
        private readonly ExperService _experService;
        public GroupController(GroupService groupService, ExperService experService)
        {
            _groupService = groupService;
            _experService = experService;
        }

        [HttpPost("AddGroup")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> AddGroup([FromBody] ADD_GroupDto Group)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _groupService.NewGroup(Group, CompanyID);
            return response.Success && response.Value ? NoContent()
                : BadRequest(response.Errors);
        }
        [HttpPut("UpdateGroup")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> UpdateGroup([FromBody] Update_GroupDto Group)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _groupService.UpdateGroup(Group, CompanyID);
            return response.Success && response.Value ? NoContent()
                : BadRequest(response.Errors);
        }
        [HttpPut("JoinExperToGroup/{GroupID}/{ExperID}")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> JoinExperToGroup(int GroupID, int ExperID)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _groupService.JoinExperInGroup(GroupID, ExperID, CompanyID);
            return response.Success && response.Value ? NoContent()
            : BadRequest(response.Errors);
        }
        [HttpPut("UnJoinExperToGroup/{GroupID}/{ExperID}")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> UnJoinExperToGroup(int GroupID, int ExperID)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _groupService.UnJoinExperInGroup(GroupID, ExperID, CompanyID);
            return response.Success && response.Value ? NoContent()
            : BadRequest(response.Errors);
        }
        [HttpDelete("DeleteGroup/{GroupID}")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> DeleteGroup(int GroupID)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _groupService.DeleteGroup(GroupID, CompanyID);
            return response.Success && response.Value ? NoContent()
                : BadRequest(response.Errors);
        }
        [HttpGet("GetGroupsCompany")]
        [Authorize]
        public async Task<ActionResult<List<Read_GroupDto>>> GetGroups(int? CompanyID)
        {

            if (!CompanyID.HasValue)
            {
                if (User.IsInRole("Exper"))
                {
                    string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                    int ExperID = Convert.ToInt32(strExperID);
                    CompanyID = await _experService.GetCompanyIDExper(ExperID);
                }
                else if (User.IsInRole("Company"))
                {
                    string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                    CompanyID = Convert.ToInt32(strCompanyID);


                }
            }
            var response = await _groupService.GetGroupsCompany(CompanyID.Value);
            return Ok(response);
        }
        [HttpGet("GetGroupsFromExperID")]
        [Authorize]
        public async Task<ActionResult<List<Read_GroupDto>>> GetGroups(int ExperID)
        {
            var response = await _groupService.GetGroupsExper(ExperID);
            return Ok(response);
        }
        [HttpGet("GetGroups")]
        [Authorize]
        public async Task<ActionResult<List<Read_GroupDto>>> GetGroupsAnon()
        {
            if (User.IsInRole("Exper"))
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                int ExperID = Convert.ToInt32(strExperID);
                var response = await _groupService.GetGroupsExper(ExperID);
                return Ok(response);
            }
            else if (User.IsInRole("Company"))
            {
                string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
               int CompanyID = Convert.ToInt32(strCompanyID);
                var response = await _groupService.GetGroupsCompany(CompanyID);
                return Ok(response);

            }
            return NotFound();
        }
        [HttpGet("GetExpersFromGroupID/{GroupID}")]
        public async Task<ActionResult<List<Read_ExperDto>>> GetExpersGroup(int GroupID)
        {
            var response = await _groupService.GetExpersFromGroupID(GroupID);
            return Ok(response);
        }
        //[HttpGet("GetOnlineExpersFromGroupID/{GroupID}")]
        //public async Task<ActionResult<AuthResponse>> GetOnlineExpersGroup(int GroupID)
        //{
        //    var CompanyID = await _companyService.GetCompanyID(User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First());
        //    var response = await _groupService.GetOnlineExpersGroup(GroupID, CompanyID.GetValueOrDefault());
        //    return response.Success ? Ok(response.Value)
        //        : BadRequest(response.Errors);
        //}
        [HttpPut("ChangeAvailableGroupFromManager/{GroupID}")] //تغییر وضعیت در دسترس بودن یا نبودن گروه' توسط مدیرش
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> ChangeAvailableExperFromManager(int GroupID, bool Available)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _groupService.ChangeAvailableGroup(new Common.Dtos.Exper.ChangeAvailable_GroupDto()
            { Available = Available, ID = GroupID }, CompanyID);
            return response ? NoContent()
                : BadRequest(new List<string> { "یافت نشد" });
        }

    }
}
