using System;
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
{
    [Authorize]
    [ApiVersion("1.0")]
    [Route("v{version:apiVersion}")]
    public class ExamsController : BaseController<Exams, EFCoreExamRepository>
    {
        EFCoreExamRepository _repository;
        string responseMessage = string.Empty;
        public ExamsController(EFCoreExamRepository repository) : base(repository)
        {
            _repository = repository;
        }
        
        #region Exams 
        /// <summary>
        /// Add new exam 
        /// </summary>
        /// <param name="language"></param>
        /// <param name="class_id"></param>
        /// <param name="newExam"></param>
        /// <returns></returns>
        [HttpPost("{language}/Classes/{class_id}/Exams")]
        [Authorize(Roles = "Admin")]
        public IActionResult AddNewExam(string language, int class_id, [FromBody] ExamAddModel newExam)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            int language_id = _repository.GetLanguageIdByCode(language);

            string return_message = string.Empty;
            if ((!(ModelState.IsValid)) || (base.InstituteId <= 0) || (language_id <= 0))
            {
                responseMessage = _repository.GetMessageByCode(Message.InvalidInput.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }

            else
            {
                ExamViewModel exam = _repository.AddNewExam(base.InstituteId, language_id, class_id, user_id, newExam, out return_message);
                if (exam == null)
                {
                    responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Institute);
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
                else
                {
                    returnResponse = Ok(ReturnResponse.GetSuccessStatus(exam));
                }
            }
            return returnResponse;
        }

        /// <summary>
        /// Add new exam section
        /// </summary>
        /// <param name="exam_id"></param>
        /// <param name="sectionIdList"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/Sections")]
        [Authorize(Roles = "Admin")]
        public IActionResult AddNewExamSections(int exam_id, [FromBody] IntegerSectionList sectionIdList)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (sectionIdList == null || sectionIdList.idList == null || sectionIdList.idList.Count == 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.InvalidInput.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }

            //TODO: check if works fine
            IntegerSectionList sectionsAdded = _repository.AddNewExamSections(base.InstituteId, exam_id, user_id, sectionIdList, out return_message);
            if (sectionsAdded == null || sectionsAdded.idList == null || sectionsAdded.idList.Count == 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAttach.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(sectionsAdded));
            }

            return returnResponse;
        }


        /// <summary>
        /// Arrange sections of an given exam
        /// </summary>
        /// <param name="exam_id"></param>
        /// <param name="examSectionList"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/ArrangeSections")]
        [Authorize(Roles = "Admin")]
        public IActionResult ReorderExamSectionOfTheExam(int exam_id, [FromBody] ExamSectionsList examSectionList)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);


            IntegerSectionList sectionsAdded = _repository.ReorderExamSectionOfTheExam(base.InstituteId, user_id, exam_id, examSectionList);
            if (sectionsAdded == null || sectionsAdded.idList == null || sectionsAdded.idList.Count == 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.StudyNote);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(exam_id));
            }
            return returnResponse;
        }
        
        /// <summary>
        /// Attach Questions To Exam Sections
        /// </summary>
        /// <param name="exam_section_id"></param>
        /// <param name="questionIdList"></param>
        /// <returns></returns>
        [HttpPost("ExamSections/{exam_section_id}/AttachQuestions")]
        [Authorize(Roles = "Admin")]
        public IActionResult AttachQuestionsToExamSections(int exam_section_id, [FromBody] QuestionsList questionIdList)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (questionIdList == null || questionIdList.idList == null || questionIdList.idList.Count == 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.InvalidInput.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                int recordsEffected = _repository.AttachQuestionsToExamSections(base.InstituteId, -1, exam_section_id, questionIdList, out return_message);
                if (recordsEffected == 0)
                {
                    responseMessage = _repository.GetMessageByCode(Message.FailedToAttach.ToString());
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
                else
                {
                    returnResponse = Ok(ReturnResponse.GetSuccessStatus(return_message));
                }
            }
            return returnResponse;
        }

        /// <summary>
        /// Detach Questions from Exam Section
        /// </summary>
        /// <param name="exam_section_id"></param>
        /// <param name="questionIdList"></param>
        /// <returns></returns>
        [HttpPost("ExamSections/{exam_section_id}/DetachQuestions")]
        [Authorize(Roles = "Admin")]
        public IActionResult DetachExamSectionFromQuestions(int exam_section_id, [FromBody] QuestionsList questionIdList)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            if (questionIdList == null || questionIdList.idList == null || questionIdList.idList.Count == 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.InvalidInput.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                int recordsEffected = _repository.DetachExamSectionFromQuestions(base.InstituteId, exam_section_id, questionIdList, out return_message);
                if (recordsEffected < 0)
                {
                    responseMessage = _repository.GetMessageByCode(Message.FailedToDetach.ToString());
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
                else
                {
                    returnResponse = Ok(ReturnResponse.GetSuccessStatus(return_message));
                }
            }
            return returnResponse;
        }

        /// <summary>
        /// Mark Questions OfTheExamSection
        /// </summary>
        /// <param name="exam_section_id"></param>
        /// <param name="questionList"></param>
        /// <returns></returns>
        [HttpPost("ExamSections/{exam_section_id}/MarkQuestions")]
        [Authorize(Roles = "Admin")]
        public IActionResult AssignMarksToExamSectionQuestions(int exam_section_id, [FromBody] QuestionMarksList questionList)
        {
            IActionResult returnResponse = null;

            ExamSectionViewModel examsection = _repository.AssignMarksToExamSection(base.InstituteId, exam_section_id, questionList);
            if (examsection == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.StudyNote);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(examsection));
            }
            return returnResponse;
        }

        /// <summary>
        /// Publish Exam  
        /// </summary>
        /// <param name="language"></param>
        /// <param name="exam_id"></param>
        /// <param name="scheduleExam"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/Publish")]
        [Authorize(Roles = "Admin")]
        public IActionResult PublishExam(string language, int exam_id, [FromBody] ExamPublishModel scheduleExam)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            ExamViewAllModel exam = _repository.PublishExam(base.InstituteId, user_id, exam_id, scheduleExam);
            if (exam == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.StudyNote);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(exam));
            }
            return returnResponse;
        }


        /// <summary>
        /// Get exam details 
        /// </summary>
        /// <param name="exam_id"></param>
        /// <returns></returns>
        [HttpGet("Exams/{exam_id}")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetExamByID(int exam_id)
        {
            IActionResult returnResponse;

            ExamViewModel exam = _repository.GetExamById(base.InstituteId, exam_id);
            if (exam == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(exam));
            }
            return returnResponse;
        }


        /// <summary>
        /// Get all exams 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/UpcomingExams")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetUpcomingExamsOfTheClass(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            ExamViewAllPagedModel examListPaged = new ExamViewAllPagedModel();

            if (pageNumber == null) pageNumber = 1;
            if (pageSize == null) pageSize = 20;

            //PREET_EXPLAIN : -1 is passed in user ID as for admin user it will retun all exams
            List<ExamViewAllModel> theList = _repository.GetUpcomingExams(base.InstituteId, class_id, -1, sortBy, sortOrder);

            if (pageNumber != null && pageSize != null)
            {
                PaginatedList<ExamViewAllModel> pList = PaginatedList<ExamViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                examListPaged.total_count = theList.Count;
                examListPaged.total_pages = pList.TotalPages;
                examListPaged.page_index = pList.PageIndex;
                examListPaged.next = pList.HasNextPage;
                examListPaged.previous = pList.HasPreviousPage;
                examListPaged.exams = pList;
            }
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(examListPaged));
           
            return returnResponse;
        }

        /// <summary>
        /// Get all exams 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/LiveExams")]
        [Authorize(Roles = "Admin, Teacher")]
        public IActionResult GetLiveExamsOfTheClass(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            ExamViewAllPagedModel examListPaged = new ExamViewAllPagedModel();

            if (pageNumber == null) pageNumber = 1;
            if (pageSize == null) pageSize = 20;

            //PREET_EXPLAIN : -1 is passed in user ID as for admin user it will retun all exams
            List<ExamViewAllModel> theList = _repository.GetLiveExams(base.InstituteId, class_id, -1, sortBy, sortOrder);

            if (pageNumber != null && pageSize != null)
            {
                PaginatedList<ExamViewAllModel> pList = PaginatedList<ExamViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                examListPaged.total_count = theList.Count;
                examListPaged.total_pages = pList.TotalPages;
                examListPaged.page_index = pList.PageIndex;
                examListPaged.next = pList.HasNextPage;
                examListPaged.previous = pList.HasPreviousPage;
                examListPaged.exams = pList;
            }
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(examListPaged));
            
            return returnResponse;
        }

        /// <summary>
        /// Get all exams 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/HistoryExams")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetHistoryExamsOfTheClass(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            ExamViewAllPagedModel examListPaged = new ExamViewAllPagedModel();

            if (pageNumber == null) pageNumber = 1;
            if (pageSize == null) pageSize = 20;

            //PREET_EXPLAIN : -1 is passed in user ID as for admin user it will retun all exams
            List<ExamViewAllModel> theList = _repository.GetHistoryExams(base.InstituteId, class_id, -1, sortBy, sortOrder);

            if (pageNumber != null && pageSize != null)
            {
                PaginatedList<ExamViewAllModel> pList = PaginatedList<ExamViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                examListPaged.total_count = theList.Count;
                examListPaged.total_pages = pList.TotalPages;
                examListPaged.page_index = pList.PageIndex;
                examListPaged.next = pList.HasNextPage;
                examListPaged.previous = pList.HasPreviousPage;
                examListPaged.exams = pList;
            }
            
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(examListPaged));
            
            return returnResponse;
        }

        /// <summary>
        /// Get all exams 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/DraftExams")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetDraftExamsOfTheClass(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            ExamViewDraftPagedModel examListPaged = new ExamViewDraftPagedModel();
            if (pageNumber == null) pageNumber = 1;
            if (pageSize == null) pageSize = 20;

            //PREET_EXPLAIN : -1 is passed in user ID as for admin user it will retun all exams
            List<ExamViewDraftModel> theList = _repository.GetDraftExams(base.InstituteId, class_id, -1, sortBy, sortOrder);

            if (pageNumber != null && pageSize != null)
            {
                PaginatedList<ExamViewDraftModel> pList = PaginatedList<ExamViewDraftModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                examListPaged.total_count = theList.Count;
                examListPaged.total_pages = pList.TotalPages;
                examListPaged.page_index = pList.PageIndex;
                examListPaged.next = pList.HasNextPage;
                examListPaged.previous = pList.HasPreviousPage;
                examListPaged.exams = pList;
            }
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(examListPaged));
            
            return returnResponse;
        }

        /// <summary>
        /// Update an exam
        /// </summary>
        /// <param name="exam_id"></param>
        /// <param name="theExam"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/Update")]
        [Authorize(Roles = "Admin")]
        public IActionResult UpdateExamOfTheInstitute(int exam_id, [FromBody] ExamEditModel theExam)
        {
            IActionResult returnResponse = null;

            theExam.id = exam_id;
            ExamViewAllModel exam = _repository.UpdateExamOfTheInstitute(base.InstituteId, theExam);
            if (exam == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.StudyNote);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(exam));
            }
            return returnResponse;
        }

        /// <summary>
        /// Delete an exam 
        /// </summary>
        /// <param name="exam_id"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/Delete")]
        [Authorize(Roles = "Admin")]
        public IActionResult DeleteExamOfTheInstitute(int exam_id)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            int returnResult = _repository.DeleteExam(base.InstituteId, user_id, exam_id);
            if (returnResult <= 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotDeleted.ToString(), Constant.Exam);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectDeleteSuccessfully.ToString(), Constant.Exam);
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(responseMessage));
            }
            return returnResponse;
        }


        /// <summary>
        /// Delete an exam section
        /// </summary>
        /// <param name="exam_section_id"></param>
        /// <returns></returns>
        [HttpPost("ExamSections/{exam_section_id}")]
        [Authorize(Roles = "Admin")]
        public IActionResult DeleteExamSectionOfTheInstitute(int exam_section_id)
        {
            IActionResult returnResponse = null;

            int returnResult = _repository.DeleteExamSection(exam_section_id);
            if (returnResult <= 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotDeleted.ToString(), Constant.Exam);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectDeleteSuccessfully.ToString(), Constant.Exam);
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(responseMessage));
            }
            return returnResponse;
        }

        /// <summary>
        /// Get all questions
        /// </summary>
        /// <param name="exam_section_id"></param>
        /// <returns></returns>
        [HttpGet("ExamSections/{exam_section_id}/Questions")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetQuestionsOfTheExamSection(int exam_section_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            SectionQuestionsPagedModel qnsListPaged = new SectionQuestionsPagedModel();
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (pageNumber == null) pageNumber = 1;
            if (pageSize == null) pageSize = 20;

            List<SectionQuestionDetails> qnsList = _repository.GetQuestionsOfTheSection(base.InstituteId, user_id, exam_section_id, sortBy, sortOrder);

            if (pageNumber != null && pageSize != null)
            {
                PaginatedList<SectionQuestionDetails> pList = PaginatedList<SectionQuestionDetails>.CreateAsync(qnsList, (int)pageNumber, (int)pageSize);
                qnsListPaged.total_count = qnsList.Count;
                qnsListPaged.total_pages = pList.TotalPages;
                qnsListPaged.page_index = pList.PageIndex;
                qnsListPaged.next = pList.HasNextPage;
                qnsListPaged.previous = pList.HasPreviousPage;
                qnsListPaged.questions = pList;
            }
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(qnsListPaged));
            
            return returnResponse;
        }

