using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.EventBuses;
using CloudBuilder.Security.Entity;
using CloudBuilder.Security.Policy;
using CloudBuilder.Security.Subscriber;

namespace CloudBuilder.Security.Service.Subscriber
{
    public class InsertSecurityUserActorSubscriber : ISecurityUserRegisterSubscriber
    {
        private readonly IApplicationService service;

        public InsertSecurityUserActorSubscriber(IApplicationService service)
        {
            this.service = service;
        }

        public void Handle(EventData eventData)
        {
            IRepository<SecurityUserActorEntity> repository = service.GetRepository<IRepository<SecurityUserActorEntity>>();

            SecurityUserActorEntity ent = new SecurityUserActorEntity();
            ent.UserName = ((SecurityUserEntity)eventData.EventSource).UserName;
            ent.ActorName = SecurityUserPolicy.SECURITY_USER_BASE_ACTOR;

            repository.InsertNow(ent);
        }
    }
}
