package info.limpet.data.operations;

import info.limpet.ICommand;
import info.limpet.IOperation;
import info.limpet.IStore;
import info.limpet.IStore.IStoreItem;
import info.limpet.data.commands.AbstractCommand;
import info.limpet.data.store.InMemoryStore.StoreGroup;

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

public class AddLayerOperation implements IOperation<IStoreItem>
{

	public static interface StringProvider
	{
		public String getString(String title);
	}

	private final StringProvider _stringProvider;

	public AddLayerOperation(StringProvider stringProvider)
	{
		_stringProvider = stringProvider;
	}

	public Collection<ICommand<IStoreItem>> actionsFor(
			List<IStoreItem> selection, IStore destination)
	{
		Collection<ICommand<IStoreItem>> res = new ArrayList<ICommand<IStoreItem>>();
		if (appliesTo(selection))
		{
			String thisTitle = "Add new layer";
			ICommand<IStoreItem> newC = new AddLayerCommand(thisTitle, destination,
					_stringProvider);
			res.add(newC);
		}

		return res;
	}

	private boolean appliesTo(List<IStoreItem> selection)
	{
		return true;
	}

	public static class AddLayerCommand extends AbstractCommand<IStoreItem>
	{
		final StringProvider _stringProvider;

		public AddLayerCommand(String title, IStore store,
				StringProvider stringProvider)
		{
			super(title, "Add a new layer", null, store, false, false, null);
			_stringProvider = stringProvider;
		}

		@Override
		public void execute()
		{
			// get the String
			String string = _stringProvider.getString("Name for new layer");

			if (string != null)
			{
				StoreGroup group = new StoreGroup(string);
				getStore().add(group);
			}
		}

		@Override
		protected void recalculate()
		{
			// don't worry
		}

	}

}

@@ -866,24 +866,30 @@
 on))%0A%09%09%7B%0A%09%09%09
+final 
 String thisT
@@ -919,29 +919,254 @@
 %0A%09%09%09
-ICommand%3CIStoreItem%3E 
+// hmm, see if a group has been selected%0A%09%09%09ICommand%3CIStoreItem%3E newC = null;%0A%09%09%09if (selection.size() == 1)%0A%09%09%09%7B%0A%09%09%09%09IStoreItem first = selection.get(0);%0A%09%09%09%09if (first instanceof StoreGroup)%0A%09%09%09%09%7B%0A%09%09%09%09%09StoreGroup group = (StoreGroup) first;%0A%09%09%09%09%09
 newC
@@ -1198,16 +1198,23 @@
 isTitle,
+ group,
  destina
@@ -1219,16 +1219,18 @@
 nation,%0A
+%09%09
 %09%09%09%09%09_st
@@ -1244,16 +1244,160 @@
 vider);%0A
+%09%09%09%09%7D%0A%09%09%09%7D%0A%0A%09%09%09if (newC == null)%0A%09%09%09%7B%0A%09%09%09%09newC = new AddLayerCommand(thisTitle, destination, _stringProvider);%0A%09%09%09%7D%0A%0A%09%09%09if (newC != null)%0A%09%09%09%7B%0A%09
 %09%09%09res.a
@@ -1406,16 +1406,21 @@
 (newC);%0A
+%09%09%09%7D%0A
 %09%09%7D%0A%0A%09%09r
@@ -1626,16 +1626,45 @@
 rovider;
+%0A%09%09private StoreGroup _group;
 %0A%0A%09%09publ
@@ -1856,24 +1856,197 @@
 vider;%0A%09%09%7D%0A%0A
+%09%09public AddLayerCommand(String title, StoreGroup group, IStore store,%0A%09%09%09%09StringProvider stringProvider)%0A%09%09%7B%0A%09%09%09this(title, store, stringProvider);%0A%09%09%09_group = group;%0A%09%09%7D%0A%0A
 %09%09@Override%0A
@@ -2202,25 +2202,28 @@
 %09StoreGroup 
-g
+newG
 roup = new S
@@ -2241,16 +2241,96 @@
 tring);%0A
+%0A%09%09%09%09if (_group != null)%0A%09%09%09%09%7B%0A%09%09%09%09%09_group.add(newGroup);%0A%09%09%09%09%7D%0A%09%09%09%09else%0A%09%09%09%09%7B%0A%09
 %09%09%09%09getS
@@ -2344,15 +2344,25 @@
 add(
-g
+newG
 roup);
+%0A%0A%09%09%09%09%7D
 %0A%09%09%09

allow layers in layers