/**
* Copyright 2016 PT Inovação e Sistemas SA
* Copyright 2016 INESC-ID
* Copyright 2016 QUOBIS NETWORKS SL
* Copyright 2016 FRAUNHOFER-GESELLSCHAFT ZUR FOERDERUNG DER ANGEWANDTEN FORSCHUNG E.V
* Copyright 2016 ORANGE SA
* Copyright 2016 Deutsche Telekom AG
* Copyright 2016 Apizee
* Copyright 2016 TECHNISCHE UNIVERSITAT BERLIN
*
* 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 eu.rethink.mn.component;

import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import eu.rethink.mn.IComponent;
import eu.rethink.mn.pipeline.PipeContext;
import eu.rethink.mn.pipeline.PipeRegistry;
import eu.rethink.mn.pipeline.message.PipeMessage;
import eu.rethink.mn.pipeline.message.ReplyCode;

/**
 * @author micaelpedrosa@gmail.com
 * Address allocation manager for objects.
 */
public class ObjectAllocationManager implements IComponent {
	final String name;
	final PipeRegistry register;

	final String baseURL;

	public ObjectAllocationManager(PipeRegistry register) {
		this.register = register;
		this.name = "domain://msg-node." + register.getDomain()  + "/object-address-allocation";
		this.baseURL = "://" + register.getDomain() + "/";
	}
	
	@Override
	public String getName() { return name; }
	
	@Override
	public void handle(PipeContext ctx) {
		final PipeMessage msg = ctx.getMessage();
		final JsonObject body = msg.getBody();
		
		if(msg.getType().equals("create")) {
			//process JSON msg requesting a number of available addresses
			final String scheme = body.getString("scheme");
			
			//on value
			final JsonObject msgBodyValue = body.getJsonObject("value");
			final int number = msgBodyValue.getInteger("number", 5);
			
			final List<String> allocated = allocate(ctx, scheme, number);
		
			final PipeMessage reply = new PipeMessage();
			reply.setId(msg.getId());
			reply.setFrom(name);
			reply.setTo(msg.getFrom());
			reply.setReplyCode(ReplyCode.OK);
			
			final JsonObject value = new JsonObject();
			value.put("allocated", new JsonArray(allocated));
			
			reply.getBody().put("value", value);
			
			ctx.reply(reply);
		} else if(msg.getType().equals("delete")) {
			//process JSON msg releasing an address
			final String resource = body.getString("resource");
			
			deallocate(ctx, resource);
			
			ctx.replyOK(name);
		}
	}

	private List<String> allocate(PipeContext ctx, String scheme, int number) {
		final ArrayList<String> list = new ArrayList<String>(number);
		int i = 0;
		while(i < number) {
			//find unique url, not in registry...
			final String url = scheme + baseURL + UUID.randomUUID().toString();
			if(ctx.getSession().allocate(url + "/subscription")) {
				list.add(url);
				i++;
			}
		}
		
		return list;
	}
	
	private void deallocate(PipeContext ctx, String url) {
		ctx.getSession().deallocate(url + "/subscription");
	}
}

@@ -1732,18 +1732,17 @@
 %22/%22;%0A%09%7D%0A
-%09
 %0A
+
 %09@Overri
@@ -1786,18 +1786,17 @@
 name; %7D%0A
-%09
 %0A
+
 %09@Overri
@@ -1922,19 +1922,17 @@
 Body();%0A
-%09%09
 %0A
+
 %09%09if(msg
@@ -2078,20 +2078,17 @@
 heme%22);%0A
-%09%09%09
 %0A
+
 %09%09%09//on 
@@ -2217,20 +2217,17 @@
 r%22, 5);%0A
-%09%09%09
 %0A
+
 %09%09%09final
@@ -2283,19 +2283,17 @@
 umber);%0A
-%09%09
 %0A
+
 %09%09%09final
@@ -2453,20 +2453,17 @@
 de.OK);%0A
-%09%09%09
 %0A
+
 %09%09%09final
@@ -2553,20 +2553,17 @@
 ated));%0A
-%09%09%09
 %0A
+
 %09%09%09reply
@@ -2590,28 +2590,25 @@
 e%22, value);%0A
-%09%09%09
 %0A
+
 %09%09%09ctx.reply
@@ -2760,20 +2760,17 @@
 urce%22);%0A
-%09%09%09
 %0A
+
 %09%09%09deall
@@ -2795,12 +2795,9 @@
 e);%0A
-%09%09%09
 %0A
+
 %09%09%09c
@@ -3145,34 +3145,16 @@
 cate(url
- + %22/subscription%22
 )) %7B%0A%09%09%09
@@ -3191,11 +3191,9 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%09re
@@ -3210,10 +3210,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09pri
@@ -3263,16 +3263,16 @@
  url) %7B%0A
+
 %09%09ctx.ge
@@ -3300,26 +3300,8 @@
 (url
- + %22/subscription%22
 );%0A%09

fix on allocate and deallocate