/*******************************************************************************
 * Copyright (c) 2015, 2016 CNRS, IRIT, IRT AESE (IRT Saint Exupéry).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the LGPL 3.0
 * which accompanies this distribution, and is available at
 * https://www.gnu.org/licenses/lgpl-3.0-standalone.html
 *
 * Contributors:
 *     Faiez Zalila - initial API and implementation and/or initial documentation
 *     Jorge Enrique Gutierrez Zuluaga
 *******************************************************************************/
package com.irtsaintexupery.fiacre.simulator.perspective.views;

import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.part.*;

import com.irtsaintexupery.fiacre.simulator.perspective.model.TreeObject;
import com.irtsaintexupery.fiacre.simulator.perspective.model.TreeParent;
import com.irtsaintexupery.fiacre.simulator.perspective.ViewsController;

import org.eclipse.jface.viewers.*;
import org.eclipse.swt.graphics.Image;

import java.util.ArrayList;

import org.eclipse.jface.action.*;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.*;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.SWT;

public class InstancesView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "com.irtsaintexupery.fiacre.simulator"
			+ ".perspective.instancesView";

	private TreeViewer viewer;
	private DrillDownAdapter drillDownAdapter;
	private Action doubleClickAction;
	private Action oneClickAction;
	private TreeObject instanceTree;
	 
	

	class ViewContentProvider implements ITreeContentProvider {
		@Override
		public void dispose() {
			// TODO Auto-generated method stub

		}
		@Override
		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			// TODO Auto-generated method stub

		}		

		private TreeParent invisibleRoot;

		public Object[] getElements(Object parent) {
			if (parent.equals(getViewSite())) {
				if (invisibleRoot==null) initialize();
				return getChildren(invisibleRoot);
			}
			return getChildren(parent);
		}
		public Object getParent(Object child) {
			if (child instanceof TreeObject) {
				return ((TreeObject)child).getParent();
			}
			return null;
		}
		public Object [] getChildren(Object parent) {
			if (parent instanceof TreeParent) {
				return ((TreeParent)parent).getChildren();
			}
			return new Object[0];
		}
		public boolean hasChildren(Object parent) {
			if (parent instanceof TreeParent)
				return ((TreeParent)parent).hasChildren();
			return false;
		}
		
		public void initialize() {
			invisibleRoot = (TreeParent) instanceTree;
		}
		
		
	}

	class ViewLabelProvider extends ColumnLabelProvider {
		@Override
		public String getText(Object obj) {
			return obj.toString();
			
		}
		@Override
		public Image getImage(Object obj) {
			String imageKey = ISharedImages.IMG_OBJ_ELEMENT;
			if (obj instanceof TreeParent)
				if (((TreeParent) obj).getName().equals("")) {
					imageKey = ISharedImages.IMG_TOOL_UP_DISABLED;
				}else {
					imageKey = ISharedImages.IMG_TOOL_UP;					
				}
			return PlatformUI.getWorkbench().getSharedImages().getImage(imageKey);  
		}
		
		@Override
		public String getToolTipText(Object obj) {
			return ViewsController.INSTANCE.returnFlatName((TreeObject) obj);
		}
		
		@Override
		  public int getToolTipDisplayDelayTime(Object object) { 
		    return 0;
		  }

		  @Override
		  public int getToolTipTimeDisplayed(Object object) {
		    return 5000;
		  }
		
		
	}

/**
	 * The constructor.
	 */
	public InstancesView() {
	}

	/**
	 * This is a callback that will allow us
	 * to create the viewer and initialize it.
	 */
	public void createPartControl(Composite parent) {
		
		//create the content
		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		drillDownAdapter = new DrillDownAdapter(viewer);
		
	viewer.setContentProvider(new ViewContentProvider());
	
	ColumnViewerToolTipSupport.enableFor(viewer);
	
	viewer.setInput(getViewSite());
	viewer.setLabelProvider(new ViewLabelProvider());
	
	viewer.expandAll();

		// Create the help context id for the viewer's control
		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), 
				"viewDemo2.viewer");
		getSite().setSelectionProvider(viewer);
		makeActions();
		hookContextMenu();
		hookDoubleClickAction();
		hookOneClickAction();
		contributeToActionBars();
	}

	private void hookContextMenu() {
		MenuManager menuMgr = new MenuManager("#PopupMenu");
		menuMgr.setRemoveAllWhenShown(true);
		menuMgr.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
			}
		});
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
		viewer.getControl().setMenu(menu);
		getSite().registerContextMenu(menuMgr, viewer);
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
	}

	private void makeActions() {
		
		doubleClickAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				//showMessage("Double-click detected on "+obj.toString());
				if (!selection.isEmpty()) {
					ViewsController.INSTANCE.showInstanceSelected((TreeObject)obj);
				}				
			}
		};
		
		oneClickAction = new Action() {
			public void run() {
				ISelection selection = viewer.getSelection();
				Object obj = ((IStructuredSelection)selection).getFirstElement();
				if (selection != null & selection instanceof IStructuredSelection 
						&& obj != null) {
					ViewsController.INSTANCE.showValuesView((TreeObject)obj);
				}
			}
		};
	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});
	}
	
	private void hookOneClickAction() {
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
			
			@Override
			public void selectionChanged(SelectionChangedEvent event) {
				oneClickAction.run();
			}
		});
	}
	
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Instances View",
			message);
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
		
	public void update(TreeObject rood) {
		instanceTree = rood;
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setInput(getViewSite());		
		viewer.setLabelProvider(new ViewLabelProvider());
		
		viewer.expandAll();
	}
	
	public void selectInstanceObject(TreeObject obj) {
		viewer.setSelection(new StructuredSelection(obj), true);
	}

	public void selectInstanceObject(ArrayList<TreeObject> instanceList) {
		
		TreeObject[] array = new TreeObject[instanceList.size()];
		for (int i = 0; i < instanceList.size(); i++) {
			array[i] = instanceList.get(i);		
		}
		viewer.setSelection(new StructuredSelection(array), true);
	}
	
	
}

