package info.limpet.analysis;

import info.limpet.ICollection;
import info.limpet.IStore.IStoreItem;
import info.limpet.data.impl.ObjectCollection;
import info.limpet.data.operations.CollectionComplianceTests;

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

import org.opengis.geometry.Geometry;

public abstract class SimpleDescriptiveObject extends CoreAnalysis
{

	final CollectionComplianceTests aTests;

	public SimpleDescriptiveObject()
	{
		super("Object Analysis");
		aTests = new CollectionComplianceTests();
	}


	@Override
	public void analyse(List<IStoreItem> selection)
	{
		List<String> titles = new ArrayList<String>();
		List<String> values = new ArrayList<String>();

		// check compatibility
		if (appliesTo(selection))
		{
			if (selection.size() == 1)
			{
				// ok, let's go for it.
				for (Iterator<IStoreItem> iter = selection.iterator(); iter.hasNext();)
				{					
					ICollection thisC = (ICollection) iter.next();
					ObjectCollection<?> o = (ObjectCollection<?>) thisC;

					titles.add("Content Type");
					Object nextObject = o.getValues().iterator().next();
					values.add(typeFor(nextObject, nextObject.getClass()));
				}
			}
		}

		if (titles.size() > 0)
			presentResults(titles, values);

	}
	
	public String typeFor(Object subject, Object oClass)
	{
		String res = "un-recognised";
		
		if(oClass.equals(String.class))
		{
			res = "String";
		}
		else if(subject instanceof Geometry)
		{
			res = "Location";
		}
		
		return res;
	}

	private boolean appliesTo(List<IStoreItem> selection)
	{
		return aTests.allCollections(selection) &&  aTests.allNonQuantity(selection);
	}

	abstract protected void presentResults(List<String> titles,
			List<String> values);
}

@@ -547,17 +547,16 @@
 ();%0A%09%7D%0A%0A
-%0A
 %09@Overri
@@ -910,21 +910,16 @@
 ;)%0A%09%09%09%09%7B
-%09%09%09%09%09
 %0A%09%09%09%09%09IC
@@ -1022,16 +1022,78 @@
 thisC;%0A%0A
+%09%09%09%09%09// check it has some data%0A%09%09%09%09%09if (o.size() %3E 0)%0A%09%09%09%09%09%7B%0A%09
 %09%09%09%09%09tit
@@ -1117,16 +1117,17 @@
 Type%22);%0A
+%09
 %09%09%09%09%09Obj
@@ -1181,16 +1181,17 @@
 );%0A%09%09%09%09%09
+%09
 values.a
@@ -1238,16 +1238,23 @@
 ss()));%0A
+%09%09%09%09%09%7D%0A
 %09%09%09%09%7D%0A%09%09
@@ -1325,17 +1325,16 @@
 s);%0A%0A%09%7D%0A
-%09
 %0A%09public
@@ -1419,15 +1419,14 @@
 d%22;%0A
-%09%09
 %0A%09%09if
+ 
 (oCl
@@ -1487,16 +1487,17 @@
 %09else if
+ 
 (subject
@@ -1547,18 +1547,16 @@
 n%22;%0A%09%09%7D%0A
-%09%09
 %0A%09%09retur
@@ -1668,17 +1668,16 @@
 tion) &&
- 
  aTests.

handle zero length object collections