/*******************************************************************************
* 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 20-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.taskpane;

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

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;

import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.animation.AnimationManager;
import uk.ac.ic.doc.kevlar.gui.internal.animation.SlideAnimation;
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.Utils;
import uk.ac.ic.doc.kevlar.gui.internal.popup.PopUpBox;
import uk.ac.ic.doc.kevlar.gui.internal.popup.PopUpManager;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.directorypane.DirectoryPane;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.loadpane.LoadPane;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.programpane.ProgramPane;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.savepane.SavePane;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.searchpane.SearchPane;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.listbox.IListSelectionListener;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.listbox.ListBox;

/**
 * @author Kate
 *
 */
public class TaskPaneManager extends AMouseListenerObject implements IListSelectionListener{
	
	public static final int TASK_PANE_WIDTH = 200;
	public static final int TASK_PANE_INTERNAL_WIDTH = 178;	
	public static final String TASK_PANE_SKIN_NAME = "taskpane2";
	public static final Point PANE_POSITION = new Point(12, 33);

	private static TaskPaneManager singleton;
	private List<ATaskPane> availablePanes;
	private ATaskPane currentPane;
	private boolean expanded;
	
	private TaskPaneExpandButton expandButton;
	private SelectTaskPaneButton selectButton;
	
	private ListBox panesList;
	private PopUpBox popup;
	
	public static final int PROGRAM = 0;
	public static final int HISTORY = 1;
	public static final int SEARCH = 2;
	public static final int LOAD = 3;
	public static final int SAVE = 4;
	public static final int DIRECTORY = 5;
	
	public TaskPaneManager() {
		
//	    super(new Point(10, 0), true, false);
	    super();
	    setPosition(new Point(10, 0));
	    setAlignment(true, false);
		setSizeFillVertical(new Point(TASK_PANE_WIDTH, 0));
	    
		availablePanes = new ArrayList<ATaskPane>();
		availablePanes.add(new ProgramPane(PANE_POSITION));
		availablePanes.add(new HistoryPane(PANE_POSITION));
		availablePanes.add(new SearchPane(PANE_POSITION));
		availablePanes.add(new LoadPane(PANE_POSITION));
		availablePanes.add(new SavePane(PANE_POSITION));
		availablePanes.add(new DirectoryPane(PANE_POSITION));
		
		if (!availablePanes.isEmpty()) {
		
		    setCurrentPane(availablePanes.get(0));
		    
		}
		
		expandButton = new TaskPaneExpandButton(this);
		expandButton.setPosition(new Point(2, 20));
		addChild(expandButton);
		
		selectButton = new SelectTaskPaneButton(this);
		selectButton.setPosition(new Point(175, 15));
		addChild(selectButton);
		
	}
	
	public static TaskPaneManager getSingleton() {
		
		return singleton;
		
	}
	
	public static void createSingleton() {
		
		if (singleton==null) {
			
			singleton = new TaskPaneManager();
			
		}
		
	}
	
	public void drawBefore(GC gc, Rectangle redrawArea) {
		
		Utils.drawVerticalBar(gc, TASK_PANE_SKIN_NAME+"a", new Rectangle(-redrawArea.x, -redrawArea.y, area.width, area.height/2));
		Utils.drawVerticalBar(gc, TASK_PANE_SKIN_NAME+"b", new Rectangle(-redrawArea.x, -redrawArea.y+area.height/2, area.width, area.height/2));

		if (currentPane != null) {
		
			gc.setFont(FontCache.get("Arial", 10, SWT.BOLD));
			gc.setForeground(ColorCache.get(255, 255, 255));
			gc.drawText(currentPane.getName(), 20-redrawArea.x, 12-redrawArea.y, true);
			
		}
				
	}
	
	public boolean isExpanded() {
	    
	    return expanded;
	    
	}
	
	public void expand() {
	    
        AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(TASK_PANE_WIDTH, 0)));
        expanded = true;
        expandButton.select();
	    
	}
	
	public void contract() {
	    
		currentPane.reset();
        AnimationManager.getSingleton().addAnimation(new SlideAnimation(this, new Point(10, 0)));
        expanded = false;
        expandButton.deselect();
	    
	}
	
	public void setCurrentPane(ATaskPane pane) {
	    
	    if (currentPane != null) {
	        
	        removeChild(currentPane);
	        currentPane.deactivate();
	        
	    }
	    currentPane = pane;
	    currentPane.reset();
	    addChild(currentPane);	  
	    currentPane.activate();
	    
/*	    if (pane instanceof SearchPane)
	    {
	    	((SearchPane)pane).activateSearchBox();
	    	((SearchPane)pane).getTextBox().updateDimensions();
	    } */
	    
	    redraw();
	    
	}
	
	public boolean isCurrentPane(int pane) {
	    
	    return currentPane == availablePanes.get(pane);
	    
	}
	
	public Rectangle getPaneArea()
	{
		return currentPane.getAlignedArea();
	}
	
	public void parentResized(Point size) {

	    super.parentResized(size);
	    expandButton.setPosition(new Point(2, (size.y/2)-23));
	    
	}
	
	public void paneSelectionButtonPressed(Point position)
	{
		panesList = new ListBox();
		panesList.setData(getAvailablePanes());
		panesList.setSelectionListener(this);
		
    	popup = new PopUpBox();
    	popup.setSize(new Point(150, 110));
	    popup.setName("Select a task pane");
	    popup.setContents(panesList);
	    popup.popUpAt(position);
    	
	}
	
	private List<String> getAvailablePanes()
	{
		List<String> panes = new ArrayList<String>();
		
		for (int i = 0; i<availablePanes.size(); i++)
		{
			ATaskPane pane = availablePanes.get(i);
			if (!pane.equals(currentPane))
			{
				panes.add(pane.getName());
			}
		}
		
		return panes;
	}

	public void entrySelected(Object entry) 
	{
			for (int i = 0; i<availablePanes.size(); i++)
			{
				ATaskPane pane = availablePanes.get(i);
				if (pane.getName().equals(entry))
				{
					setCurrentPane(pane);
					break;
				}
			}
			
			PopUpManager.getSingleton().hidePopUp(popup);
				
	}
	
	public ATaskPane getPane(int pane) {
	    
	    return availablePanes.get(pane);
	    
	}
	
	public void setPane(int pane)
	{
		setCurrentPane(getPane(pane));
	}
	
	public void setExpandButton(boolean selected)
	{
		/* this method gets called when a task pane is accessed
		 * from the toolbar. This ensures that animations and images of
		 * buttons are properly set.
		 */
		
		expandButton.updateImage(selected);
	
	}
	
}
