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 QuestionTypesController : BaseController<QuestionTypes, EFCoreQuestionTypeRepository>
    {
        EFCoreQuestionTypeRepository _repository;
        string responseMessage = string.Empty;
        public QuestionTypesController(EFCoreQuestionTypeRepository repository) : base(repository)
        {
            _repository = repository;
        }

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

            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 questiontype by id (SU,A,T,S) but only SU can get inactive questiontype.
        /// </summary>
        [HttpGet("{id}")]
        [Authorize(Roles = "SuperAdmin,Admin,Teacher,Student")]
        public override IActionResult Get(int id)
        {
            IActionResult returnResponse;
            int role_id = int.Parse(Security.GetValueFromToken("RoleId", HttpContext.User.Identity as ClaimsIdentity));

            //-------------------------------------------------------------------------------------
            QuestionType entity = _repository.GetQuestionTypeById(id);
            //-------------------------------------------------------------------------------------

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

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

            //-------------------------------------------------------------------------------------
            QuestionType newQuestionType = _repository.AddQuestionType(user_id, qnstype);
            //-------------------------------------------------------------------------------------

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

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

            //-------------------------------------------------------------------------------------
            QuestionType questionType = _repository.UpdateQuestionType(user_id, id, theQnsType);
            //-------------------------------------------------------------------------------------

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

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

            //-------------------------------------------------------------------------------------
            QuestionType qnstype = _repository.RestoreQuestionType(user_id, id);
            //-------------------------------------------------------------------------------------

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