/*******************************************************************************
* Copyright (c)                                                                *
*           2005   Tristan Allwood,                                            *
*           2005   Daniel Burke,                                               *
*           2005   Marc Hull,                                                  *
*           2005   Ekaterina Itskova,                                          *
*           2005   Steve Zymler                                                *
*                                                                              *
* Permission is hereby granted, free of charge, to any person obtaining a      *
* copy of this software and associated documentation files (the                *
* "Software"), to deal in the Software without restriction, including          *
* without limitation the rights to use, copy, modify, merge, publish,          *
* distribute, sublicense, and/or sell copies of the Software, and to           *
* permit persons to whom the Software is furnished to do so, subject to        *
* the following conditions:                                                    *
*                                                                              *
* The above copyright notice and this permission notice shall be included      *
* in all copies or substantial portions of the Software.                       *
*                                                                              *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS      *
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF                   *
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.       *
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY         *
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,         *
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE            *
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                       *
*******************************************************************************/
/*
 * Created on 09-Nov-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.taskpane;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;

import uk.ac.ic.doc.kevlar.gui.GUIManager;
import uk.ac.ic.doc.kevlar.gui.internal.pipeline.PipeLineManager;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.directorypane.ADirectoryTreeNodeEntry;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.loadpane.GUILoaderCallback;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.loadpane.MacroLoadStructure;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.savepane.GUISaverCallback;
import uk.ac.ic.doc.kevlar.hial.IHialContext;
import uk.ac.ic.doc.kevlar.hial.XMLLoadingException;

/**
 * @author Kate
 *
 */

public class HistoryPane extends AScrollingTaskPane{

	List<Thumbnail> history;
	Heading heading;
	int positionIndex;
	private final int OFFSET = 33;
    protected int selected;
	
	public HistoryPane(Point position) {
		
		super(position);
		
		scrollbar.setIncrement(15);
		history = new ArrayList<Thumbnail>();
		heading = new Heading("Pipelines constructed in this session:", new Point(160, 30), new Point(0,0));
		addChild(heading);
		
		positionIndex = 0;
		setSelected(-1);		
		
	}

	public void setSelected(int index) {
	    
	    if (selected >= 0 && selected < history.size()) {
	        
	        history.get(selected).deselect();
	        
	    }

	    if (index >= -1 && index < history.size()) {
	        
	        selected = index;
	        
	    }
	    
	    if (selected >= 0) {
	        
	        ADirectoryTreeNodeEntry selectedEntry = history.get(selected);
	        selectedEntry.select();
	        scrollTo(selectedEntry);
	        
	    } else {
	        
	        scrollTo(0);
	        
	    }
	    
	}
	
	public ADirectoryTreeNodeEntry getSelected() {
	    
	    if (selected >= 0 && selected < history.size()) {
	        
	        return history.get(selected);
	        
	    } else {
	        
	        return null;
	        
	    }
	    
	}
	
	public int moveUp() {
	    
	    if (selected >= 0 && selected < history.size()) {
	        
	        history.get(selected).deselect();
	        
	    }
	    
	    selected--;
	    
	    if (selected < 0) {
	        
	        selected = history.size()-1;
	        
	    }
	    
	    if (selected >= 0) {
	        
	        ADirectoryTreeNodeEntry selectedEntry = history.get(selected);
	        selectedEntry.select();
	        scrollTo(selectedEntry);
	        
	    }
	    
	    return selected;
	    
	}
	
	public int moveDown() {
	    
	    if (selected >= 0 && selected < history.size()) {
	        
	        history.get(selected).deselect();
	        
	    }
	    
	    selected++;
	    
	    if (selected >= history.size()) {
	        
	        selected = 0;
	        
	    }
	    
	    if (selected >= 0) {
	        
	        ADirectoryTreeNodeEntry selectedEntry = history.get(selected);
	        selectedEntry.select();
	        scrollTo(selectedEntry);
	        
	    }
	    
	    return selected;
	    
	}
	
	public void select() {
	    
	    ADirectoryTreeNodeEntry selectedEntry = getSelected();
	    
	    if (selectedEntry != null) {

	        selectedEntry.perform();
	        
	    }
	    
	}
		
	public String getName() {
		
		return "History";
	}
	
	public void addToHistory()
	{
		//add a thumbnail of a pipeline to the list of executed pipelines
		
		Image pipelineSnapshot = PipeLineManager.getSingleton().createSnapshot();
		
		//save context
		GUISaverCallback guiSaver = GUISaverCallback.getSingleton();
		IHialContext hialContext = GUIManager.getHialContext();
		String loadContent = GUIManager.getHial().saveContext(hialContext, guiSaver);
		Thumbnail thumb = new Thumbnail(pipelineSnapshot, positionIndex++, OFFSET, this, loadContent);
		
		addChild(thumb);
		
		redraw();
		applyRedraw();
	}	
	
	public void load(String loadContext)
	{
		GUILoaderCallback guiLoader = GUILoaderCallback.getSingleton();
		try 
		{
			GUIManager.getHial().loadContext(loadContext, guiLoader);
			List<MacroLoadStructure> macros = guiLoader.getMacroStructures();
			
			if (!macros.isEmpty())
			{
				for (MacroLoadStructure macro : macros)
				{
					PipeLineManager.getSingleton().loadMacro(macro, macros);
				}
			}
		}
		catch (XMLLoadingException e) 
		{	
			e.printStackTrace();
		}		
	}
	
	public String getLoadContext()
	{
		return null;
	}
	
}
