/**
 * 
 */
package org.gcube.portlets.user.td.client.ribbon;

import org.gcube.portlets.user.td.client.resource.TabularDataResources;

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.web.bindery.event.shared.EventBus;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonArrowAlign;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.cell.core.client.ButtonCell.IconAlign;
import com.sencha.gxt.core.client.dom.XElement;
import com.sencha.gxt.widget.core.client.button.ButtonGroup;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.event.SelectEvent.SelectHandler;
import com.sencha.gxt.widget.core.client.toolbar.ToolBar;

/**
 * @author "Federico De Faveri defaveri@isti.cnr.it"
 *
 */
public class ChartsToolBar {

	protected EventBus eventBus;
	protected ToolBar toolBar;
	
	protected TextButton pieButton;
	protected TextButton barButton;
	protected TextButton curveButton;
	protected TextButton bullsButton;
	protected TextButton statisticalButton;
	protected TextButton rstudioButton;
	
	protected TextButton gisButton;
	
	
	
	public ChartsToolBar(EventBus eventBus)
	{
		this.eventBus = eventBus;
		build();
	}
	
	public ToolBar getToolBar()
	{
		return toolBar;
	}

	protected void build()
	{
		toolBar = new ToolBar();
		toolBar.setSpacing(1);
		toolBar.setEnableOverflow(false);
		
		//Charts Group
		ButtonGroup chartsGroup = new ButtonGroup();
		chartsGroup.setId("Charts");
		chartsGroup.setStyleName("ribbon");
		chartsGroup.setHeadingText("Charts");
		chartsGroup.disable();
		toolBar.add(chartsGroup);

		FlexTable basicLayout = new FlexTable();
		chartsGroup.add(basicLayout);

		pieButton = new TextButton("Pie", TabularDataResources.INSTANCE.chartPie32());
		pieButton.setScale(ButtonScale.LARGE);
		pieButton.setIconAlign(IconAlign.TOP);
		pieButton.setToolTip("Pie");
		pieButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		pieButton.addSelectHandler(new SelectHandler() {
			
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		basicLayout.setWidget(0, 0, pieButton);
		basicLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
		

		barButton = new TextButton("Bar", TabularDataResources.INSTANCE.chartBar32());
		barButton.setScale(ButtonScale.LARGE);
		barButton.setIconAlign(IconAlign.TOP);
		barButton.setToolTip("Bar");
		barButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		barButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		basicLayout.setWidget(0, 1, barButton);
		basicLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);

		
		curveButton = new TextButton("Curve", TabularDataResources.INSTANCE.chartCurve32());
		curveButton.setScale(ButtonScale.LARGE);
		curveButton.setIconAlign(IconAlign.TOP);
		curveButton.setToolTip("Curve");
		curveButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		curveButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		basicLayout.setWidget(0, 2, curveButton);
		basicLayout.getFlexCellFormatter().setRowSpan(0, 2, 2);

		bullsButton = new TextButton("Bulls", TabularDataResources.INSTANCE.chartBulls32());
		bullsButton.setScale(ButtonScale.LARGE);
		bullsButton.setIconAlign(IconAlign.TOP);
		bullsButton.setToolTip("Bulls");
		bullsButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		bullsButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		basicLayout.setWidget(0, 3, bullsButton);
		basicLayout.getFlexCellFormatter().setRowSpan(0, 3, 2);
		
		
		cleanCells(basicLayout.getElement());
		
		
		//External Tools 
		ButtonGroup externalToolGroup = new ButtonGroup();
		externalToolGroup.setId("ExternalTools");
		externalToolGroup.setStyleName("ribbon");
		externalToolGroup.setHeadingText("External Tools");
		externalToolGroup.disable();
		toolBar.add(externalToolGroup);

		FlexTable externalToolLayout = new FlexTable();
		externalToolGroup.add(externalToolLayout);

		gisButton = new TextButton("GIS", TabularDataResources.INSTANCE.gis32());
		gisButton.setScale(ButtonScale.LARGE);
		gisButton.setIconAlign(IconAlign.TOP);
		gisButton.setToolTip("GIS");
		gisButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		gisButton.addSelectHandler(new SelectHandler() {
			
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		externalToolLayout.setWidget(0, 0, gisButton);
		externalToolLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
		
		
		
		rstudioButton = new TextButton("R Studio",
				TabularDataResources.INSTANCE.rstudio32());
		rstudioButton.setScale(ButtonScale.LARGE);
		rstudioButton.setIconAlign(IconAlign.TOP);
		rstudioButton.setToolTip("R Studio");
		rstudioButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		rstudioButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				// eventBus.fireEvent(new
				// ImportTableEvent(ImportTableType.JSON));
			}
		});

		externalToolLayout.setWidget(0, 1, rstudioButton);
		externalToolLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);

		statisticalButton = new TextButton("Statistical",
				TabularDataResources.INSTANCE.statistical32());
		statisticalButton.setScale(ButtonScale.LARGE);
		statisticalButton.setIconAlign(IconAlign.TOP);
		statisticalButton.setToolTip("Statistical");
		statisticalButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		statisticalButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				// eventBus.fireEvent(new
				// ImportTableEvent(ImportTableType.JSON));
			}
		});

		externalToolLayout.setWidget(0, 2, statisticalButton);
		externalToolLayout.getFlexCellFormatter().setRowSpan(0, 2, 2);

		cleanCells(externalToolLayout.getElement());

		
	}

	protected void cleanCells(Element elem) {
		NodeList<Element> tds = elem.<XElement> cast().select("td");
		for (int i = 0; i < tds.getLength(); i++) {
			Element td = tds.getItem(i);

			if (!td.hasChildNodes() && td.getClassName().equals("")) {
				td.removeFromParent();
			}
		}
	}

}
