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 PlansController : BaseController<Subscriptions, EFCorePlanRepository>
    {
        EFCorePlanRepository _repository;
        string responseMessage = string.Empty;

        public PlansController(EFCorePlanRepository repository) : base(repository)
        {
            _repository = repository;
        }

        
        #region Plans

        /// <summary>
        /// Add new Plan
        /// </summary>
        /// <param name="newPlan"></param>
        /// <returns></returns>
        [HttpPost("Plans")]
        [Authorize(Roles = "Admin, Teacher")]
        public IActionResult AddPlans([FromBody] PlanAddModel newPlan)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            dynamic retCode = _repository.AddPlans(base.InstituteId, user_id, newPlan);
            if(retCode is string)
            {
                returnResponse = Ok(ReturnResponse.GetSuccessStatus(retCode));
            } 
            else if (retCode is int && retCode == (int) Message.InvalidInput)
            {
                responseMessage = _repository.GetMessageByCode(Message.InvalidInput.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToAdd.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            return returnResponse;
        }

        /// <summary>
        /// Publish Plan
        /// </summary>
        /// <param name="plan_code"></param>
        /// <returns></returns>
        [HttpPost("Plans/{plan_code}/Publish")]
        [Authorize(Roles = "Admin, Teacher")]
        public IActionResult PublishPlans(string plan_code)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            dynamic retCode = _repository.PublishPlans(base.InstituteId, user_id, plan_code);
            if (retCode is string)
            {
                return Ok(ReturnResponse.GetSuccessStatus(retCode));
            }
            
            else if(retCode is int && retCode == (int)Message.NotAllowedToResource)
            {
                responseMessage = _repository.GetMessageByCode(Message.NotAllowedToResource.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                responseMessage = _repository.GetMessageByCode(Message.FailedToUpdate.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }

            return returnResponse;
        }

        /// <summary>
        /// Delete Plan
        /// </summary>
        /// <param name="plan_code"></param>
        /// <returns></returns>
        [HttpPost("Plans/{plan_code}/Delete")]
        [Authorize(Roles = "Admin, Teacher")]
        public IActionResult DeletePlans(string plan_code)
        {
            IActionResult returnResponse = null;
            int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);

            dynamic retCode = _repository.DeletePlans(base.InstituteId, user_id, plan_code);
            if (retCode is string)
            {
                return Ok(ReturnResponse.GetSuccessStatus(retCode));
            }

            else if (retCode is int && retCode == (int)Message.NotAllowedToResource)
            {
                responseMessage = _repository.GetMessageByCode(Message.NotAllowedToResource.ToString(), Constant.Institute);
                returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
            }
            else
            {
                returnResponse = Ok(ReturnResponse.GetFailureStatus(retCode));
            }

            return returnResponse;
        }


        /// <summary>
        /// Get all tags
        /// </summary>
        /// <param name="sortBy"></param>
        /// <param name="sortOrder"></param>
        /// <returns></returns>
        [HttpGet("Plans")]
        [Authorize(Roles = "Admin, Teacher")]
        public IActionResult GetAllPlans([FromQuery] string sortBy, string sortOrder)
        {
            IActionResult returnResponse;
            List<PlanViewModel> theList = _repository.GetPlans(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
        
    }
}
