/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2015, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.wildfly.extension.clustering.singleton;

import static org.wildfly.extension.clustering.singleton.ElectionPolicyResourceDefinition.Attribute.*;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.jboss.as.clustering.controller.CommonUnaryRequirement;
import org.jboss.as.clustering.controller.ResourceServiceBuilder;
import org.jboss.as.clustering.dmr.ModelNodes;
import org.jboss.as.controller.OperationContext;
import org.jboss.as.controller.OperationFailedException;
import org.jboss.as.controller.PathAddress;
import org.jboss.as.network.OutboundSocketBinding;
import org.jboss.dmr.ModelNode;
import org.jboss.msc.service.ServiceBuilder;
import org.jboss.msc.service.ServiceController;
import org.jboss.msc.service.ServiceTarget;
import org.jboss.msc.service.ValueService;
import org.jboss.msc.value.Value;
import org.wildfly.clustering.service.Builder;
import org.wildfly.clustering.service.Dependency;
import org.wildfly.clustering.service.InjectedValueDependency;
import org.wildfly.clustering.singleton.SingletonElectionPolicy;
import org.wildfly.clustering.singleton.election.NamePreference;
import org.wildfly.clustering.singleton.election.Preference;
import org.wildfly.clustering.singleton.election.PreferredSingletonElectionPolicy;
import org.wildfly.extension.clustering.singleton.election.OutboundSocketBindingPreference;

/**
 * Builds a service that provides an election policy.
 * @author Paul Ferraro
 */
public abstract class ElectionPolicyBuilder extends ElectionPolicyServiceNameProvider implements ResourceServiceBuilder<SingletonElectionPolicy>, Value<SingletonElectionPolicy> {

    private final List<Preference> preferences = new CopyOnWriteArrayList<>();
    private final List<Dependency> dependencies = new CopyOnWriteArrayList<>();

    protected ElectionPolicyBuilder(PathAddress policyAddress) {
        super(policyAddress);
    }

    @Override
    public ServiceBuilder<SingletonElectionPolicy> build(ServiceTarget target) {
        final List<Preference> preferences = this.preferences;
        Value<SingletonElectionPolicy> value = new Value<SingletonElectionPolicy>() {
            @Override
            public SingletonElectionPolicy getValue() {
                SingletonElectionPolicy policy = ElectionPolicyBuilder.this.getValue();
                return preferences.isEmpty() ? policy : new PreferredSingletonElectionPolicy(policy, preferences);
            }
        };
        ServiceBuilder<SingletonElectionPolicy> builder = target.addService(this.getServiceName(), new ValueService<>(value)).setInitialMode(ServiceController.Mode.ON_DEMAND);
        for (Dependency dependency : this.dependencies) {
            dependency.register(builder);
        }
        return builder;
    }

    @Override
    public Builder<SingletonElectionPolicy> configure(OperationContext context, ModelNode model) throws OperationFailedException {
        this.preferences.clear();
        this.dependencies.clear();
        ModelNodes.optionalList(SOCKET_BINDING_PREFERENCES.resolveModelAttribute(context, model)).ifPresent(bindings -> {
            bindings.stream().map(ModelNode::asString).forEach(bindingName -> {
                InjectedValueDependency<OutboundSocketBinding> binding = new InjectedValueDependency<>(CommonUnaryRequirement.OUTBOUND_SOCKET_BINDING.getServiceName(context, bindingName), OutboundSocketBinding.class);
                this.preferences.add(new OutboundSocketBindingPreference(binding));
                this.dependencies.add(binding);
            });
        });
        ModelNodes.optionalList(NAME_PREFERENCES.resolveModelAttribute(context, model)).ifPresent(names -> {
            names.stream().map(ModelNode::asString).forEach(name -> this.preferences.add(new NamePreference(name)));
        });
        return this;
    }
}

@@ -3036,338 +3036,58 @@
     
-final List%3CPreference%3E preferences = this.preferences;%0A        Value%3CSingletonElectionPolicy%3E value = new Value%3CSingletonElectionPolicy%3E() %7B%0A            @Override%0A            public SingletonElectionPolicy getValue() %7B%0A                SingletonElectionPolicy policy = ElectionPolicyBuilder.this.getValue();%0A                return 
+Value%3CSingletonElectionPolicy%3E value = () -%3E this.
 pref
@@ -3106,22 +3106,31 @@
 pty() ? 
-policy
+this.getValue()
  : new P
@@ -3165,53 +3165,42 @@
 icy(
-policy, preferences);%0A            %7D%0A        %7D
+this.getValue(), this.preferences)
 ;%0A  
@@ -3385,69 +3385,47 @@
     
-for (Dependency dependency : this.dependencies) %7B%0A           
+this.dependencies.forEach(dependency -%3E
  dep
@@ -3449,27 +3449,18 @@
 builder)
+)
 ;
-%0A        %7D
 %0A       

Simplify builder implementation.