/*******************************************************************************
 * 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 java.util.ArrayList;
import java.util.Vector;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.ViewPart;
import org.eclipse.ui.plugin.AbstractUIPlugin;

import com.irtsaintexupery.fiacre.simulator.perspective.ViewsController;

public class ExecutedActionView extends ViewPart {

	public static final String ID = "com.irtsaintexupery.fiacre.simulator"
			+ ".perspective.executedActionView";

	private TableViewer viewer;
	private Action undo;
	
	private Vector <String> elementsTable = new Vector <String> ();
	
	class ViewLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}
		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}
		public Image getImage(Object obj) {
			if (ViewsController.INSTANCE.isDelay(getText(obj))) {
				Image im = 	getImage("icons/time_add.png");
				return im;
			}else {
				return PlatformUI.getWorkbench().getSharedImages()
						.getImage(ISharedImages.IMG_OBJS_TASK_TSK);				
			}
			
		}
	}
	/**
	 * The constructor.
	 */
	public ExecutedActionView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		
		viewer.setContentProvider(ArrayContentProvider.getInstance());
		//TODO add elements to the table
		viewer.setInput(elementsTable);
		viewer.setLabelProvider(new ViewLabelProvider());

		// Create the help context id for the viewer's control
		PlatformUI.getWorkbench().getHelpSystem().setHelp(viewer.getControl(), 
				"ExecutedActionView.viewer");
		getSite().setSelectionProvider(viewer);
		makeActions();
		contributeToActionBars();
		undo.setEnabled(false);
		undo.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO_DISABLED));
	}

	private void contributeToActionBars() {
		IActionBars bars = getViewSite().getActionBars();
		fillLocalPullDown(bars.getMenuManager());
		fillLocalToolBar(bars.getToolBarManager());
	}

	private void fillLocalPullDown(IMenuManager manager) {
		manager.add(new Separator());
		manager.add(undo);		
	}
	
	private void fillLocalToolBar(IToolBarManager manager) {
		manager.add(undo);
	}
	
	private void makeActions() {
		
		undo = new Action() {
			public void run() {
				if (!elementsTable.isEmpty()) {
					ViewsController.INSTANCE.undoAction();
				}
			}
		};
		undo.setText("Undo");
		undo.setToolTipText("Undo");
//		ImageDescriptor imageDescriptorUndo = AbstractUIPlugin
//				.imageDescriptorFromPlugin( "com.irtsaintexupery.fiacre"
//						+ ".simulator.perspective", "/icons/arrow_undo.png" );
		
		undo.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
				.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
//		
//		redo = new Action() {
//			public void run() {
//				showMessage("Redo executed");
//			}
//		};
//		redo.setText("Redo");
//		redo.setToolTipText("Redo tooltip");
//		ImageDescriptor imageDescriptorRedo = AbstractUIPlugin
//				.imageDescriptorFromPlugin( "com.irtsaintexupery.fiacre"
//						+ ".simulator.perspective", "/icons/arrow_redo.png" );
//		redo.setImageDescriptor(imageDescriptorRedo);
	}
	
	private void showMessage(String message) {
		MessageDialog.openInformation(
			viewer.getControl().getShell(),
			"Executed Action View",
			message);
	}

	@Override
	public void setFocus() {
		viewer.getControl().setFocus();

	}

	public void update(ArrayList<String> list) {
		elementsTable.clear();
		for (String s : list) {
			elementsTable.add(s);
		}
		if (elementsTable.isEmpty()) {
			undo.setEnabled(false);
			undo.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
					.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO_DISABLED));
		}else{
			undo.setEnabled(true);
			undo.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages()
					.getImageDescriptor(ISharedImages.IMG_TOOL_UNDO));
		}
		viewer.setContentProvider(ArrayContentProvider.getInstance());
		viewer.setInput(elementsTable);
		viewer.setLabelProvider(new ViewLabelProvider());
	}

}
