using System.Collections.Generic;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
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}")]
    public class InstitutesController : BaseController<Institutes, EFCoreInstituteRepository>
    {
        EFCoreInstituteRepository _repository;
        string responseMessage = string.Empty;

        public InstitutesController(EFCoreInstituteRepository repository) : base(repository)
        {
            _repository = repository;
        }

        #region Institute
        /// <summary>
        /// Get the detail of a institute
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        [Authorize(Roles = "SuperAdmin,Admin")]
        public IActionResult Get(int id)
        {
            IActionResult returnResponse;
            if (id != base.InstituteId)
            {
                responseMessage = _repository.GetMessageByCode(Message.NotAllowedToResource.ToString());
                returnResponse = Unauthorized(ReturnResponse.GetFailureStatus(responseMessage));
                return returnResponse;
            }
            dynamic entity = _repository.GetInstituteById(id);
            if (entity == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            }
            return returnResponse;
        }
        #endregion

        #region Classes
        /// <summary>
        /// Get class structure 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("Classes/{id}/Structure")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetClassesStructure(int id)
        {
            IActionResult returnResponse;

            ClassStructureViewModel structure = null;

            //-------------------------------------------------------------------------------------
            structure = _repository.GetClassStructurebyId(base.InstituteId, id);
            //-------------------------------------------------------------------------------------

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


        /// <summary>
        /// Get list of active classes (for the user's institution)
        /// </summary>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetClassesOfTheInstitution([FromQuery] string sortBy, string sortOrder)
        {
            IActionResult returnResponse;
            int role_id = int.Parse(Security.GetValueFromToken("RoleId", HttpContext.User.Identity as ClaimsIdentity));

            //-------------------------------------------------------------------------------------
            List<ClassViewModel> classList = _repository.GetClassesOfTheInstitution(base.InstituteId, sortBy, sortOrder);
            //-------------------------------------------------------------------------------------

            if (classList == null || classList.Count.Equals(0))
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(classList));
            }
            return returnResponse;
        }

        /// <summary>
        /// Get detail of a specific class of the Institution
        /// </summary>
        /// <param name="class_id"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetClassById(int class_id)
        {
            IActionResult returnResponse;

            ClassViewModel classvm = null;

            //-------------------------------------------------------------------------------------
            classvm = _repository.GetClassById(base.InstituteId, class_id);
            //-------------------------------------------------------------------------------------

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

        #region Subjects
        /// <summary>
        /// Get subjects of a given class of the institution
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="subject_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/Subjects")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetAllSubjectsOfTheClass(int class_id, [FromQuery] int subject_id, [FromQuery] string sortBy, string sortOrder)
        {
            IActionResult returnResponse;

            //-------------------------------------------------------------------------------------
            List<SubjectViewModel> theList = _repository.GetSubjectsOfTheClass(base.InstituteId, class_id, subject_id, sortBy, sortOrder);
            //-------------------------------------------------------------------------------------

            if (theList == null || theList.Count.Equals(0))
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(theList));
            }
            return returnResponse;
        }
        #endregion

        #region Categories
        /// <summary>
        /// Get active categories of a active subject
        /// </summary>
        /// <param name="subject_id"></param>
        /// <param name="category_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Subjects/{subject_id}/Categories")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetCategoriesOfTheSubject(int subject_id, [FromQuery] int category_id, [FromQuery] string sortBy, string sortOrder)
        {
            IActionResult returnResponse;

            //-------------------------------------------------------------------------------------
            List<CategoryViewModel> theList = _repository.GetCategoriesOfTheSubject(base.InstituteId, subject_id, category_id, sortBy, sortOrder);
            //-------------------------------------------------------------------------------------

            if (theList == null || theList.Count.Equals(0))
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }

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

        }
        #endregion

        #region Tags
        /// <summary>
        /// Get all tags
        /// </summary>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Tags")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetAllTagsOfTheInstitution([FromQuery] string sortBy, string sortOrder)
        {
            IActionResult returnResponse;
            List<TagViewModel> theList = _repository.GetTagsOfTheInstitute(base.InstituteId, sortBy, sortOrder);
            if (theList == null || theList.Count.Equals(0))
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(theList));
            }
            return returnResponse;
        }
        #endregion
    }
}
