using System.Collections.Generic;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OnlineAssessment.Common;
using OnlineAssessment.Data;
using OnlineAssessment.Domain;

namespace OnlineAssessment.V1.Controllers
{
    [Route("v{version:apiVersion}/[controller]")]
    [ApiController]
    [EnableCors("OdiwarePolicy")]
    [ApiVersion("1.0")]

    public class BaseController<TEntity, TRepository> : ControllerBase
        where TEntity : class, IEntity
        where TRepository : IRepository<TEntity>
    {
        private readonly TRepository repository;

        public int InstituteId
        {
            get
            {
                int institute_id = int.Parse(Security.GetValueFromToken("InstituteId", HttpContext.User.Identity as ClaimsIdentity));
                return institute_id;
            }
        }
        public BaseController(TRepository repository)
        {
            this.repository = repository;
        }


        internal List<string> NotAllowedMessages(UserOperation userOperation)
        {
            string responseMessage;
            List<string> errList = new List<string>();
            responseMessage = repository.GetMessageByCode(Message.NotAllowedToResource.ToString());
            errList.Add(responseMessage);

            if (userOperation.Equals(UserOperation.Add))
                responseMessage = repository.GetMessageByCode(Message.NotAllowedToAddResourceOtherThanYours.ToString());
            else if (userOperation.Equals(UserOperation.Update))
                responseMessage = repository.GetMessageByCode(Message.NotAllowedToUpdateResourceOtherThanYours.ToString());
            else if (userOperation.Equals(UserOperation.Delete))
                responseMessage = repository.GetMessageByCode(Message.NotAllowedToDeleteResourceOtherThanYours.ToString());
            else if (userOperation.Equals(UserOperation.View))
                responseMessage = repository.GetMessageByCode(Message.NotAllowedToViewResourceOtherThanYours.ToString());

            errList.Add(responseMessage);

            return errList;
        }

    }
}
