package org.gcube.portlets.user.reportgenerator.client.targets;

import java.util.ArrayList;

import org.gcube.portlets.d4sreporting.common.shared.Tuple;
import org.gcube.portlets.d4sreporting.common.shared.RepeatableSequence;
import org.gcube.portlets.user.reportgenerator.client.Presenter.Presenter;
import org.gcube.portlets.user.reportgenerator.client.model.TemplateComponent;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
/**
 * 
 * @author massi
 *
 */
public class ClientTuple extends Composite implements ClientSequence {

	protected ArrayList<TemplateComponent> groupedComponents = new ArrayList<TemplateComponent>();
	private VerticalPanel myPanel = new VerticalPanel();

	private  ArrayList<Tuple> tupleList;
	private SequenceWidget first;


	/**
	 * s
	 * @param p
	 * @param ms
	 */
	public ClientTuple(Presenter p, ArrayList<Tuple> tupleList) {
		this.tupleList = tupleList;
		for (Tuple seq : tupleList) {	
			if (myPanel.getWidgetCount()==0) { // if is the first one
				first = new SequenceWidget(p, this, seq, true, false);
				if (tupleList.size() == 1) {
					first.enableClear();		
				}
				myPanel.add(first);
			} else {
				SequenceWidget seqW = new SequenceWidget(p, this, seq, false, false);
				myPanel.add(seqW);
			}
			

			
		}

		initWidget(myPanel);
	}


	public ArrayList<Tuple> getTupleList() {
		return tupleList;
	}


	public void setTupleList(ArrayList<Tuple> tupleList) {
		this.tupleList = tupleList;
	}


	@Override
	public ArrayList<TemplateComponent> getGroupedComponents() {
		return groupedComponents;
	}

	@Override
	public boolean add(RepeatableSequence sequence) {
		return false;
	}


	@Override
	public SequenceWidget remove(SequenceWidget toRemove) {
		myPanel.remove(toRemove);
		for (TemplateComponent tc : toRemove.getSeqGroupedComponents()) {
			groupedComponents.remove(tc);
		}
		tupleList.remove(toRemove.getSequence());
		if (tupleList.size() == 1)
			first.enableClear();	
		return toRemove;
	}

}
