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;
        }


        /// <summary>
        /// Get the theme of an institute
        /// </summary>
        /// <returns></returns>
        [HttpGet("Theme")]
        [Authorize(Roles = "Admin,Student")]
        public IActionResult GetTheme()
        {
            IActionResult returnResponse;
            string entity = _repository.GetTheme(base.InstituteId);
            if (entity == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
            }
            return returnResponse;
        }

        /// <summary>
        /// Update the theme of an institute
        /// </summary>
        /// <returns></returns>
        [HttpPost("Theme/update")]
        [Authorize(Roles = "Admin")]
        public IActionResult UpdateTheme([FromBody] string color)
        {
            IActionResult returnResponse;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            string entity = _repository.UpdateTheme(base.InstituteId, user_id, color);
            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")]
        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")]
        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);
            //-------------------------------------------------------------------------------------

            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")]
        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;
        }


        /// <summary>
        /// Add a new class of the Institution
        /// </summary>
        /// <param name="language"></param>
        /// <param name="newClass"></param>
        /// <returns></returns>
        [HttpPost("Classes")]
        [Authorize(Roles = "Admin")]
        public IActionResult AddClassToTheInstitute([FromBody] ClassAddModel newClass)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (newClass == null || newClass.name == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAdd.ToString(), Constant.Class);
                return returnResponse;
            }

            // Add new class
            ClassViewModel cv = _repository.AddNewClassOfTheInstitution(base.InstituteId, user_id, newClass.name);
            if (cv == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Class);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(cv));
            }
            return returnResponse;
        }


        /// <summary>
        /// Update the class of an institute
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="theClass"></param>
        /// <returns></returns>
        [HttpPost("Classes/{class_id}/Update")]
        [Authorize(Roles = "Admin")]
        public IActionResult UpdateClassOfTheInstitute(int class_id, [FromBody] ClassEditModel theClass)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (theClass == null || theClass.name == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToUpdate.ToString(), Constant.Class);
                return returnResponse;
            }

            ClassViewModel cv = _repository.UpdateClassOfTheInstitution(base.InstituteId, user_id, class_id, theClass.name);

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

        /// <summary>
        /// Delete the class of an institute
        /// </summary>
        /// <param name="class_id"></param>
        /// <returns></returns>
        [HttpPost("Classes/{class_id}/Delete")]
        [Authorize(Roles = "Admin")]
        public IActionResult DeleteClassOfTheInstitute(int class_id)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            int returnResult = _repository.DeleteClassOfTheInstitution(base.InstituteId, user_id, class_id);
            if (returnResult <= 0)
            {
                if (returnResult.Equals((int)Message.NotAllowedToResource))
                {
                    returnResponse = Unauthorized(ReturnResponse.GetFailureStatus(base.NotAllowedMessages(UserOperation.Delete)));
                }
                else
                {
                    responseMessage = _repository.GetMessageByCode(Message.ObjectNotDeleted.ToString(), Constant.Institute);
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectDeleteSuccessfully.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(responseMessage));
            }
            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")]
        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);
            //-------------------------------------------------------------------------------------
            
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(theList));
            return returnResponse;
        }

        /// <summary>
        /// Add a new subject of a class
        /// </summary>
        /// <param name="class_id"></param>
        /// <param name="newSubject"></param>
        /// <returns></returns>
        [HttpPost("Classes/{class_id}/Subjects")]
        [Authorize(Roles = "Admin")]
        public IActionResult AddSubjectOfTheClass(int class_id, [FromBody] SubjectAddModel newSubject)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            string returnMessage = string.Empty;
            ClassViewModel c = _repository.GetClassById(base.InstituteId, class_id);
            
            if (c == null || c.isActive == false) // This class not belong to the request sender's institute
            {
                returnResponse = Unauthorized(ReturnResponse.GetFailureStatus(base.NotAllowedMessages(UserOperation.Add)));
                return returnResponse;
            }
            
            //-------------------------------------------------------------------------------------
            SubjectViewModel subject = _repository.AddSubjectOfTheClass(base.InstituteId, c.id, user_id, newSubject);
            //-------------------------------------------------------------------------------------

            if (subject == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Subject);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(new List<string> { responseMessage, returnMessage }));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(subject));
            }
            return returnResponse;
        }

        /// <summary>
        /// Update subject
        /// </summary>
        /// <param name="subject_id"></param>
        /// <param name="theSubject"></param>
        /// <returns></returns>
        [HttpPost("Subjects/{subject_id}/Update")]
        [Authorize(Roles = "Admin")]
        public IActionResult UpdateSubjectOfTheInstitute(int subject_id, [FromBody] SubjectEditModel theSubject)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (theSubject == null || theSubject.name == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAdd.ToString(), Constant.Subject);
                return returnResponse;
            }

            //-------------------------------------------------------------------------------------
            SubjectViewModel subject = _repository.UpdateSubjectOfTheInstitution(base.InstituteId, user_id, subject_id, theSubject.name);
            //-------------------------------------------------------------------------------------

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

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

            //-------------------------------------------------------------------------------------
            int returnResult = _repository.DeleteSubjectById(base.InstituteId, user_id, subject_id);
            //-------------------------------------------------------------------------------------

            if (returnResult <= 0)
            {
                if (returnResult.Equals((int)Message.NotAllowedToResource))
                {
                    returnResponse = Unauthorized(ReturnResponse.GetFailureStatus(base.NotAllowedMessages(UserOperation.Delete)));
                }
                else
                {
                    responseMessage = _repository.GetMessageByCode(Message.ObjectNotDeleted.ToString(), Constant.Subject);
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectDeleteSuccessfully.ToString(), Constant.Subject);
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(responseMessage));
            }
            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")]
        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);
            //-------------------------------------------------------------------------------------

            
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(theList));
            return returnResponse;

        }

        /// <summary>
        /// Create new category
        /// </summary>
        /// <param name="newCategory"></param>
        /// <param name="subject_id"></param>
        /// <returns></returns>
        [HttpPost("Subjects/{subject_id}/Categories")]
        [Authorize(Roles = "Admin")]
        public IActionResult AddCategoryOfTheSubject(int subject_id, [FromBody] CategoryAddModel newCategory)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (newCategory == null || newCategory.name == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAdd.ToString(), Constant.Category);
                return returnResponse;
            }

           
            CategoryViewModel category = _repository.AddCategoryOfTheSubject(base.InstituteId, subject_id, user_id, newCategory);
            if (category == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Category);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(category));
            }
            return returnResponse;
        }

        /// <summary>
        /// Update Category (Logic) - category id should be from same institute, category should be active 
        /// </summary>
        /// <param name="category_id"></param>
        /// <param name="theCategory"></param>
        /// <returns></returns>
        [HttpPost("Categories/{category_id}/Update")]
        [Authorize(Roles = "Admin")]
        public IActionResult UpdateCategoryByID(int category_id, [FromBody] CategoryEditModel theCategory)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (theCategory == null || theCategory.name == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAdd.ToString(), Constant.Category);
                return returnResponse;
            }

            CategoryViewModel category = _repository.UpdateCategoryByID(base.InstituteId, category_id, user_id, theCategory.name);
            if (category == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Category);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(category));
            }
            return returnResponse;
        }

        /// <summary>
        ///  Delete Category (Logic) - category id should be from same institute, category should be active 
        /// <param name="category_id"></param>
        /// <returns></returns>
        [HttpPost("Categories/{category_id}/Delete")]
        [Authorize(Roles = "Admin")]
        public IActionResult DeleteCategoryByID(int category_id)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            
            //-------------------------------------------------------------------------------------
            int returnResult = _repository.DeleteCategoryByID(base.InstituteId, user_id, category_id);
            //-------------------------------------------------------------------------------------

            if (returnResult <= 0)
            {
                if (returnResult.Equals((int)Message.NotAllowedToResource))
                {
                    returnResponse = Unauthorized(ReturnResponse.GetFailureStatus(base.NotAllowedMessages(UserOperation.Delete)));
                }
                else
                {
                    responseMessage = _repository.GetMessageByCode(Message.ObjectNotDeleted.ToString(), Constant.Category);
                    returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                }
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectDeleteSuccessfully.ToString(), Constant.Category);
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(responseMessage));
            }
            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")]
        public IActionResult GetAllTagsOfTheInstitution([FromQuery] string sortBy, string sortOrder)
        {
            IActionResult returnResponse;
            List<TagViewModel> theList = _repository.GetTagsOfTheInstitute(base.InstituteId, sortBy, sortOrder);
            
            returnResponse = Ok(ReturnResponse.GetSuccessStatus(theList));
            return returnResponse;
        }

        /// <summary>
        /// Add new tag
        /// </summary>
        /// <param name="newTag"></param>
        /// <returns></returns>
        [HttpPost("Tags")]
        [Authorize(Roles = "Admin")]
        public IActionResult AddTagOfClassOfTheInstitute([FromBody] TagAddModel newTag)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            TagViewModel tag = _repository.AddTagOfTheInstitute(base.InstituteId, user_id, newTag);
            if (tag == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(tag));
            }
            return returnResponse;
        }

        /// <summary>
        /// Edit a tag
        /// </summary>
        /// <param name="tag_id"></param>
        /// <param name="tag"></param>
        /// <returns></returns>
        [HttpPost("Tags/{tag_id}/Update")]
        [Authorize(Roles = "Admin")]
        public IActionResult UpdateTagOfTheInstitute(int tag_id, [FromBody] TagEditModel tag)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            if (tag_id != tag.Id)
            {
                responseMessage = _repository.GetMessageByCode(Message.IdMismatchBetweenBodyAndQueryString.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
                return returnResponse;
            }

            TagViewModel theTag = _repository.UpdateTagOfTheInstitute(base.InstituteId, user_id, tag);
            if (theTag == null)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Tag);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(theTag));
            }
            return returnResponse;
        }

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

            int returnResult = _repository.DeleteTagOfTheInstitute(base.InstituteId, user_id, tag_id);
            if (returnResult <= 0)
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectNotDeleted.ToString(), Constant.Tag);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.ObjectDeleteSuccessfully.ToString(), Constant.Tag);
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(responseMessage));
            }
            return returnResponse;
        }
        #endregion

        #region Users 
        /// <summary>
        /// Get the users of an institute
        /// </summary>
        /// <param name="role"></param>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Users")]
        [Authorize(Roles = "Admin")]
        public IActionResult GetUserOfTheInstitution([FromQuery] string role, [FromQuery] string sortBy, string sortOrder, [FromQuery] int? pageNumber, [FromQuery] int? pageSize)
        {
            IActionResult returnResponse;
            UserViewAllPagedModel userListPaged = new UserViewAllPagedModel();

            int roleId = 4; //TODO : hardcoded

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

            List<UserViewModel> userList = _repository.GetUserOfTheInstitution(base.InstituteId, roleId, sortBy, sortOrder);
            if (userList == null || userList.Count.Equals(0))
            {
                responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                if (pageNumber != null && pageSize != null)
                {
                    PaginatedList<UserViewModel> pList = PaginatedList<UserViewModel>.CreateAsync(userList, (int)pageNumber, (int)pageSize);
                    userListPaged.total_count = userList.Count;
                    userListPaged.total_pages = pList.TotalPages;
                    userListPaged.page_index = pList.PageIndex;
                    userListPaged.next = pList.HasNextPage;
                    userListPaged.previous = pList.HasPreviousPage;
                    userListPaged.users = pList;
                }
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(userListPaged));

                //returnResponse = Ok(ReturnResponse.GetSuccessStatus(userList));
            }
            return returnResponse;
        }


        #endregion
        
    }
}
