package com.sensia.tools.client.swetools.editors.sensorml.renderer.advanced.panels.rng;

import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.sensia.relaxNG.RNGChoice;
import com.sensia.relaxNG.RNGTag;
import com.sensia.relaxNG.RNGTagVisitor;
import com.sensia.tools.client.swetools.editors.sensorml.panels.AbstractPanel;
import com.sensia.tools.client.swetools.editors.sensorml.panels.IPanel;
import com.sensia.tools.client.swetools.editors.sensorml.panels.IRefreshHandler;
import com.sensia.tools.client.swetools.editors.sensorml.panels.ViewerPanel;
import com.sensia.tools.client.swetools.editors.sensorml.utils.Utils;

public class RNGChoicePanel extends AbstractPanel<RNGChoice>{

	private ListBox choices;
	private Panel patternContainer;
	
	public RNGChoicePanel(final RNGChoice tag,final IRefreshHandler refreshHandler) {
		super(tag,refreshHandler);
		patternContainer = new VerticalPanel();
		patternContainer.addStyleName("rng-choice-pattern");
		
		HorizontalPanel selectHeader = new HorizontalPanel();
		HTML htmlLabel = new HTML(Utils.findLabel(tag) + ": ");
		htmlLabel.addStyleName("rng-optional-label");
		selectHeader.add(htmlLabel);
		choices = new ListBox();
		selectHeader.add(choices);
		container.add(selectHeader);
		container.add(patternContainer);
		
		choices.addItem("---");
		
		// add children name into the choice and
		List<RNGTag> children = tag.getItems();
		if(children != null){
			for(RNGTag child : children) {
				String label = Utils.findLabel(child);
				choices.addItem(label);
			}
		}
		
		if(getTag().getSelectedIndex() != -1) {
			choices.setSelectedIndex(getTag().getSelectedIndex()+1);
		}
		
		/*choices.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				//patternContainer.clear();
				if(choices.getSelectedIndex() == 0) {
					RNGChoicePanel.this.getTag().setSelectedIndex(-1);
				} else {
					RNGChoicePanel.this.getTag().setSelectedIndex(choices.getSelectedIndex()-1);
					//tag.getSelectedPattern().accept(visitor);
				}
				//ViewerPanel.getInstance(null).redraw();
				if(refreshHandler != null) {
					refreshHandler.refresh();
				}
			}
		});*/
		choices.addChangeHandler(new ChangeHandler() {
			
			@Override
			public void onChange(ChangeEvent event) {
				if(choices.getSelectedIndex() == 0) {
					RNGChoicePanel.this.getTag().setSelectedIndex(-1);
				} else {
					RNGChoicePanel.this.getTag().setSelectedIndex(choices.getSelectedIndex()-1);
					//tag.getSelectedPattern().accept(visitor);
				}
				//ViewerPanel.getInstance(null).redraw();
				if(refreshHandler != null) {
					refreshHandler.refresh();
				}
			}
		});
	}
	
	@Override
	public String getName() {
		if(getTag().getSelectedPattern() != null) {
			return getTag().getSelectedPattern().toString();	
		} else {
			return "RNGChoice";
		}
	}

	@Override
	protected void addInnerElement(IPanel<? extends RNGTag> element) {
		patternContainer.add(element.getPanel());
	}

	@Override
	protected AbstractPanel<RNGChoice> newInstance() {
		// TODO Auto-generated method stub
		return null;
	}

}

@@ -1489,54 +1489,152 @@
 ;%0A%09%09
-HTML htmlLabel = new HTML(Utils.findLabel(tag)
+//TODO: check that we take the parent tag for the choice box name%0A%09%09String strLabel = Utils.findLabel(tag);%0A%09%09HTML htmlLabel = new HTML(strLabel
  + %22
@@ -1745,16 +1745,124 @@
 tBox();%0A
+%09%09%0A%09%09//TODO: hide the label for now%0A%09%09htmlLabel.setVisible(strLabel != null && !strLabel.trim().isEmpty());%0A
 %09%09select

Remove choice listbox name when null or empty