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


        #region Practices 
        /// <summary>
        /// Add new practice
        /// </summary>
        /// <param name="language"></param>
        /// <param name="class_id"></param>
        /// <param name="newPractice"></param>
        /// <returns></returns>
        [HttpPost("{language}/Classes/{class_id}/Practices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult AddNewPractice(string language, int class_id, [FromBody] PracticeAddModel newPractice)
        {
            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
            {
                PracticeViewModel practice = _repository.AddNewPractice(base.InstituteId, language_id, class_id, user_id, newPractice, out return_message);
                if (practice == null)
                {
                    responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Practice);
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
                else
                {
                    returnResponse = Ok(ReturnResponse.GetSuccessStatus(practice));
                }
            }
            return returnResponse;
        }

        /// <summary>
        /// Delete a practice 
        /// </summary>
        /// <param name="practice_id"></param>
        /// <returns></returns>
        [HttpPost("Practices/{practice_id}/Delete")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult DeletePracticeOfTheInstitute(int practice_id)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }

            int returnResult = _repository.DeletePractice(base.InstituteId, user_id, practice_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 Practice details 
        /// </summary>
        /// <param name="practice_id"></param>
        /// <returns></returns>
        [HttpGet("Practices/{practice_id}")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetPracticeByID(int practice_id)
        {
            IActionResult returnResponse;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(p));
            }
            return returnResponse;
        }

        /// <summary>
        /// Get all upcoming practices of the subject
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/UpcomingSubjectPractices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetUpcomingSubjectPractices(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            PracticeViewAllPagedModel practiceListPaged = new PracticeViewAllPagedModel();

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

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

            List<PracticeViewAllModel> theList = _repository.GetUpcomingPractices(base.InstituteId, class_id, user_id, Constant.Subject.ToString(), sortBy, sortOrder);

            if (theList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeViewAllModel> pList = PaginatedList<PracticeViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                    practiceListPaged.total_count = theList.Count;
                    practiceListPaged.total_pages = pList.TotalPages;
                    practiceListPaged.page_index = pList.PageIndex;
                    practiceListPaged.next = pList.HasNextPage;
                    practiceListPaged.previous = pList.HasPreviousPage;
                    practiceListPaged.practices = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practiceListPaged));
            }

            return returnResponse;
        }

        /// <summary>
        /// Get all upcoming practices of the category
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/UpcomingCategoryPractices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetUpcomingCategoryPractices(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            PracticeViewAllPagedModel practiceListPaged = new PracticeViewAllPagedModel();

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

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

            List<PracticeViewAllModel> theList = _repository.GetUpcomingPractices(base.InstituteId, class_id, user_id, Constant.Category.ToString(), sortBy, sortOrder);

            if (theList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeViewAllModel> pList = PaginatedList<PracticeViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                    practiceListPaged.total_count = theList.Count;
                    practiceListPaged.total_pages = pList.TotalPages;
                    practiceListPaged.page_index = pList.PageIndex;
                    practiceListPaged.next = pList.HasNextPage;
                    practiceListPaged.previous = pList.HasPreviousPage;
                    practiceListPaged.practices = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practiceListPaged));
            }

            return returnResponse;
        }

        /// <summary>
        /// Get all Live practices 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/LiveSubjectPractices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetLiveSubjectPractices(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            PracticeViewAllPagedModel practiceListPaged = new PracticeViewAllPagedModel();

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

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

            List<PracticeViewAllModel> theList = _repository.GetLivePractices(base.InstituteId, class_id, user_id, Constant.Subject.ToString(), sortBy, sortOrder);

            if (theList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeViewAllModel> pList = PaginatedList<PracticeViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                    practiceListPaged.total_count = theList.Count;
                    practiceListPaged.total_pages = pList.TotalPages;
                    practiceListPaged.page_index = pList.PageIndex;
                    practiceListPaged.next = pList.HasNextPage;
                    practiceListPaged.previous = pList.HasPreviousPage;
                    practiceListPaged.practices = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practiceListPaged));
            }

            return returnResponse;
        }


        /// <summary>
        /// Get all Live practices 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/LiveCategoryPractices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetLiveCategoryPractices(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            PracticeViewAllPagedModel practiceListPaged = new PracticeViewAllPagedModel();

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

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

            List<PracticeViewAllModel> theList = _repository.GetLivePractices(base.InstituteId, class_id, user_id, Constant.Category.ToString(), sortBy, sortOrder);

            if (theList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeViewAllModel> pList = PaginatedList<PracticeViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                    practiceListPaged.total_count = theList.Count;
                    practiceListPaged.total_pages = pList.TotalPages;
                    practiceListPaged.page_index = pList.PageIndex;
                    practiceListPaged.next = pList.HasNextPage;
                    practiceListPaged.previous = pList.HasPreviousPage;
                    practiceListPaged.practices = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practiceListPaged));
            }

            return returnResponse;
        }


        /// <summary>
        /// Get all draft practices 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/DraftSubjectPractices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetDraftSubjectPractices(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            PracticeViewAllPagedModel practiceListPaged = new PracticeViewAllPagedModel();

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

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

            List<PracticeViewAllModel> theList = _repository.GetDraftPractices(base.InstituteId, class_id, user_id, Constant.Subject.ToString(), sortBy, sortOrder);

            if (theList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeViewAllModel> pList = PaginatedList<PracticeViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                    practiceListPaged.total_count = theList.Count;
                    practiceListPaged.total_pages = pList.TotalPages;
                    practiceListPaged.page_index = pList.PageIndex;
                    practiceListPaged.next = pList.HasNextPage;
                    practiceListPaged.previous = pList.HasPreviousPage;
                    practiceListPaged.practices = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practiceListPaged));
            }

            return returnResponse;
        }

        /// <summary>
        /// Get all draft practices 
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Classes/{class_id}/DraftCategoryPractices")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetDraftCategoryPractices(int class_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            PracticeViewAllPagedModel practiceListPaged = new PracticeViewAllPagedModel();

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

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

            List<PracticeViewAllModel> theList = _repository.GetDraftPractices(base.InstituteId, class_id, user_id, Constant.Category.ToString(), sortBy, sortOrder);

            if (theList == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeViewAllModel> pList = PaginatedList<PracticeViewAllModel>.CreateAsync(theList, (int)pageNumber, (int)pageSize);
                    practiceListPaged.total_count = theList.Count;
                    practiceListPaged.total_pages = pList.TotalPages;
                    practiceListPaged.page_index = pList.PageIndex;
                    practiceListPaged.next = pList.HasNextPage;
                    practiceListPaged.previous = pList.HasPreviousPage;
                    practiceListPaged.practices = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practiceListPaged));
            }

            return returnResponse;
        }

        /// <summary>
        /// Attach Questions To Practices
        /// </summary>
        /// <param name="practice_id"></param>
        /// <param name="questionIdList"></param>
        /// <returns></returns>
        [HttpPost("Practices/{practice_id}/AttachQuestions")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult AttachQuestionsToPractice(int practice_id, [FromBody] QuestionsList questionIdList)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }

            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.AttachQuestionsToPractice(base.InstituteId, user_id, practice_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 Practice
        /// </summary>
        /// <param name="practice_id"></param>
        /// <param name="questionIdList"></param>
        /// <returns></returns>
        [HttpPost("Practices/{practice_id}/DetachQuestions")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult DetachQuestionsFromPractice(int practice_id, [FromBody] QuestionsList questionIdList)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }

            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.DetachQuestionsFromPractice(base.InstituteId, practice_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>
        /// Get all questions
        /// </summary>
        /// <param name="practice_id"></param>
        /// <returns></returns>
        [HttpGet("Practices/{practice_id}/Questions")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult GetQuestionsOfThePractice(int practice_id, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse = null;
            string return_message = string.Empty;
            PracticeQuestionsPagedModel qnsListPaged = new PracticeQuestionsPagedModel();
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }

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

            List<PracticeQuestionDetails> qnsList = _repository.GetQuestionsOfThePractice(base.InstituteId, user_id, practice_id, sortBy, sortOrder);

            if (qnsList == null || qnsList.Count == 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<PracticeQuestionDetails> pList = PaginatedList<PracticeQuestionDetails>.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>
        /// Review Questions Of The Practice
        /// </summary>
        /// <param name="practice_id"></param>
        /// <param name="questionList"></param>
        /// <returns></returns>
        [HttpPost("Practices/{practice_id}/ReviewQuestions")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult AssignDurationToPracticeQuestions(int practice_id, [FromBody] QuestionDurationList questionList)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }

            PracticeViewModel practice = _repository.AssignDurationToPracticeQuestions(base.InstituteId, practice_id, questionList);
            if (practice == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Practice);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practice));
            }
            return returnResponse;
        }

        /// <summary>
        /// Publish Practice  
        /// </summary>
        /// <param name="practice_id"></param>
        /// <param name="schedulePractice"></param>
        /// <returns></returns>
        [HttpPost("Practices/{practice_id}/Publish")]
        [Authorize(Roles = "Admin,Teacher")]
        public IActionResult PublishPractice(int practice_id, [FromBody] PracticePublishModel schedulePractice)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            PracticeViewModel p = _repository.GetPracticeById(base.InstituteId, user_id, practice_id);
            if (p == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = BadRequest(ReturnResponse.GetFailureStatus(responseMessage));
            }

            PracticeViewModel practice = _repository.PublishPractice(base.InstituteId, user_id, practice_id, schedulePractice);
            if (practice == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Practice);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(practice));
            }
            return returnResponse;
        }

        #endregion

    }
}
