/**
 * 
 */
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 FormulasToolBar {

	protected EventBus eventBus;
	protected ToolBar toolBar;
	
	//Table
	protected TextButton filterButton;
	protected TextButton unionButton;
	protected TextButton denormalizeButton;
	protected TextButton groupButton;
	protected TextButton aggregateButton;
	
	//Column
	protected TextButton modifyLabelButton;
	protected TextButton modifyColumnTypeButton;
	protected TextButton modifyColumnValuesButton;
	
	
	
	//History
	protected TextButton historyButton;
	protected TextButton discardButton;
	protected TextButton discardAllButton;
	
	public FormulasToolBar(EventBus eventBus)
	{
		this.eventBus = eventBus;
		build();
	}
	
	public ToolBar getToolBar()
	{
		return toolBar;
	}

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

		FlexTable tableLayout = new FlexTable();
		tableGroup.add(tableLayout);

		filterButton = new TextButton("Filter", TabularDataResources.INSTANCE.filter32());
		filterButton.setScale(ButtonScale.LARGE);
		filterButton.setIconAlign(IconAlign.TOP);
		filterButton.setToolTip("Filter table data");
		filterButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		filterButton.addSelectHandler(new SelectHandler() {
			

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

		tableLayout.setWidget(0, 0, filterButton);
		tableLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
		
		unionButton = new TextButton("Union", TabularDataResources.INSTANCE.union32());
		unionButton.setScale(ButtonScale.LARGE);
		unionButton.setIconAlign(IconAlign.TOP);
		unionButton.setToolTip("Merge tables data");
		unionButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		unionButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		tableLayout.setWidget(0, 1, unionButton);
		tableLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);
		
		 
		denormalizeButton = new TextButton("Denormalize", TabularDataResources.INSTANCE.denormalize32());
		denormalizeButton.setScale(ButtonScale.LARGE);
		denormalizeButton.setIconAlign(IconAlign.TOP);
		denormalizeButton.setToolTip("Denormalize table");
		denormalizeButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		denormalizeButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		tableLayout.setWidget(0, 2, denormalizeButton);
		tableLayout.getFlexCellFormatter().setRowSpan(0, 2, 2);
	

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

		tableLayout.setWidget(0, 3, aggregateButton);
		tableLayout.getFlexCellFormatter().setRowSpan(0, 3, 2);
		cleanCells(tableLayout.getElement());
		
		
		//Column Group
		ButtonGroup columnGroup = new ButtonGroup();
		columnGroup.setId("Column");
		columnGroup.setStyleName("ribbon");
		columnGroup.setHeadingText("Column");
		columnGroup.disable();
		toolBar.add(columnGroup);

		FlexTable columnLayout = new FlexTable();
		columnGroup.add(columnLayout);

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

		columnLayout.setWidget(0, 0, modifyLabelButton);
		columnLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
		
		
		modifyColumnTypeButton = new TextButton("Type", TabularDataResources.INSTANCE.columnType32());
		modifyColumnTypeButton.setScale(ButtonScale.LARGE);
		modifyColumnTypeButton.setIconAlign(IconAlign.TOP);
		modifyColumnTypeButton.setToolTip("Change the column type");
		modifyColumnTypeButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		modifyColumnTypeButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		columnLayout.setWidget(0, 1, modifyColumnTypeButton);
		columnLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);
		
		modifyColumnValuesButton = new TextButton("Values", TabularDataResources.INSTANCE.columnValues32());
		modifyColumnValuesButton.setScale(ButtonScale.LARGE);
		modifyColumnValuesButton.setIconAlign(IconAlign.TOP);
		modifyColumnValuesButton.setToolTip("Modify column values according to an expression");
		modifyColumnValuesButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		modifyColumnValuesButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});

		columnLayout.setWidget(0, 2, modifyColumnValuesButton);
		columnLayout.getFlexCellFormatter().setRowSpan(0, 2, 2);
		
		cleanCells(columnLayout.getElement());
	
		
		//History Group
		ButtonGroup historyGroup = new ButtonGroup();
		historyGroup.setId("History");
		historyGroup.setStyleName("ribbon");
		historyGroup.setHeadingText("History");
		historyGroup.disable();
		toolBar.add(historyGroup);

		FlexTable historyLayout = new FlexTable();
		historyGroup.add(historyLayout);

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

		historyLayout.setWidget(0, 0, historyButton);
		historyLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
	

		discardButton = new TextButton("Discard", TabularDataResources.INSTANCE.discard());
		discardButton.setToolTip("Discard the last operation");
		historyLayout.setWidget(0, 1, discardButton);
		discardButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.CSV));
			}
		});

		discardAllButton = new TextButton("Discard All", TabularDataResources.INSTANCE.discardAll());
		discardAllButton.setToolTip("Discard all unsaved operations");
		historyLayout.setWidget(1, 1, discardAllButton);
		discardAllButton.addSelectHandler(new SelectHandler() {
			
			public void onSelect(SelectEvent event) {
				//eventBus.fireEvent(new ImportTableEvent(ImportTableType.JSON));
			}
		});
		
		cleanCells(historyLayout.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();
			}
		}
	}

}
