/**
Copyright 2013 project Ardulink http://www.ardulink.org/
 
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 org.ardulink.core.mqtt;

import static org.ardulink.util.Preconditions.checkNotNull;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.ardulink.core.linkmanager.LinkConfig;
import org.ardulink.core.linkmanager.LinkConfig.I18n;

/**
 * [ardulinktitle] [ardulinkversion]
 * 
 * project Ardulink http://www.ardulink.org/
 * 
 * [adsense]
 *
 */
@I18n("message")
public class MqttLinkConfig implements LinkConfig {

	public static final int DEFAULT_PORT = 1883;

	public enum Connection {
		TCP, SSL, TLS;
	}

	private static final String LOCALHOST = "localhost";

	@Named("host")
	@NotNull
	private String host = LOCALHOST;

	@Named("port")
	@Min(1)
	@Max(2 << 16 - 1)
	private int port = DEFAULT_PORT;

	@Named("connection")
	private Connection connection = Connection.TCP;

	@Named("topic")
	@NotNull
	private String topic = normalize("home/devices/ardulink/");

	@Named("clientId")
	@NotNull
	private String clientId = "ardulink-mqtt-link";

	@Named("user")
	private String user;

	@Named("password")
	private String password;

	@Named("separatedTopics")
	private boolean separateTopics;

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host == null ? LOCALHOST : host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public Connection getConnection() {
		return connection;
	}

	public void setConnection(Connection connection) {
		this.connection = checkNotNull(connection,
				"connection must not be null");
	}

	public String getTopic() {
		return topic;
	}

	public void setTopic(String topic) {
		this.topic = normalize(checkNotNull(topic, "topic must not be null"));
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = checkNotNull(clientId, "clientId must not be null");
	}

	public String getUser() {
		return this.user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return this.password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isSeparateTopics() {
		return this.separateTopics;
	}

	public void setSeparateTopics(boolean separateTopics) {
		this.separateTopics = separateTopics;
	}

	private static String normalize(String topic) {
		return topic.endsWith("/") ? topic : topic + "/";
	}

}

@@ -1264,21 +1264,24 @@
  String 
-LOCAL
+DEFAULT_
 HOST = %22
@@ -1346,21 +1346,24 @@
  host = 
-LOCAL
+DEFAULT_
 HOST;%0D%0A%0D
@@ -1412,22 +1412,21 @@
 - 1)%0D%0A%09p
-rivate
+ublic
  int por
@@ -1610,24 +1610,80 @@
 link/%22);%0D%0A%0D%0A
+%09@Named(%22qos%22)%0D%0A%09@Min(0)%0D%0A%09@Max(2)%0D%0A%09public int qos;%0D%0A%0D%0A
 %09@Named(%22cli
@@ -1769,30 +1769,29 @@
 (%22user%22)%0D%0A%09p
-rivate
+ublic
  String user
@@ -1814,30 +1814,29 @@
 ssword%22)%0D%0A%09p
-rivate
+ublic
  String pass
@@ -1874,22 +1874,21 @@
 cs%22)%0D%0A%09p
-rivate
+ublic
  boolean
@@ -2027,13 +2027,16 @@
 l ? 
-LOCAL
+DEFAULT_
 HOST
@@ -2055,116 +2055,8 @@
 %0D%0A%0D%0A
-%09public int getPort() %7B%0D%0A%09%09return port;%0D%0A%09%7D%0D%0A%0D%0A%09public void setPort(int port) %7B%0D%0A%09%09this.port = port;%0D%0A%09%7D%0D%0A%0D%0A
 %09pub
@@ -2218,14 +2218,9 @@
 ion,
-%0D%0A%09%09%09%09
+ 
 %22con
@@ -2610,450 +2610,8 @@
 %0D%0A%0D%0A
-%09public String getUser() %7B%0D%0A%09%09return this.user;%0D%0A%09%7D%0D%0A%0D%0A%09public void setUser(String user) %7B%0D%0A%09%09this.user = user;%0D%0A%09%7D%0D%0A%0D%0A%09public String getPassword() %7B%0D%0A%09%09return this.password;%0D%0A%09%7D%0D%0A%0D%0A%09public void setPassword(String password) %7B%0D%0A%09%09this.password = password;%0D%0A%09%7D%0D%0A%0D%0A%09public boolean isSeparateTopics() %7B%0D%0A%09%09return this.separateTopics;%0D%0A%09%7D%0D%0A%0D%0A%09public void setSeparateTopics(boolean separateTopics) %7B%0D%0A%09%09this.separateTopics = separateTopics;%0D%0A%09%7D%0D%0A%0D%0A
 %09pri

support for setting qos