using System.Collections.Generic;
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 ExamTypesController : BaseController<ExamTypes, EFCoreExamTypeRepository>
    {
        EFCoreExamTypeRepository _repository;
        string responseMessage = string.Empty;
        public ExamTypesController(EFCoreExamTypeRepository repository) : base(repository)
        {
            _repository = repository;
        }

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

            //-------------------------------------------------------------------------------------
            List<ExamType> iList = _repository.GetListOfExamTypes();
            //-------------------------------------------------------------------------------------
            
            if (iList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(iList));
            }
            return returnResponse;

        }

        /// <summary>
        /// This endpoint will retrieve the active examtype by id (SU,A,T) but only SU can get inactive examtype.
        /// </summary>
        /// <returns></returns>
        [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));

            //-------------------------------------------------------------------------------------
            ExamType entity = _repository.GetExamTypeById(id);
            //-------------------------------------------------------------------------------------

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

        /// <summary>
        /// This endpoint will add a new examtype(SU).
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Authorize(Roles = "SuperAdmin")]
        public IActionResult AddExamType([FromBody] ExamTypeAddModel examtype)
        {
            IActionResult returnResponse;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            //-------------------------------------------------------------------------------------
            ExamType newExamType = _repository.AddExamType(user_id, examtype);
            //-------------------------------------------------------------------------------------

            if (newExamType.Id > 0) //Successfully Added
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(newExamType));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.ExamType);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            return returnResponse;
        }

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

            //-------------------------------------------------------------------------------------
            ExamType examType = _repository.UpdateExamType(user_id, id, theExamType);
            //-------------------------------------------------------------------------------------

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

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

            //-------------------------------------------------------------------------------------
            ExamType examtype = _repository.RestoreExamType(user_id, id);
            //-------------------------------------------------------------------------------------

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