/*******************************************************************************
* 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.io.FileNotFoundException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Display;

import uk.ac.ic.doc.kevlar.gui.internal.caches.ColorCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.FontCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.ImageCache;
import uk.ac.ic.doc.kevlar.gui.internal.caches.Utils;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.directorypane.FileEntry;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.loadpane.LoadPane;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.scrollbars.AScrollbar;

/**
 * @author Kate
 *
 */

public class Thumbnail extends FileEntry {

    private static final String FADER_IMAGE = "semitransparent.png";
    
/*	public static final int THUMB_WIDTH = 100;
	public static final int THUMB_HEIGHT = 70; */
	public static final int THUMB_WIDTH = TaskPaneManager.TASK_PANE_INTERNAL_WIDTH-AScrollbar.SCROLLBAR_SIZE;
	public static final int THUMB_HEIGHT = 118;
	public static final int GAP = THUMB_HEIGHT+3;
	
	Image pipeline;
	int positionIndex;
	int positionOffset;
	ATaskPane pane;
	String name;
	Heading fileName;
	String loadContext;
	Image fader;
	
	public Thumbnail(Image pipeline, int positionIndex, int positionOffset, ATaskPane pane, String loadContext)
	{
		super();
		this.pipeline = pipeline;
		this.positionIndex = positionIndex;
		this.positionOffset = positionOffset;
		this.pane = pane;
		this.loadContext = loadContext;
		initialise();
	}
	
	public Thumbnail(Image pipeline, String name, int positionIndex, int positionOffset, ATaskPane pane)
	{
		super();
		this.pipeline = pipeline;
		this.name = name;
		this.positionIndex = positionIndex;
		this.positionOffset = positionOffset;
		this.pane = pane;
		initialise();
	}
	
	private void initialise()
	{
		Point position = new Point((TaskPaneManager.TASK_PANE_INTERNAL_WIDTH-AScrollbar.SCROLLBAR_SIZE)/2-THUMB_WIDTH/2, positionIndex*GAP + positionOffset);
		setPosition(position);
		setSize(new Point(THUMB_WIDTH, THUMB_HEIGHT));
		
		try {
		    
		    fader = ImageCache.get(FADER_IMAGE);
		    
		} catch (FileNotFoundException e) {
		    
		    System.err.println("File not found: "+FADER_IMAGE);
		    fader = new Image(Display.getDefault(), 1, 1);
		    
		}
		
/*		if (name!=null)
		{
			fileName = new Heading(name, new Point(position.x, position.y-5), new Point(0,0));
		} */
	}
	
	public void drawBefore(GC gc, Rectangle redrawArea)
	{
		
	    gc.setForeground(ColorCache.get(0, 0, 0));
	    Utils.drawImage(gc, pipeline, new Rectangle(-redrawArea.x, -redrawArea.y, area.width, area.height));

	    if (!isMouseOver() && !selected) {
	        
	        Rectangle faderBounds = fader.getBounds();
	        gc.drawImage(fader, 0, 0, faderBounds.width, faderBounds.height, -redrawArea.x, -redrawArea.y, area.width, area.height);
	        
	    }
	    
		//draw image
		if (name!=null)
		{
			//draw heading
//			fileName.drawAfter(gc, redrawArea);
		    
		    gc.setFont(FontCache.get("Arial", 10, SWT.BOLD));
		    Utils.drawText(gc, name, new Rectangle(-redrawArea.x, THUMB_HEIGHT-20-redrawArea.y, THUMB_WIDTH, 20), Utils.ALIGN_CENTER);
		} 
	}
	
	public String getName()
	{
		return name;
	}
	
	public void mousePress(Point position, int mouseButton)
	{
	    
	    perform();
	    
	}
	
	public void mouseEnter() {
	 
	    redraw();
	    
	}
	
	public void mouseExit() {
	    
	    redraw();
	    
	}
	
	public void perform() {
	    
		//GUILoaderCallback guiLoader = GUILoaderCallback.getSingleton();
		if (name==null)
		{
			((HistoryPane)pane).load(loadContext);
			//GUIManager.getHial().loadContext(loadContext, guiLoader);		
		}
		else
		{
			if (pane instanceof LoadPane)
			{
				((LoadPane)pane).load(name);
				//GUIManager.getHial().loadContextFromFile(name, guiLoader);
			}
		}	    
	    
	}
	
}
