package org.openengsb.domains.notification.email.internal;

import java.util.Map;

import org.openengsb.core.config.ServiceInstanceFactory;
import org.openengsb.core.config.descriptor.AttributeDefinition;
import org.openengsb.core.config.descriptor.ServiceDescriptor;
import org.openengsb.domains.notification.NotificationDomain;
import org.openengsb.domains.notification.email.internal.abstraction.MailAbstraction;

public class EmailNotifierFactory implements ServiceInstanceFactory<NotificationDomain, EmailNotifier> {

    private final MailAbstraction mailAbstraction;

    public EmailNotifierFactory(MailAbstraction mailAbstraction) {
        this.mailAbstraction = mailAbstraction;
    }

    private void setAttributesOnNotifier(Map<String, String> attributes, EmailNotifier notifier) {

        if (attributes.containsKey("user")) {
            notifier.getProperties().setUser(attributes.get("user"));
        }
        if (attributes.containsKey("password")) {
            notifier.getProperties().setPassword(attributes.get("password"));
        }
        if (attributes.containsKey("smtpAuth")) {
            notifier.getProperties().setSmtpAuth(attributes.get("smtpAuth"));
        }
        if (attributes.containsKey("smtpSender")) {
            notifier.getProperties().setSender(attributes.get("smtpSender"));
        }
        if (attributes.containsKey("smtpHost")) {
            notifier.getProperties().setSmtpHost(attributes.get("smtpHost"));
        }
        if (attributes.containsKey("smtpPort")) {
            notifier.getProperties().setSmtpPort(attributes.get("smtpPort"));
        }
    }

    @Override
    public ServiceDescriptor getDescriptor(ServiceDescriptor.Builder builder) {
        builder.name("email.name").description("email.description");

        builder.attribute(buildAttribute(builder, "user", "username.outputMode", "username.outputMode.description"))
                .attribute(
                        buildAttribute(builder, "password", "password.outputMode", "password.outputMode.description"))
                .attribute(
                        buildAttribute(builder, "smtpAuth", "mail.smtp.auth.outputMode",
                                "mail.smtp.auth.outputMode.description"))
                .attribute(
                        buildAttribute(builder, "smtpSender", "mail.smtp.sender.outputMode",
                                "mail.smtp.sender.outputMode.description"))
                .attribute(
                        buildAttribute(builder, "smtpPort", "mail.smtp.port.outputMode",
                                "mail.smtp.port.outputMode.description"))
                .attribute(
                        buildAttribute(builder, "smtpHost", "mail.smtp.host.outputMode",
                                "mail.smtp.host.outputMode.description")).build();

        return builder.build();
    }

    private AttributeDefinition buildAttribute(ServiceDescriptor.Builder builder, String id, String nameId,
            String descriptionId) {
        return builder.newAttribute().id(id).name(nameId).description(descriptionId).defaultValue("").required()
                .build();

    }

    @Override
    public void updateServiceInstance(EmailNotifier instance, Map<String, String> attributes) {
        setAttributesOnNotifier(attributes, instance);
    }

    @Override
    public EmailNotifier createServiceInstance(String id, Map<String, String> attributes) {
        EmailNotifier notifier = new EmailNotifier(id, mailAbstraction);
        setAttributesOnNotifier(attributes, notifier);
        return notifier;
    }
}

@@ -1945,32 +1945,38 @@
            build
+er.new
 Attribute(builde
@@ -1969,25 +1969,21 @@
 tribute(
-builder, 
+).id(
 %22passwor
@@ -1984,18 +1984,23 @@
 assword%22
-, 
+).name(
 %22passwor
@@ -2016,43 +2016,137 @@
 ode%22
-, %22password.outputMode.description%22
+).description(%0A                                %22password.outputMode.description%22).defaultValue(%22%22).required().asPassword().build(
 ))%0A 
@@ -2334,33 +2334,16 @@
 ption%22))
-%0A                
 .attribu
@@ -2514,33 +2514,16 @@
 ption%22))
-%0A                
 .attribu
@@ -2688,33 +2688,16 @@
 ption%22))
-%0A                
 .attribu

Update email connector attribute specification to call for a password instead of a text field.