/*
        /// <summary>
        /// Exam subscription status
        /// </summary>
        /// <param name="exam_id"></param>
        /// <param name="subscription"></param>
        /// <returns></returns>
        [HttpPut("Exams/{exam_id}/SubscriptionType")]
        [Authorize(Roles = "Admin, Teacher")]
        public IActionResult SubscriptionType(int exam_id, [FromBody] SubscriptionType subscription)
        {
            IActionResult returnResponse = null;
            
            if(subscription == null || subscription.type < 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.InvalidInput.ToString(), Constant.StudyNote);
                return BadRequest(ReturnResponse.GetFailureStatus(responseMessage));                
            }

            int status = _repository.SubscriptionType(base.InstituteId, exam_id, subscription.type);

            if (status >= 0)
            {
                return Ok(ReturnResponse.GetSuccessStatus(status));
            }
            else if (status == (int)Message.NotAllowedToResource)
            {
                responseMessage = _repository.GetMessageByCode(Message.NotAllowedToResource.ToString(), Constant.Exam);
                return BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Exam);
                return returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
        }
*/
        
        /// <summary>
        /// Attach usergroups to Exam  
        /// </summary>
        /// <param name="exam_id"></param>
        /// <param name="batchList"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/AttachBatch")]
        [Authorize(Roles = "Admin")]
        public IActionResult AttachBatchToTheExam(int exam_id, [FromBody] UserGroupsList batchList)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            int groups_added = _repository.AttachUserGroups(base.InstituteId, user_id, exam_id, batchList);
            if (groups_added <= 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAttach.ToString(), Constant.Exam);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(groups_added));
            }
            return returnResponse;
        }



        /// <summary>
        /// Detach usergroups to Exam  
        /// </summary>
        /// <param name="exam_id"></param>
        /// <param name="batchList"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/DetachBatch")]
        [Authorize(Roles = "Admin")]
        public IActionResult DetachBatchToTheExam(int exam_id, [FromBody] UserGroupsList batchList)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            int groups_added = _repository.DetachUserGroups(base.InstituteId, user_id, exam_id, batchList);
            if (groups_added <= 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToDetach.ToString(), Constant.Exam);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(groups_added));
            }
            return returnResponse;
        }


        /// <summary>
        /// Get usergroups attached to Exam  
        /// </summary>
        /// <param name="exam_id"></param>
        /// <returns></returns>
        [HttpGet("Exams/{exam_id}/Batches")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetBatchListOfTheExam(int exam_id)
        {
            
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            UserGroupsList ugl = new UserGroupsList(); ;
            ugl = _repository.GetBatchListsOfTheExam(base.InstituteId, user_id, exam_id);
            
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(ugl));
            
            return returnResponse;
        }

        /// <summary>
        /// Stop Exam
        /// </summary>
        /// <param name="exam_id"></param>
        /// <returns></returns>
        [HttpPost("Exams/{exam_id}/StopExam")]
        [Authorize(Roles = "Admin")]
        public IActionResult StopExam(int exam_id)
        {
            IActionResult returnResponse = null;

            int exam_code = _repository.StopExam(exam_id);
            if (exam_code < 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.StudyNote);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(exam_id));
            }
            return returnResponse;
        }
        
        #endregion
       
    }
}
