using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using OnlineAssessment.Common;
using OnlineAssessment.Data.EFCore;
using OnlineAssessment.Domain.Models;
using OnlineAssessment.Domain.ViewModels;

namespace OnlineAssessment.V1.Controllers
{
    [ApiController]
    [ApiVersion("1.0")]
    [Route("v{version:apiVersion}/[controller]")]
    public class RolesController : BaseController<Roles, EFCoreRoleRepository>
    {
        EFCoreRoleRepository _repository;
        string responseMessage = string.Empty;
        public RolesController(EFCoreRoleRepository repository) : base(repository)
        {
            _repository = repository;
        }


        /// <summary>
        /// This endpoint will retrieve all active roles(SU,A,T,S).
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Authorize(Roles = "SuperAdmin,Admin,Teacher")]
        public override IActionResult GetAll()
        {
            IActionResult returnResponse;

            //-------------------------------------------------------------------------------------
            List<Role> iList = _repository.GetListOfRoles();
            //-------------------------------------------------------------------------------------

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

        }

        [HttpGet("{id}")]
        [Authorize(Roles = "SuperAdmin,Admin,Teacher")]
        public override IActionResult Get(int id)
        {
            IActionResult returnResponse;
            int role_id = int.Parse(Security.GetValueFromToken("RoleId", HttpContext.User.Identity as ClaimsIdentity));

            //-------------------------------------------------------------------------------------
            Role entity = _repository.GetRoleById(id);
            //-------------------------------------------------------------------------------------

            //Only superadmin can retrive deleted roles
            if (entity == null || (entity.IsActive == false && role_id != 1))
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotFound.ToString(), Constant.Role);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));

            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            }
            return returnResponse;
        }

        /// <summary>
        /// This endpoint will add a new role(SU).
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "SuperAdmin")]
        public async Task<IActionResult> AddRole([FromBody] RoleAddModel role)
        {
            //Debug.WriteLine("PostRole controller started " + DateTime.Now.ToLongTimeString());

            IActionResult returnResponse;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            //-------------------------------------------------------------------------------------
            Role newRole = await _repository.AddRole(user_id, role);
            //-------------------------------------------------------------------------------------

            if (newRole != null && newRole.Id > 0) //Successfully Added
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(newRole));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Role);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            //Debug.WriteLine("PostRole controller closed " + DateTime.Now.ToLongTimeString());

            return returnResponse;
        }

        /// <summary>
        /// This endpoint will update the role(SU).
        /// </summary>
        /// <returns></returns>
        [HttpPost("{id}/Update")]
        [Authorize(Roles = "SuperAdmin")]
        public IActionResult UpdateRole(int id, [FromBody] RoleEditModel theRole)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            //-------------------------------------------------------------------------------------
            Role role = _repository.UpdateRole(user_id, id, theRole);
            //-------------------------------------------------------------------------------------

            if (role == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Role);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(role));
            }
            return returnResponse;
        }

        /// <summary>
        /// This endpoint will restore the deleted role(SU).
        /// </summary>
        /// <returns></returns>
        [HttpPost("{id}/Restore")]
        [Authorize(Roles = "SuperAdmin")]
        public IActionResult RestoreRole(int id)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            //-------------------------------------------------------------------------------------
            Role role = _repository.RestoreRole(user_id, id);
            //-------------------------------------------------------------------------------------

            if (role == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Role);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(role));
            }
            return returnResponse;
        }
    }
}
