using Back.Data.Models;
using Back.Services;
using Back.Validations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Shared.DTOs;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace Back.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class VerificationController : ControllerBase
    {
        private readonly ServValidatinMsg _servValidatinMsg;
        private readonly GetVerificationValidation _getVerificationValidation;
        private readonly servSendMsg _servSendMsg;
        private readonly servCompany _servCompany;
        private readonly servUser _servUser;
        public VerificationController(ServValidatinMsg servValidatinMsg, GetVerificationValidation getVerificationValidation
            , servCompany servCompany, servSendMsg servSendMsg, servUser servUser)
        {
            _servValidatinMsg = servValidatinMsg;
            _getVerificationValidation = getVerificationValidation;
            _servCompany = servCompany;
            _servSendMsg = servSendMsg;
            _servUser = servUser;
        }
        [HttpGet("GetVerification/{ID}")]

        public async Task<ActionResult<VerificationCode>> GetVerification(int ID)
        {
            var resultValidationmodel = await _getVerificationValidation.ValidateAsync(ID);
            if (!resultValidationmodel.IsValid)
                return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());

            return Ok(new VerificationCodeDto { 
                ID = ID,
                Code=0,
            prm= _getVerificationValidation.verificationCode.prm,
            Type= _getVerificationValidation.verificationCode.Type,
            val= _getVerificationValidation.verificationCode.val
            });;
        }
        [HttpGet("ReSend/{ID}")]

        public async Task<ActionResult> ReSend(int ID)
        {
            var resultValidationmodel = await _getVerificationValidation.ValidateAsync(ID);
            if (!resultValidationmodel.IsValid)
                return BadRequest(resultValidationmodel.Errors.Select(s => s.ErrorMessage).ToList());
            // _getVerificationValidation.verificationCode

            switch (_getVerificationValidation.verificationCode.Type)
            {
                case "NewTicketNoAuthentication":
                    _servSendMsg.toContinue(_getVerificationValidation.verificationCode.Code.ToString(), _getVerificationValidation.verificationCode.val,"ثبت نظر");
                    break;

                case "CompanyRegistration":
                   var company=await _servCompany.GetCompanyOrg(Convert.ToInt32(_getVerificationValidation.verificationCode.prm),false);
                    _servSendMsg.toContinue(_getVerificationValidation.verificationCode.Code.ToString(),company.Mobile, "ثبت نام");
                    break;


                case "ForgetPassword":
                   // var user = await _servUser.ChangePasswordByMobile(_getVerificationValidation.verificationCode.prm, _getVerificationValidation.verificationCode.val);
                    _servSendMsg.toContinue(_getVerificationValidation.verificationCode.Code.ToString(), _getVerificationValidation.verificationCode.prm, "فراموشی کلمه عبور");
                    break;


                case "ChangeUserName":
                    _servSendMsg.toContinue(_getVerificationValidation.verificationCode.Code.ToString(), _getVerificationValidation.verificationCode.val, "تغییر نام کاربری");
                    break;


                default:
                    return BadRequest(new List<string> { "این نوع احراز تعریف نشده" });
            }

            return NoContent();
        }
        [HttpPost("Submit")]

        public async Task<ActionResult<bool>> Submit(VerificationCodeDto item)
        {
           var VerificationCode= await _servValidatinMsg.GetVerificationCodeByID(item.ID);
            if (VerificationCode==null)
                return NotFound("آیتمی یافت نشد");

            if (VerificationCode.Code==item.Code)
            {
                if (VerificationCode.prm == item.prm && VerificationCode.val == item.val && VerificationCode.Type == item.Type)
                {
                    bool Sucstatus = false;
                    switch (VerificationCode.Type)
                    {
                        case "NewTicketNoAuthentication":
                            Sucstatus = await _servValidatinMsg.SubmittedTicket(VerificationCode);
                            break;

                        case "CompanyRegistration":
                            //string UserID = VerificationCode.val;
                            Sucstatus = await _servValidatinMsg.SubmittedCompanyRegistration(VerificationCode);
                            //    return Ok(await _servUser.UserAuthentication(UserID));
                            //else return BadRequest();
                            break;

                        case "ForgetPassword":
                            Sucstatus = await _servUser.ChangePasswordByMobile(VerificationCode.prm, VerificationCode.val);
             
                            break;

                        case "ChangeUserName":
                            Sucstatus = await _servUser.ChangeUserName(VerificationCode.val, Convert.ToInt32(VerificationCode.prm));
                            break;

                        default:
                          return BadRequest(new List<string> { "این نوع احراز تعریف نشده" });
                    }

                    if (Sucstatus)
                        await _servValidatinMsg.Delete(VerificationCode);

                    return Ok(Sucstatus);
                }
                 else return BadRequest(new List<string> { "اطلاعات شما منطبق با سامانه نیست" });
                
            }
            else return BadRequest(new List<string> { "کد احراز صحیح نمی باشد" });
          
        }
        [HttpDelete("Remove/{ID}")]

        public async Task<ActionResult<bool>> Remove(int ID)
        {
            var VerificationCode = await _servValidatinMsg.GetVerificationCodeByID(ID);
            await _servValidatinMsg.Delete(VerificationCode);
            return NoContent();
        }
    }
}
