using CloudBuilder.Core.DatabaseAccessor;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Security.Entity;
using CloudBuilder.Security.Policy;
using CloudBuilder.Security.SearchCriteria;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace CloudBuilder.Security.Service
{
    public class SecurityUserPreferenceService : ISecurityUserPreferenceService
    {
        private readonly IApplicationService service;

        private IRepository<SecurityUserPreferenceEntity> repository;
        public SecurityUserPreferenceService(IApplicationService service)
        {
            this.service = service;
            repository = service.GetRepository<IRepository<SecurityUserPreferenceEntity>>();
        }

        [AllowAnonymous]
        public SecurityUserPreferenceEntity FindByKey(SecurityUserPreferenceEntityKey key)
        {
            return repository.DetachedEntities.Where(x => 1 == 1
            && x.Username == service.UserId
            && x.PreferenceKey == key.PreferenceKey
           ).FirstOrDefault()!;
        }

        [PermissionOperate(name: SecurityPermissionPolicy.SecurityUserPreference, operate: Operate.Find)]
        public SecurityUserPreferenceEntity[] FindBySearchCriteria(SecurityUserPreferenceSearchCriteria criteria)
        {
            throw new NotImplementedException();
        }

        [PermissionOperate(name: SecurityPermissionPolicy.SecurityUserPreference, operate: Operate.Print)]
        public dynamic PrintBySearchCriteria(SecurityUserPreferenceSearchCriteria criteria)
        {
            throw new NotImplementedException();
        }

        [AllowAnonymous]
        [UnitOfWork]
        public SecurityUserPreferenceEntity Insert(SecurityUserPreferenceEntity data)
        {
            SecurityUserPreferenceEntity preferenceEntity = repository.DetachedEntities.Where(x => x.Username == data.Username &&
x.PreferenceKey == data.PreferenceKey).FirstOrDefault()!;
            if (preferenceEntity != null) { repository.DeleteNow(preferenceEntity); }

            repository.InsertNow(data);
            return data!;
        }

        [AllowAnonymous]
        [UnitOfWork]
        public SecurityUserPreferenceEntity Update(SecurityUserPreferenceEntity data)
        {
            SecurityUserPreferenceEntity preferenceEntity = repository.DetachedEntities.Where(x => x.Username == data.Username &&
x.PreferenceKey == data.PreferenceKey).FirstOrDefault()!;
            if (preferenceEntity == null)
            {
                repository.InsertNow(data);
            }
            else
            {
                preferenceEntity.Preference = data.Preference;
                repository.UpdateNow(preferenceEntity);
            }

            return data!;
        }


        [AllowAnonymous]
        public void Delete(SecurityUserPreferenceEntityKey key)
        {
            SecurityUserPreferenceEntity ent = repository.DetachedEntities.Where(x => 1 == 1
              && x.Username == key.Username
              && x.PreferenceKey == key.PreferenceKey
             ).FirstOrDefault()!;
            if (ent != null) { repository.DeleteNow(ent); }
        }
    }
}
