package cz.xtf.jolokia;

import io.fabric8.kubernetes.api.model.Pod;

/**
 * "%s/api/v1/namespaces/%s/pods/https:%s:8778/proxy/
 * jolokia/exec/org.apache.activemq:type=Broker,brokerName=%3$s,destinationType=Queue,destinationName=%s/browse%28%29",
 *
 * @author David Simansky | dsimansk@redhat.com
 */
public class JolokiaRequestBuilder {

	private String masterUrl;
	private String namespace;
	private Pod pod;
	private String beanName;
	private AmqJolokiaRequest amqReq;

	public static JolokiaRequestBuilder newRequest() {
		return new JolokiaRequestBuilder();
	}

	public JolokiaRequestBuilder withMasterUrl(String masterUrl) {
		this.masterUrl = masterUrl;
		return this;
	}

	public JolokiaRequestBuilder withNamespace(String namespace) {
		this.namespace = namespace;
		return this;
	}

	public JolokiaRequestBuilder withPod(Pod pod) {
		this.pod = pod;
		return this;
	}

	public AmqJolokiaRequest withAmq() {
		this.beanName = "org.apache.activemq:type=Broker";
		this.amqReq = new AmqJolokiaRequest(this);
		return this.amqReq;
	}

	public JolokiaRequestBuilder endRequest() {
		return this;
	}

	public String build() {
		StringBuilder sb = new StringBuilder(masterUrl)
				.append("/api/v1/namespaces/").append(namespace).append("/pods")
				.append("/https:").append(pod.getMetadata().getName()).append(":8778/proxy")
				.append("/jolokia")
				.append("/exec/")
				.append(beanName);
		if (amqReq != null) {
			sb.append(",brokerName=").append(pod.getMetadata().getName())
					.append(",destinationType=").append(this.amqReq.destinationType)
					.append(",destinationName=").append(this.amqReq.destinationName)
					.append("/").append(this.amqReq.execMethod).append("%28%29");
		}
		return sb.toString();
	}

	public class AmqJolokiaRequest {
		String destinationName;
		String destinationType;
		String execMethod;

		JolokiaRequestBuilder parent;

		AmqJolokiaRequest(JolokiaRequestBuilder parent) {
			this.parent = parent;
		}

		public AmqJolokiaRequest withQueue(String name) {
			destinationType = "Queue";
			destinationName = name;
			return this;
		}

		public AmqJolokiaRequest withExecMethod(String execMethod) {
			this.execMethod = execMethod;
			return this;
		}

		public JolokiaRequestBuilder endAmq() {
			return parent;
		}
	}
}

@@ -1365,14 +1365,58 @@
 end(
-%22/exec
+this.amqReq.execMethod != null ? %22/exec/%22 : %22/read
 /%22)%0A
@@ -1688,57 +1688,171 @@
 %22/%22)
-.append(this.amqReq.execMethod).append(%22%2528%2529%22);
+;%0A%09%09%09%09%09if (this.amqReq.execMethod != null) %7B%0A%09%09%09%09%09%09sb.append(this.amqReq.execMethod).append(%22%2528%2529%22);%0A%09%09%09%09%09%7D else %7B%0A%09%09%09%09%09%09sb.append(this.amqReq.attribute);%0A%09%09%09%09%09%7D
 %0A%09%09%7D
@@ -1986,16 +1986,36 @@
 cMethod;
+%0A%09%09String attribute;
 %0A%0A%09%09Jolo
@@ -2312,24 +2312,24 @@
 ecMethod) %7B%0A
-
 %09%09%09this.exec
@@ -2362,32 +2362,144 @@
 turn this;%0A%09%09%7D%0A%0A
+%09%09public AmqJolokiaRequest withAttribute(String attribute)%7B%0A%09%09%09this.attribute = attribute;%0A%09%09%09return this;%0A%09%09%7D%0A%0A
 %09%09public Jolokia

Enhance JolokiaRequestBuilder to option to read attributes