package org.gcube.portlets.user.reportgenerator.client.dialog;

import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import org.gcube.portlets.d4sreporting.common.shared.BasicComponent;
import org.gcube.portlets.d4sreporting.common.shared.Model;
import org.gcube.portlets.d4sreporting.common.shared.BasicSection;
import org.gcube.portlets.user.reportgenerator.client.events.ItemSelectionEvent;
import org.gcube.portlets.user.reportgenerator.client.resources.FimesReportTreeStructureResources;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;


public class ReportStructureDialog extends ScrollPanel{

	public ReportStructureDialog(final HandlerManager eventBus, final Model report, 
			final String height, final String width) {
		
		this.setWidth(width);
		this.setHeight(height);
		
		Tree t = new Tree();
		t.setAnimationEnabled(true);
		t.addItem(loadReportRootTree(report));
		t.addSelectionHandler(new SelectionHandler<TreeItem>() {
			
			@SuppressWarnings("unchecked")
			public void onSelection(SelectionEvent<TreeItem> event) {
				// TODO Auto-generated method stub
				TreeItem item = event.getSelectedItem();
				HashMap<String,Object> map = (HashMap<String, Object>)item.getUserObject();
				eventBus.fireEvent(new ItemSelectionEvent(map));
			}
		});
		
		this.add(t);
	}
	
	private TreeItem loadReportRootTree(final Model report) {
		// Add root node
		ImageResource image = FimesReportTreeStructureResources.INSTANCE.root();
		HorizontalPanel node = createNodeWidget(image, report.getTemplateName(),"gwt-label-rootTree");
		TreeItem root = new TreeItem(node);
		
		Vector<BasicSection> sections = report.getSections();
		for(int i = 1; i <= sections.size(); i++) {
			BasicSection s = sections.get(i - 1);
			// Add section item
			image = FimesReportTreeStructureResources.INSTANCE.section();
			node = createNodeWidget(image, "Section" + i,"gwt-label-sectionTree");
			
			TreeItem sectionItem = addChildItemToParentItem(root, "Section", Integer.toString(i -1), node);
			addItemsComponent(sectionItem, s);
			
		}
		
		return root;
	}
	
	private void addItemsComponent(final TreeItem sectionItem,final BasicSection sectionModel) {
		
		List<BasicComponent> components = sectionModel.getComponents();
		for(int i = 0; i < components.size(); i++) {
			BasicComponent c = components.get(i);
			
			HorizontalPanel node = null;
			switch (c.getType()) {
			case TITLE: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.heading1();
				node = createNodeWidget(image, (String)c.getPossibleContent(),"gwt-label-componentTree");
				break;
			}
			case HEADING_1: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.heading1();
				node = createNodeWidget(image, (String)c.getPossibleContent(),"gwt-label-componentTree");
				break;
			}
			case HEADING_2: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.heading2();
				node = createNodeWidget(image, (String)c.getPossibleContent(),"gwt-label-componentTree");
				break;
			}
			case HEADING_3: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.heading3();
				node = createNodeWidget(image, (String)c.getPossibleContent(),"gwt-label-componentTree");
				break;
			}
			case HEADING_4: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.heading4();
				node = createNodeWidget(image, (String)c.getPossibleContent(),"gwt-label-componentTree");
				break;
			}
			case INSTRUCTION: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.instructions();
				node = createNodeWidget(image, "Instruction","gwt-label-componentTree");
				break;
			}
			case COMMENT: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.comments();
				node = createNodeWidget(image, "Comment","gwt-label-componentTree");
				break;
			}
			case BODY: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.text();
				node = createNodeWidget(image, "Text","gwt-label-componentTree");
				break;
			}
			case DYNA_IMAGE: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.image();
				node = createNodeWidget(image, "Image","gwt-label-componentTree");
				break;
			}
			case FLEX_TABLE: {
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.table();
				node = createNodeWidget(image, "Table","gwt-label-componentTree");
				break;
			}
			case ATTRIBUTE_MULTI: {
				// TODO
				ImageResource image = FimesReportTreeStructureResources.INSTANCE.comments();
				node = createNodeWidget(image, "Attribute","gwt-label-componentTree");
				break;
			}
			case TOC:
				// TODO
				// componentItem = new TreeItem("TOC");
				break;
			case PAGEBREAK:
				// TODO
				// componentItem = new TreeItem("Pagebreak");
				break;
			case TIME_SERIES:
				// TODO
				//componentItem = new TreeItem("Time_Series");
				break;
			}
			
			if (node != null)
				addChildItemToParentItem(sectionItem, "Component",
						Integer.toString(i), node);
			
		}
	}
	
	private TreeItem addChildItemToParentItem(final TreeItem root, final String typeItem, 
			final String indexList, final Widget content) {
		
		TreeItem item = new TreeItem(content);
		// Create userObjet 
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("item",typeItem );
		map.put("index", indexList);
		map.put("parent", root.getUserObject());
		item.setUserObject(map);
		root.addItem(item);
		return item;
	}
	
	/**
	 * Generates HTML for a tree item with an attached icon.
	 * @param imageResource the image resource to use
	 * @param title the title of the item
	 * @return the resultant HTML
	 */
	private HorizontalPanel createNodeWidget(ImageResource imageResource, String title,
			String cssStyle) {
		HorizontalPanel node = new HorizontalPanel();
		
		node.setSpacing(0);
		Image image = new Image(imageResource);
		image.setPixelSize(image.getWidth() + 5, image.getHeight());
		node.add(image);
		
		String shortTitle = title;
		if (shortTitle.length() > 25)
			shortTitle = shortTitle.substring(0,25) + "...";
		
		Label text = new Label(shortTitle);
		text.setStyleName(cssStyle);
		node.add(text);
		return node;
	}
}
