/**
 * 
 */
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 "Giancarlo Panichi" 
 * <a href="mailto:g.panichi@isti.cnr.it">g.panichi@isti.cnr.it</a> 
 *
 */
public class ReviewToolBar {

	protected EventBus eventBus;
	protected ToolBar toolBar;

	protected TextButton btn;
	protected TextButton validationButton;
	protected TextButton rulesButton;
	protected TextButton operationsButton;
	protected TextButton manageRulesButton;

	public ReviewToolBar(EventBus eventBus) {
		this.eventBus = eventBus;
		build();
	}

	public ToolBar getToolBar() {
		return toolBar;
	}

	protected void build() {
		toolBar = new ToolBar();
		toolBar.setSpacing(1);
		toolBar.setEnableOverflow(false);

		// Validation
		ButtonGroup validationGroup = new ButtonGroup();
		validationGroup.setId("Validation");
		validationGroup.setStyleName("ribbon");
		validationGroup.setHeadingText("Validation");
		validationGroup.disable();
		toolBar.add(validationGroup);

		FlexTable validationLayout = new FlexTable();
		validationGroup.add(validationLayout);

		rulesButton = new TextButton("New Rule",
				TabularDataResources.INSTANCE.ruleAdd32());
		rulesButton.setScale(ButtonScale.LARGE);
		rulesButton.setIconAlign(IconAlign.TOP);
		rulesButton.setToolTip("Define a new validation rule");
		rulesButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		rulesButton.addSelectHandler(new SelectHandler() {

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

		validationLayout.setWidget(0, 1, rulesButton);
		validationLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);
		
		manageRulesButton = new TextButton("Manage Rules",
				TabularDataResources.INSTANCE.rules32());
		manageRulesButton.setScale(ButtonScale.LARGE);
		manageRulesButton.setIconAlign(IconAlign.TOP);
		manageRulesButton.setToolTip("View defined rules and apply");
		manageRulesButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		manageRulesButton.addSelectHandler(new SelectHandler() {

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

		validationLayout.setWidget(0, 2, manageRulesButton);
		validationLayout.getFlexCellFormatter().setRowSpan(0, 2, 2);
		

		validationButton = new TextButton("Show Validation",
				TabularDataResources.INSTANCE.validation32());
		validationButton.setScale(ButtonScale.LARGE);
		validationButton.setIconAlign(IconAlign.TOP);
		validationButton.setToolTip("Show validation outcome on table");
		validationButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		validationButton.addSelectHandler(new SelectHandler() {

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

		validationLayout.setWidget(0, 3, validationButton);
		validationLayout.getFlexCellFormatter().setRowSpan(0, 3, 2);

		cleanCells(validationLayout.getElement());

		// Operations
		ButtonGroup operationsGroup = new ButtonGroup();
		operationsGroup.setId("Operations");
		operationsGroup.setStyleName("ribbon");
		operationsGroup.setHeadingText("Operations");
		operationsGroup.disable();
		toolBar.add(operationsGroup);

		FlexTable operationsLayout = new FlexTable();
		operationsGroup.add(operationsLayout);

		operationsButton = new TextButton("Review Operations",
				TabularDataResources.INSTANCE.cog32());
		operationsButton.setScale(ButtonScale.LARGE);
		operationsButton.setIconAlign(IconAlign.TOP);
		operationsButton.setToolTip("View performed operations");
		operationsButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		operationsButton.addSelectHandler(new SelectHandler() {

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

		operationsLayout.setWidget(0, 1, operationsButton);
		operationsLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);

		

		cleanCells(operationsLayout.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();
			}
		}
	}

}
