package eu.dnetlib.enabling.ui.common.widgets;

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

import com.google.gwt.user.client.ui.CaptionPanel;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;




public class FieldSetPanel extends CaptionPanel implements MyFormComponent<List<MyFormComponent<?>>> {
	protected Panel innerPanel;
	protected List<MyFormComponent<?>> values;
	
	private String formName;
	private int position;

	public FieldSetPanel(String title, int position, Panel innerPanel, Widget... list) {
		super(title);
		this.formName = title;
		this.position = position;
		this.innerPanel = innerPanel;
		this.values = new ArrayList<MyFormComponent<?>>();
		add(innerPanel);
		populate(list);
	}

	private void populate(Widget... list) {
		innerPanel.clear();
		values.clear();
		if (list != null) {
			for (int i=0; i<list.length; i++) {
				Widget w = list[i];
				
				if (w instanceof WithLabel) {
					addLabel(((WithLabel) w).getLabel(), i);
				}
				if (w instanceof HasValue<?>) {
					values.add((MyFormComponent<?>) w);
				}
				addWidget(w, i);
			}
		}
	}

	protected void addLabel(Label label, int count) {
		innerPanel.add(label);
	}

	protected void addWidget(Widget w, int count) {
		innerPanel.add(w);
	}

	@Override
	public String getFieldName() {
		return formName;
	}

	@Override
	public List<MyFormComponent<?>> getFieldValue() {
		return values;
	}

	@Override
	public int getPosition() {
		return position;
	}


}
