
using Azure;
using Common.Dtos;
using Common.Dtos.Exper;
using Hushian.Application.Constants;
using Hushian.Application.Services;
using Hushian.Domain.Entites;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.Design;
using System.IdentityModel.Tokens.Jwt;

namespace Hushian.WebApi.Controllers.v1
{
    [Route("api/v1/[controller]")]
    [ApiController]
    public class ExperController : ControllerBase
    {
        private readonly ExperService _experService;

        public ExperController(ExperService experService)
        {
            _experService = experService;
        }

        [HttpPost("AddExper")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> AddExper([FromBody] ADD_ExperDto userDto)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);
            var response = await _experService.ADDExper(userDto, CompanyID);
            return response.Success ? NoContent()
             : BadRequest(response.Errors);
        }
        [HttpGet("GetExpersCompany")]
        [Authorize]
        public async Task<ActionResult> GetExpersCompany(int? CompanyID)
        {
            if (!CompanyID.HasValue)
            {
                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 _experService.GetExpersInCompany(CompanyID.GetValueOrDefault());
            return Ok(response);

        }

        [HttpPut("EditUserYourself")] //ویرایش کاربران توسط خود
        [Authorize(Roles = "Exper")]
        public async Task<ActionResult> EditUserYourself([FromBody] Update_ExperDto editUser)
        {
            string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int ExperID = Convert.ToInt32(strExperID);

            var response = await _experService.UpdateExper(editUser, ExperID);
            return response ? NoContent()
                : BadRequest(new List<string> { "یافت نشد" });
        }
        [HttpGet("GetCurrentExper")]
        [Authorize(Roles = "Exper")]
        public async Task<ActionResult> GetCurrentUser()
        {
            string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int ExperID = Convert.ToInt32(strExperID);

            var response = await _experService.GetInfoExper(ExperID);
            return response != null ? Ok(response) : BadRequest(new List<string> { "یافت نشد" });
        }
        [HttpGet("GetCurrentUserInfo")]
        [Authorize(Roles = "Exper,Company,User")]
        public async Task<ActionResult> GetCurrentUserInfo()
        {
            int CompanyID = 0;
            int? ExperID = null;
            int? UserID = null;
            if (User.IsInRole("Exper"))
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                 ExperID = Convert.ToInt32(strExperID);

            }
            else if (User.IsInRole("Company"))
            {
                string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                CompanyID = Convert.ToInt32(strCompanyID);
            }
            else if (User.IsInRole("User"))
            {
                string strUserID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                 UserID = Convert.ToInt32(strUserID);
            }

            var response = await _experService.GetCurrentUserInfo(CompanyID,ExperID,UserID);
            return response != null ? Ok(response) : BadRequest(new List<string> { "یافت نشد" });
        }
        [HttpPut("ExperEditingFromManager/{ExperID}")] //ویرایش کارشناس توسط مدیرش
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> ExperEditingFromManager(int ExperID, [FromBody] Update_ExperDto editUser)
        {
            var response = await _experService.UpdateExper(editUser, ExperID);
            return response ? NoContent()
                : BadRequest(new List<string> { "یافت نشد" });
        }
        //---
        [HttpPut("ChangePasswordYourself")] //تغییر کلمه عبور  کاربران توسط خود
        [Authorize(Roles = "Exper")]
        public async Task<ActionResult> ChangePasswordYourself([FromBody] ChangePasswordDto item)
        {
            string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int ExperID = Convert.ToInt32(strExperID);

            var response = await _experService.ChangePasswordExperFromExper(item, ExperID);
            return response.Success && response.Value ? NoContent()
                : BadRequest(response.Errors);
        }
        [HttpPut("ChangePasswordFromManager/{ExperID}")] //تغییر کلمه عبور کارشناس توسط مدیرش
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> ChangePasswordFromManager(int ExperID, [FromBody] ChangePasswordDto item)
        {
            string strCompanyID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
            int CompanyID = Convert.ToInt32(strCompanyID);

            var response = await _experService.ChangePasswordExperFromCompanyManaget(item, ExperID, CompanyID);
            return response.Success && response.Value ? NoContent()
                : BadRequest(response.Errors);
        }
        [HttpPut("ChangeAvailableExperFromManager/{ExperID}")] //تغییر وضعیت در دسترس بودن یا نبودن کارشناس توسط مدیرش
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> ChangeAvailableExperFromManager(int ExperID, 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 _experService.ChangeAvailableExper(ExperID, CompanyID, Available);
            return response ? NoContent()
                         : BadRequest(new List<string> { "یافت نشد" });
        }
        [HttpDelete("DeleteExperFromManager/{ExperID}")]
        [Authorize(Roles = "Company")]
        public async Task<ActionResult> DeleteExperFromManager(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 _experService.DeleteExper(ExperID, CompanyID);
            return response ? NoContent()
                         : BadRequest(new List<string> { "یافت نشد" });
        }
        [HttpGet("CheckAvailableExperInCompany")]
        public async Task<ActionResult> CheckAvailableExper(int? ExperID)
        {
            if (!ExperID.HasValue)
            {
                string strExperID = User.Claims.Where(w => w.Type == CustomClaimTypes.Uid).Select(s => s.Value).First();
                ExperID = Convert.ToInt32(strExperID);
            }

            var response = await _experService.AvailableExperInCompany(ExperID.GetValueOrDefault());
            return response ? NoContent()
                : BadRequest();

        }


    }
}
