package org.gcube.portlets.user.workspace.client.view.toolbars;

import java.util.ArrayList;
import java.util.LinkedHashMap;

import org.gcube.portlets.user.workspace.client.AppController;
import org.gcube.portlets.user.workspace.client.event.PathElementSelectedEvent;
import org.gcube.portlets.user.workspace.client.model.FileModel;
import org.gcube.portlets.user.workspace.client.resources.Resources;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.WidgetComponent;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.google.gwt.user.client.ui.Image;

/**
 * @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
 *
 */
public class GxtPathPanel {

	private static final String ROOT_NAME = "Workspace";

	private HorizontalPanel toolBar = new HorizontalPanel();
	private Text txtPath = new Text("PATH ");
	private LinkedHashMap<String, FileModel> hashFileModel; // Ordered-HashMap
	boolean rootAdded = false;
	WidgetComponent hardDiskIcon = new WidgetComponent(new Image (Resources.getImageHardDisk()));

	public GxtPathPanel() {
		toolBar = new HorizontalPanel();
		toolBar.setHeight("25px");
		initToolbar();
	}

	public HorizontalPanel getToolBarPathPanel() {
		return toolBar;
	}

	public void setPath(ArrayList<FileModel> parents) {

		initToolbarWithoutFakeRoot();

		if (parents != null && parents.size() > 0) {

			ArrayList<Button> listButtons = new ArrayList<Button>();

			hashFileModel = new LinkedHashMap<String, FileModel>();

			// arrayFileModel = new ArrayList<FileModel>();

			for (FileModel parent : parents) {

				Button butt = new Button(parent.getName());
				butt.setId(parent.getIdentifier());
				butt.setStyleAttribute("top", "-4px");
				butt.setStyleName("button-hyperlink");
				// butt.setHeight(12);

				hashFileModel.put(parent.getIdentifier(), parent);

				// arrayFileModel.set(parent.getIdentifier(),parent);

				butt.addListener(Events.OnClick, new Listener<ButtonEvent>() {

					@Override
					public void handleEvent(ButtonEvent be) {

						// Window.alert("Name " + be.getButton().getText() +
						// " ID: "+ be.getButton().getId());

						FileModel target = hashFileModel.get(be.getButton()
								.getId());

						AppController.getEventBus().fireEvent(
								new PathElementSelectedEvent(target));
					}

				});				
				listButtons.add(butt);				
			}

			int size = listButtons.size();
			for (int i = 0; i < size - 1; i++) {
				toolBar.add(listButtons.get(i));
				toolBar.add(new WidgetComponent(new Image(Resources.getImagePathSeparator())));
				
			}

			toolBar.add(listButtons.get(size - 1)); // Add last element
			toolBar.layout(true);
		}
	}

	private void initToolbar() {
		toolBar.removeAll();
		toolBar.setStyleName("myToolbar");
		toolBar.setStyleAttribute("padding-top", "5px");
		txtPath.setStyleAttribute("padding-right", "10px");
		toolBar.add(new WidgetComponent(new Image(Resources.getImagePathSeparator())));
		toolBar.layout(true);

		Button butt = new Button(ROOT_NAME);
		butt.setId("");
		butt.setStyleAttribute("top", "-4px");
		butt.setStyleName("button-hyperlink");
		toolBar.add(butt);
	}
	
	private void initToolbarWithoutFakeRoot() {
		toolBar.removeAll();
		toolBar.setStyleName("myToolbar");
		toolBar.setStyleAttribute("padding-top", "5px");
		txtPath.setStyleAttribute("padding-right", "10px");
		toolBar.add(new WidgetComponent(new Image(Resources.getImagePathSeparator())));
		toolBar.layout(true);
	}



}
