package com.planetmayo.debrief.satc.gwt.client.ui;

import java.util.Collection;
import java.util.Iterator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.planetmayo.debrief.satc.model.generator.BoundedStatesListener;
import com.planetmayo.debrief.satc.model.states.BaseRange.IncompatibleStateException;
import com.planetmayo.debrief.satc.model.states.BoundedState;

public class TrackStates extends Composite implements BoundedStatesListener
{

	interface TrackStatesUiBinder extends UiBinder<Widget, TrackStates>
	{
	}

	private static TrackStatesUiBinder uiBinder = GWT
			.create(TrackStatesUiBinder.class);

	@UiField
	Grid grid;

	public TrackStates()
	{
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void addStates(String time, String location, String speed,
			String course)
	{
		int index = grid.insertRow(grid.getRowCount());
		grid.setWidget(index, 0, new Label(time));
		grid.setWidget(index, 1, new Label(location));
		grid.setWidget(index, 2, new Label(speed));
		grid.setWidget(index, 3, new Label(course));

	}

	/**
	 * remove all rows
	 * 
	 */
	private void clearGrid()
	{
		while (grid.getRowCount() > 1)
			grid.removeRow(1);
	}

	@Override
	public void debugStatesBounded(Collection<BoundedState> newStates)
	{
		statesBounded(newStates);
	}

	@Override
	public void incompatibleStatesIdentified(IncompatibleStateException e)
	{
		// TODO Auto-generated method stub

	}

	@Override
	public void statesBounded(Collection<BoundedState> newStates)
	{
		clearGrid();

		Iterator<BoundedState> iter = newStates.iterator();
		while (iter.hasNext())
		{
			BoundedState state = iter.next();
			@SuppressWarnings("deprecation")
			String dateStr = state.getTime().toGMTString();
			String locStr = "n/a";
			String speedStr = "n/a";
			String courseStr = "n/a";

			if (state.getLocation() != null)
				locStr = state.getLocation().getConstraintSummary();
			if (state.getSpeed() != null)
				speedStr = state.getSpeed().getConstraintSummary();
			if (state.getCourse() != null)
				courseStr = state.getCourse().getConstraintSummary();

			addStates(dateStr, locStr, speedStr, courseStr);
		}
	}

}

@@ -416,16 +416,89 @@
 Widget;%0A
+import com.planetmayo.debrief.satc.model.contributions.BaseContribution;%0A
 import c
@@ -1505,16 +1505,71 @@
 Row(1);%0A
+%09%09%0A%09%09// TODO: Akash, check the warning label is hidden%0A
 %09%7D%0A%0A%09@Ov
@@ -1731,16 +1731,47 @@
 ntified(
+BaseContribution contribution, 
 Incompat
@@ -1801,43 +1801,383 @@
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A
+String message1 = %22Incompatible States. Contribution: %22 + contribution.toString() ;%0A%09%09String message2 = %22Adding:%22 + e.getNewRange().getConstraintSummary() + %22 to %22 + e.getExistingRange().getConstraintSummary();%0A%09%09System.err.println(message1);%0A%09%09System.err.println(message2);%0A%09%09%0A%09%09// TODO: Akash, make the label visible, put the text into it.  It may need a multi-line message
 %0A%09%7D%0A

introduce some to-do comments