/*
 * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hazelcast.config;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import static com.hazelcast.util.ValidationUtil.hasText;

public class ConsulConfig extends AbstractXmlConfigHelper{

    private static final int CONNECTION_TIMEOUT = 5;
    private static final int DEFAULT_VALUE = 5;

    private String name;
    private String host;
    private boolean enabled;
    private String type;
    private Node node;

    private int connectionTimeoutSeconds = CONNECTION_TIMEOUT;

    public ConsulConfig(SpiJoinerConfig joinerConfig) {
        this.enabled = joinerConfig.isEnabled();
        this.type = joinerConfig.getType();
        this.node = joinerConfig.getNode();
        handleConsul(node);
    }

    /**
     * Returns the connection timeout.
     *
     * @return the connectionTimeoutSeconds
     * @see #setConnectionTimeoutSeconds(int)
     */
    public int getConnectionTimeoutSeconds() {
        return connectionTimeoutSeconds;
    }

    /**
     * Sets the connection timeout. This is the maximum amount of time Hazelcast
     * will try to connect to a well known member before giving up. Setting it
     * to a too low value could mean that a member is not able to connect to a
     * cluster. Setting it as too high a value means that member startup could
     * slow down because of longer timeouts (e.g. when a well known member is
     * not up).
     *
     * @param connectionTimeoutSeconds the connection timeout in seconds.
     * @return the updated TcpIpConfig
     * @throws IllegalArgumentException if connectionTimeoutSeconds is smaller
     *                                  than 0.
     * @see #getConnectionTimeoutSeconds()
     */
    public ConsulConfig setConnectionTimeoutSeconds(final int connectionTimeoutSeconds) {
        if (connectionTimeoutSeconds < 0) {
            throw new IllegalArgumentException("connection timeout can't be smaller than 0");
        }
        this.connectionTimeoutSeconds = connectionTimeoutSeconds;
        return this;
    }

    /**
     * Checks if the Consul join mechanism is enabled.
     *
     * @return true if enabled, false otherwise
     */
    public boolean isEnabled() {
        return enabled;
    }

    /**
     * Enables or disables the Consul join mechanism.
     *
     * @param enabled true to enable the Consul join mechanism, false to disable
     * @return ConsulConfig the updated ConsulConfig config.
     */
    public ConsulConfig setEnabled(final boolean enabled) {
        this.enabled = enabled;
        return this;
    }

    /**
     * Set the serviceName
     *
     * @param name the service name to add.
     * @return the updated configuration.
     * @throws IllegalArgumentException if name is null or empty.
     */
    public ConsulConfig setName(String name) {
        String nameText = hasText(name, "name");
        this.name = nameText.trim();
        return this;
    }

    public String getName() {
        return this.name;
    }

    /**
     * Set the consul hostname
     *
     * @param host the host, including optional port
     * @return the updated configuration.
     * @throws IllegalArgumentException if host is null or empty.
     */
    public ConsulConfig setHost(String host) {
        String nameText = hasText(host, "host");
        this.host = nameText.trim();
        return this;
    }

    public String getHost() {
        return this.host;
    }

    @Override
    public String toString() {
        return "Consul [enabled=" + enabled
                + ", connectionTimeoutSeconds=" + connectionTimeoutSeconds
                + ", name=" + this.name
                + "]";
    }

    private void handleConsul(final org.w3c.dom.Node node) {
        final NamedNodeMap atts = node.getAttributes();
        for (int a = 0; a < atts.getLength(); a++) {
            final org.w3c.dom.Node att = atts.item(a);
            final String value = getTextContent(att).trim();

            if (att.getNodeName().equals("connection-timeout-seconds")) {
                setConnectionTimeoutSeconds(getIntegerValue("connection-timeout-seconds", value, DEFAULT_VALUE));
            }
        }
        for (org.w3c.dom.Node n : new AbstractXmlConfigHelper.IterableNodeList(node.getChildNodes())) {
            final String value = getTextContent(n).trim();
            if ("name".equals(cleanNodeName(n.getNodeName()))) {
                setName(value);
            } else if ("host".equals(cleanNodeName(n.getNodeName()))) {
                setHost(value);
            }
        }
    }
}

@@ -982,85 +982,8 @@
 ost;
-%0A    private boolean enabled;%0A    private String type;%0A    private Node node;
 %0A%0A  
@@ -1112,162 +1112,46 @@
     
-this.enabled = joinerConfig.isEnabled();%0A        this.type = joinerConfig.getType();%0A        this.node = joinerConfig.getNode();%0A        handleConsul(node
+handleConsul(joinerConfig.getXmlNode()
 );%0A 
@@ -2457,537 +2457,8 @@
  %7D%0A%0A
-    /**%0A     * Checks if the Consul join mechanism is enabled.%0A     *%0A     * @return true if enabled, false otherwise%0A     */%0A    public boolean isEnabled() %7B%0A        return enabled;%0A    %7D%0A%0A    /**%0A     * Enables or disables the Consul join mechanism.%0A     *%0A     * @param enabled true to enable the Consul join mechanism, false to disable%0A     * @return ConsulConfig the updated ConsulConfig config.%0A     */%0A    public ConsulConfig setEnabled(final boolean enabled) %7B%0A        this.enabled = enabled;%0A        return this;%0A    %7D%0A%0A
     
@@ -3399,23 +3399,20 @@
 led=%22 + 
-enabled
+true
 %0A       
@@ -3585,34 +3585,25 @@
 l(final 
-org.w3c.dom.
 Node 
-n
+xmlN
 ode) %7B%0A 
@@ -3635,17 +3635,20 @@
  atts = 
-n
+xmlN
 ode.getA
@@ -3731,28 +3731,16 @@
   final 
-org.w3c.dom.
 Node att
@@ -4043,28 +4043,16 @@
    for (
-org.w3c.dom.
 Node n :
@@ -4097,17 +4097,20 @@
 odeList(
-n
+xmlN
 ode.getC

remove unused fields: type, enabled, node