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

import org.gcube.portlets.user.td.client.resource.TabularDataResources;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.RibbonEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.event.UIStateEvent;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.RibbonType;
import org.gcube.portlets.user.td.widgetcommonevent.client.type.UIStateType;

import com.allen_sauer.gwt.log.client.Log;
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 FileToolBar {

	protected EventBus eventBus;
	protected ToolBar toolBar;

	protected TextButton openButton;
	protected TextButton deleteButton;
	protected TextButton closeButton;
	protected TextButton propertiesButton;

	protected TextButton importCSVButton;
	protected TextButton importJSONButton;
	protected TextButton importSDMXButton;

	protected TextButton exportSDMXButton;
	protected TextButton exportCSVButton;
	protected TextButton exportJSONButton;

	protected TextButton templateButton;
	
	protected TextButton taskButton;

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

	public ToolBar getToolBar() {
		return toolBar;
	}

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

		// File
		ButtonGroup fileGroup = new ButtonGroup();
		fileGroup.setId("File");
		fileGroup.setStyleName("ribbon");
		fileGroup.setHeadingText("File");
		toolBar.add(fileGroup);

		FlexTable dataLayout = new FlexTable();
		dataLayout.setStyleName("ribbon");
		fileGroup.add(dataLayout);

		openButton = new TextButton("Open",
				TabularDataResources.INSTANCE.open32());
		openButton.setId("openButton");
		openButton.setScale(ButtonScale.LARGE);
		openButton.setIconAlign(IconAlign.TOP);
		openButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		openButton.setToolTip("Open Tabular Resource");

		openButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.OPEN));
			}
		});

		dataLayout.setWidget(0, 0, openButton);
		dataLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);

		closeButton = new TextButton("Close",
				TabularDataResources.INSTANCE.close32());
		closeButton.disable();
		closeButton.setId("closeButton");
		closeButton.setScale(ButtonScale.LARGE);
		closeButton.setIconAlign(IconAlign.TOP);
		closeButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		closeButton.setToolTip("Close Tabular Resource");

		closeButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.CLOSE));
			}
		});

		dataLayout.setWidget(0, 1, closeButton);
		dataLayout.getFlexCellFormatter().setRowSpan(0, 1, 2);

		deleteButton = new TextButton("Delete",
				TabularDataResources.INSTANCE.delete());
		deleteButton.disable();
		deleteButton.setId("closeButton");
		deleteButton.setToolTip("Delete Tabular Resource");

		dataLayout.setWidget(0, 2, deleteButton);
		deleteButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.DELETE));
			}
		});

		propertiesButton = new TextButton("Properties",
				TabularDataResources.INSTANCE.properties());
		propertiesButton.disable();
		propertiesButton.setId("propertiesButton");
		propertiesButton.setToolTip("Show properties");

		dataLayout.setWidget(1, 2, propertiesButton);
		propertiesButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.PROPERTIES));
			}
		});

		cleanCells(dataLayout.getElement());

		// Import
		ButtonGroup importGroup = new ButtonGroup();
		importGroup.setId("Import");
		importGroup.setStyleName("ribbon");
		importGroup.setHeadingText("Import");
		toolBar.add(importGroup);

		FlexTable importLayout = new FlexTable();
		importGroup.add(importLayout);

		importSDMXButton = new TextButton("SDMX",
				TabularDataResources.INSTANCE.sdmx32());
		importSDMXButton.setScale(ButtonScale.LARGE);
		importSDMXButton.setIconAlign(IconAlign.TOP);
		importSDMXButton.setToolTip("Import table from SDMX source");
		importSDMXButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		importSDMXButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.IMPORTSDMX));
			}
		});

		importLayout.setWidget(0, 0, importSDMXButton);
		importLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);

		importCSVButton = new TextButton("CSV",
				TabularDataResources.INSTANCE.csv());
		importCSVButton.setToolTip("Import table from CSV source");
		importCSVButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.IMPORTCSV));
			}
		});
		importLayout.setWidget(0, 2, importCSVButton);

		importJSONButton = new TextButton("JSON",
				TabularDataResources.INSTANCE.json());
		importJSONButton.disable();
		importJSONButton.setToolTip("Import table from JSON source");
		importJSONButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.IMPORTJSON));
			}
		});
		importLayout.setWidget(1, 2, importJSONButton);

		cleanCells(importLayout.getElement());

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

		FlexTable exportLayout = new FlexTable();
		exportGroup.add(exportLayout);

		exportSDMXButton = new TextButton("SDMX",
				TabularDataResources.INSTANCE.sdmx32());
		exportSDMXButton.disable();
		exportSDMXButton.setToolTip("Export SDMX document");
		exportSDMXButton.setScale(ButtonScale.LARGE);
		exportSDMXButton.setIconAlign(IconAlign.TOP);
		exportSDMXButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		exportSDMXButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.EXPORTSDMX));
			}
		});

		exportLayout.setWidget(0, 0, exportSDMXButton);
		exportLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);

		exportCSVButton = new TextButton("CSV",
				TabularDataResources.INSTANCE.csv());
		exportCSVButton.disable();
		exportCSVButton.setToolTip("Export CSV document");
		exportCSVButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.EXPORTCSV));
			}
		});
		exportLayout.setWidget(0, 2, exportCSVButton);

		exportJSONButton = new TextButton("JSON",
				TabularDataResources.INSTANCE.json());
		exportJSONButton.disable();
		exportJSONButton.setToolTip("Export JSON document");
		exportJSONButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(RibbonType.EXPORTJSON));
			}
		});
		exportLayout.setWidget(1, 2, exportJSONButton);
		cleanCells(exportLayout.getElement());

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

		FlexTable templateLayout = new FlexTable();
		templateGroup.add(templateLayout);

		templateButton = new TextButton("Template",
				TabularDataResources.INSTANCE.template32());
		templateButton.disable();
		templateButton.setToolTip("Template");
		templateButton.setScale(ButtonScale.LARGE);
		templateButton.setIconAlign(IconAlign.TOP);
		templateButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		templateButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				// eventBus.fireEvent(new RibbonEvent(
				// RibbonType.EXPORTSDMX));
			}
		});

		templateLayout.setWidget(0, 0, templateButton);
		templateLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
		cleanCells(templateLayout.getElement());

		// Task
		ButtonGroup taskGroup = new ButtonGroup();
		taskGroup.setId("Template");
		taskGroup.setStyleName("ribbon");
		taskGroup.setHeadingText("Task");
		toolBar.add(taskGroup);

		FlexTable taskLayout = new FlexTable();
		taskGroup.add(taskLayout);

		taskButton = new TextButton("Task",
				TabularDataResources.INSTANCE.template32());
		taskButton.disable();
		taskButton.setToolTip("Task");
		taskButton.setScale(ButtonScale.LARGE);
		taskButton.setIconAlign(IconAlign.TOP);
		taskButton.setArrowAlign(ButtonArrowAlign.BOTTOM);
		taskButton.addSelectHandler(new SelectHandler() {

			public void onSelect(SelectEvent event) {
				eventBus.fireEvent(new RibbonEvent(
				RibbonType.TASKSMANAGER));
			}
		});

		taskLayout.setWidget(0, 0, taskButton);
		taskLayout.getFlexCellFormatter().setRowSpan(0, 0, 2);
		cleanCells(taskLayout.getElement());

		eventBus.addHandler(UIStateEvent.TYPE,
				new UIStateEvent.UIStateHandler() {

					public void onUIState(UIStateEvent event) {
						setUI(event);

					}
				});

	}

	private 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();
			}
		}
	}

	public void setUI(UIStateEvent event) {
		UIStateType uiStateType = event.getUIStateType();
		try {
			switch (uiStateType) {
			case START:
				openButton.enable();
				closeButton.disable();
				deleteButton.disable();
				propertiesButton.disable();
				importCSVButton.enable();
				importJSONButton.disable();
				importSDMXButton.enable();
				exportCSVButton.disable();
				exportJSONButton.disable();
				exportSDMXButton.disable();
				templateButton.disable();
				taskButton.disable();
				break;
			case TR_CLOSE:
				openButton.enable();
				closeButton.disable();
				deleteButton.disable();
				propertiesButton.disable();
				importCSVButton.enable();
				importJSONButton.disable();
				importSDMXButton.enable();
				exportCSVButton.disable();
				exportJSONButton.disable();
				exportSDMXButton.disable();
				templateButton.disable();
				taskButton.disable();
				break;
			case TR_OPEN:
				openButton.enable();
				closeButton.enable();
				deleteButton.enable();
				propertiesButton.enable();
				importCSVButton.enable();
				importJSONButton.disable();
				importSDMXButton.enable();

				if (event.getTrId().getTableType().compareTo("Codelist") == 0) {
					exportSDMXButton.enable();
				} else {
					exportSDMXButton.disable();
				}
				exportCSVButton.enable();
				exportJSONButton.disable();
				templateButton.disable();
				taskButton.enable();
				break;
			case WIZARD_OPEN:
				openButton.disable();
				closeButton.disable();
				deleteButton.disable();
				propertiesButton.disable();
				importCSVButton.disable();
				importJSONButton.disable();
				importSDMXButton.disable();
				exportCSVButton.disable();
				exportJSONButton.disable();
				exportSDMXButton.disable();
				templateButton.disable();
				taskButton.disable();
				break;
			default:
				break;
			}
		} catch (Exception e) {
			Log.error("setUI Error : " + e.getLocalizedMessage());
		}
	}
}
