package com.bluesky.visualprogramming.core.nativeImpl.proto;

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

import com.bluesky.visualprogramming.core.ExtendedObjectFactory;
import com.bluesky.visualprogramming.core.Field;
import com.bluesky.visualprogramming.core.ObjectScope;
import com.bluesky.visualprogramming.core.ObjectType;
import com.bluesky.visualprogramming.core.Prototype;
import com.bluesky.visualprogramming.core._Object;
import com.bluesky.visualprogramming.core.nativeproc.NativeMethodSupport;
import com.bluesky.visualprogramming.core.nativeproc.ParameterList;
import com.bluesky.visualprogramming.core.value.IntegerValue;
import com.bluesky.visualprogramming.core.value.StringValue;
import com.bluesky.visualprogramming.vm.VirtualMachine;

public class MapProto extends NativeMethodSupport {
	static String MapItemPrefix = "KEY_";

	@ParameterList({ "self", "key", "value" })
	public static void put(_Object self, StringValue key, _Object value) {

		String strkey = key.getValue();
		// take owner ship
		if (value.hasOwner())
			value.downgradeLinkFromOwner();

		self.setField(getItemName(strkey), value, true);

	}

	@ParameterList({ "self", "key" })
	public static _Object get(_Object self, StringValue key) {
		String strKey = key.getValue();
		return self.getChild(getItemName(strKey));

	}

	@ParameterList({ "self", "key" })
	public static boolean contains(_Object self, StringValue key) {
		String strKey = key.getValue();
		_Object value = self.getChild(getItemName(strKey));
		return value != null;

	}

	protected static String getItemName(String key) {
		return MapItemPrefix + key;
	}

	@ParameterList({ "self", "key" })
	public static void remove(_Object self, StringValue key) {
		String strKey = key.getValue();
		self.removeField(getItemName(strKey));
	}

	@ParameterList({ "self" })
	public static void clear(_Object self) {

		List<Field> listElements = new ArrayList<Field>();

		for (Field f : self.getFields()) {
			if (f.getName().startsWith(MapItemPrefix))
				listElements.add(f);
		}

		for (Field f : listElements)
			self.removeField(f.name);

	}

	@ParameterList({ "self" })
	public static _Object size(_Object self) {

		int size = self.getUserFieldsCount();

		IntegerValue result = getObjectFactory().createInteger(size);

		return result;
	}

	private static String getKey(String itemName) {
		return itemName.substring(MapItemPrefix.length());
	}

	@ParameterList({ "self" })
	public static _Object getKeyList(_Object self) {

		_Object listObject = ExtendedObjectFactory.create(Prototype.List);

		for (Field f : self.getFields()) {
			if (f.getName().startsWith(MapItemPrefix)) {
				String key = getKey(f.name);

				StringValue keySV = getObjectFactory().createString(key);
				ListProto.add(listObject, keySV);
			}
		}

		return listObject;
	}
}

@@ -225,117 +225,8 @@
 ld;%0A
-import com.bluesky.visualprogramming.core.ObjectScope;%0Aimport com.bluesky.visualprogramming.core.ObjectType;%0A
 impo
@@ -274,16 +274,16 @@
 totype;%0A
+
 import c
@@ -463,24 +463,86 @@
 ameterList;%0A
+import com.bluesky.visualprogramming.core.value.BooleanValue;%0A
 import com.b
@@ -655,64 +655,8 @@
 lue;
-%0Aimport com.bluesky.visualprogramming.vm.VirtualMachine;
 %0A%0Apu
@@ -1266,15 +1266,20 @@
 tic 
-b
+B
 oolean
+Value
  con
@@ -1409,30 +1409,97 @@
 ));%0A
+%0A
 %09%09
-return value != null
+BooleanValue result = getObjectFactory().createBoolean(value != null);%0A%09%09return result
 ;%0A%0A%09

fix MapProto bug