package eu.bcvsolutions.idm.acc.event.processor.provisioning;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Description;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import eu.bcvsolutions.idm.acc.AccModuleDescriptor;
import eu.bcvsolutions.idm.acc.domain.ProvisioningEventType;
import eu.bcvsolutions.idm.acc.entity.SysProvisioningOperation;
import eu.bcvsolutions.idm.acc.service.api.SysProvisioningOperationService;
import eu.bcvsolutions.idm.core.api.event.AbstractEntityEventProcessor;
import eu.bcvsolutions.idm.core.api.event.DefaultEventResult;
import eu.bcvsolutions.idm.core.api.event.EntityEvent;
import eu.bcvsolutions.idm.core.api.event.EventResult;
import eu.bcvsolutions.idm.core.model.entity.IdmIdentity;
import eu.bcvsolutions.idm.core.model.service.api.IdmIdentityService;
import eu.bcvsolutions.idm.core.notification.api.domain.NotificationLevel;
import eu.bcvsolutions.idm.core.notification.entity.IdmMessage;
import eu.bcvsolutions.idm.core.notification.service.api.NotificationManager;
import eu.bcvsolutions.idm.core.security.api.domain.GuardedString;
import eu.bcvsolutions.idm.ic.api.IcAttribute;
import eu.bcvsolutions.idm.ic.api.IcPasswordAttribute;

@Component
@Description("After success provisioning send notification to identity with new generate password.")
public class ProvisioningSendNotificationProcessor extends AbstractEntityEventProcessor<SysProvisioningOperation> {
	
	public static final String PROCESSOR_NAME = "provisioning-send-notification-processor";
	private final NotificationManager notificationManager;
	private final SysProvisioningOperationService provisioningOperationService;
	private final IdmIdentityService identityService;
	
	@Autowired
	public ProvisioningSendNotificationProcessor(NotificationManager notificationManager,
			SysProvisioningOperationService provisioningOperationService,
			IdmIdentityService identityService) {
		super(ProvisioningEventType.CREATE);
		//
		Assert.notNull(notificationManager);
		Assert.notNull(provisioningOperationService);
		Assert.notNull(identityService);
		//
		this.identityService = identityService;
		this.notificationManager = notificationManager;
		this.provisioningOperationService = provisioningOperationService;
	}

	@Override
	public String getName() {
		return PROCESSOR_NAME;
	}

	@Override
	public EventResult<SysProvisioningOperation> process(EntityEvent<SysProvisioningOperation> event) {
		SysProvisioningOperation provisioningOperation = event.getContent();
		IdmIdentity identity = identityService.get(provisioningOperation.getEntityIdentifier());
		for (IcAttribute attribute : provisioningOperationService.getFullConnectorObject(provisioningOperation).getAttributes()) {
			// TODO: send password always, when create?
			if (attribute instanceof IcPasswordAttribute && attribute.getValue() != null) {
				GuardedString password = ((IcPasswordAttribute) attribute).getPasswordValue();
				// send message with new password to identity
				notificationManager.send(AccModuleDescriptor.TOPIC_NEW_PASSWORD, new IdmMessage.Builder()
						.setLevel(NotificationLevel.SUCCESS)
						.setSubject("Provisioning success")
						.setHtmlMessage(
								"Provisioning on system: " + provisioningOperation.getSystem().getName() + ", with uid " + provisioningOperation.getSystemEntityUid() + ", was success. There is your new awesome password: '" + password.asString() + "'").build(), identity);
				break;
			}
			
		}
		return new DefaultEventResult<>(event, this);
	}
	
	@Override
	public int getOrder() {
		// after create
		return 1000;
	}

}

@@ -2604,74 +2604,254 @@
 y = 
-identityService.get(provisioningOperation.getEntityIdentifier());%0A
+null;%0A%09%09if (provisioningOperation.getEntityIdentifier() != null) %7B%0A%09%09%09identity = identityService.get(provisioningOperation.getEntityIdentifier());%0A%09%09%7D%0A%09%09// TODO: identity or email null, send message to actual log user?%0A%09%09if (identity != null) %7B%0A%09
 %09%09fo
@@ -2974,16 +2974,17 @@
 )) %7B%0A%09%09%09
+%09
 // TODO:
@@ -3019,16 +3019,17 @@
 create?%0A
+%09
 %09%09%09if (a
@@ -3107,16 +3107,17 @@
 ) %7B%0A%09%09%09%09
+%09
 GuardedS
@@ -3191,16 +3191,17 @@
 ();%0A%09%09%09%09
+%09
 // send 
@@ -3238,16 +3238,17 @@
 dentity%0A
+%09
 %09%09%09%09noti
@@ -3335,24 +3335,25 @@
 der()%0A%09%09%09%09%09%09
+%09
 .setLevel(No
@@ -3377,16 +3377,17 @@
 UCCESS)%0A
+%09
 %09%09%09%09%09%09.s
@@ -3422,24 +3422,25 @@
 ess%22)%0A%09%09%09%09%09%09
+%09
 .setHtmlMess
@@ -3444,16 +3444,17 @@
 essage(%0A
+%09
 %09%09%09%09%09%09%09%09
@@ -3697,16 +3697,17 @@
 y);%0A%09%09%09%09
+%09
 break;%0A%09
@@ -3712,13 +3712,20 @@
 %0A%09%09%09
+%09
 %7D%0A%09%09%09
+%09%0A%09%09%09%7D
 %0A%09%09%7D

check to empty email or identity