using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using OnlineAssessment.Common;
using OnlineAssessment.Data.EFCore;
using OnlineAssessment.Domain.Models;
using OnlineAssessment.Domain.ViewModels;

namespace OnlineAssessment.Controllers
{
    [Route("v1/[controller]")]
    [Authorize]
    [ApiController]
    public class UsersController : BaseController<Users, EfCoreUserRepository>
    {
        EfCoreUserRepository _repository;
        string responseMessage;
        public UsersController(EfCoreUserRepository repository) : base(repository)
        {
            _repository = repository;
        }


        /// <summary>
        /// Get All Users
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public override IActionResult GetAll()
        {

            IActionResult returnResponse;
            dynamic userList = _repository.GetUsersList();
            if (userList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(userList));
            }
            return returnResponse;

        }


        /// <summary>
        /// Get an indivisual user details
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public override IActionResult Get(int id)
        {
            IActionResult returnResponse;
            dynamic entity =  _repository.GetUserById(id);
            if (entity == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotFound.ToString(), Constant.User);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            }
            return returnResponse;
        }

        /// <summary>
        /// User Log in 
        /// </summary>
        /// <param name="loginCredentials"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("[Action]")]
        public IActionResult SignIn([FromBody] UserLogin loginCredentials)
        {
            int returnCode = 0;
            IActionResult returnResponse;
            UserViewModel loggedOnUser = _repository.SignIn(loginCredentials, out returnCode);
            if (loggedOnUser != null && returnCode > 0)
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(loggedOnUser as dynamic));
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAdd.ToString());
                switch (returnCode)
                {
                    case (int)UserMessage.InvalidUser:
                        responseMessage = string.Concat(responseMessage, ". Reason: ", _repository.GetMessageByCode(UserMessage.InvalidUser.ToString()));
                        break;
                    case (int)UserMessage.InvalidPasword:
                        responseMessage = string.Concat(responseMessage, ". Reason: ", _repository.GetMessageByCode(UserMessage.InvalidPasword.ToString()));  
                        break;
                    case (int)UserMessage.UserNotActive:
                        responseMessage = string.Concat(responseMessage, ". Reason:", _repository.GetMessageByCode(UserMessage.UserNotActive.ToString()));
                        break;
                }
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
                 

            return returnResponse;
        }

        /// <summary>
        /// New User creation 
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult SignUp([FromBody] UserAddModel user)
        {
            int returnCode = 0;
            string returnMessage = string.Empty;
            IActionResult returnResponse;
            UserViewModel newUser = _repository.SignUp(user, out returnCode, out returnMessage);
            if (newUser != null)
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(newUser as dynamic));
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(),Constant.User);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage + " " + returnMessage));
            }

            return returnResponse;
        }

        [HttpPut("{id}")]
        public  IActionResult Put(int id, [FromBody] UserEditModel userEdit)
        {
            IActionResult returnResponse = null;
            string returnMessage = string.Empty;
            //UserEditModel userEdit = user.ToObject<UserEditModel>();
            if (id != userEdit.Id)
            {
                responseMessage = _repository.GetMessageByCode(Message.IdMismatchBetweenBodyAndQueryString.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                return returnResponse;
            }
            else
            {
                UserViewModel uvm = _repository.UpdateUser(id, userEdit, out returnMessage);
                if (uvm != null)
                {
                    returnResponse = Ok(ReturnResponse.GetSuccessStatus(uvm));
                }
                else
                {
                    responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(),Constant.User);

                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage + " " + returnMessage));
                }
            }
            return returnResponse;
        }

    }
}
