/*******************************************************************************
* 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 25-Nov-2004
 *
 */

package uk.ac.ic.doc.kevlar.gui.internal.taskpane.searchpane;

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

import org.eclipse.swt.graphics.Point;

import uk.ac.ic.doc.kevlar.core.framework.IProgramIDDescription;
import uk.ac.ic.doc.kevlar.gui.internal.ADrawableObject;
import uk.ac.ic.doc.kevlar.gui.internal.AMouseListenerObject;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.TaskPaneManager;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.programpane.AProgramTreeNodeEntry;
import uk.ac.ic.doc.kevlar.gui.internal.taskpane.programpane.ProgramEntry;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.IScrollingWindow;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.StretchyBox;
import uk.ac.ic.doc.kevlar.gui.internal.widgets.scrollbars.VerticalScrollBar;
import uk.ac.ic.doc.kevlar.hial.IProgramSearchResult;

/**
 * @author Kate
 *
 */

public class ResultBox extends AMouseListenerObject implements IScrollingWindow {

	private Heading searchResult;
	private SearchPane parent;
	private StretchyBox contents;
	private VerticalScrollBar scrollbar;
	private int selected;
	private ArrayList<ProgramEntry> entries;
	
	public ResultBox(SearchPane parent) {
	    
	    super();
	    
		this.parent = parent;
		
		contents = new StretchyBox(false, true);
		contents.setSize(new Point(TaskPaneManager.TASK_PANE_INTERNAL_WIDTH, 0));
		super.addChild(contents);
		
		scrollbar = new VerticalScrollBar(this);
		super.addChild(scrollbar);
		
		selected = -1;
		entries = new ArrayList<ProgramEntry>();
		
	}
	
	public void setSelected(int index) {
	    
	    if (selected >= 0 && selected < entries.size()) {
	        
	        entries.get(selected).deselect();
	        
	    }

	    if (index >= -1 && index < entries.size()) {
	        
	        selected = index;
	        
	    }
	    
	    if (selected >= 0) {
	        
	        entries.get(selected).select();
	        
	    }
	    
	}
	
	public ProgramEntry getSelected() {
	    
	    if (selected >= 0 && selected < entries.size()) {
	        
	        return entries.get(selected);
	        
	    } else {
	        
	        return null;
	        
	    }
	    
	}
	
	public int moveUp() {
	    
	    if (selected >= 0 && selected < entries.size()) {
	        
	        entries.get(selected).deselect();
	        
	    }
	    
	    selected--;
	    
	    if (selected < 0) {
	        
	        selected = entries.size()-1;
	        
	    }
	    
	    if (selected >= 0) {
	        
	        entries.get(selected).select();
	        
	    }
	    
	    return selected;
	    
	}
	
	public int moveDown() {
	    
	    if (selected >= 0 && selected < entries.size()) {
	        
	        entries.get(selected).deselect();
	        
	    }
	    
	    selected++;
	    
	    if (selected >= entries.size()) {
	        
	        selected = 0;
	        
	    }
	    
	    if (selected >= 0) {
	        
	        entries.get(selected).select();
	        
	    }
	    
	    return selected;
	    
	}
	
    public void addChild(ADrawableObject child) {
        
        contents.addChild(child);
        
    }
    
    
    public void removeChild(ADrawableObject child) {
        
        contents.removeChild(child);
        
    }
    
    public void removeAllChildren() {
        
        contents.removeAllChildren();
        
    }
    
    public void setHorizontalPosition(int position) {
        
        contents.setPosition(new Point(-position, contents.getPosition().y));
        redraw();
        applyRedraw();
        
    }
    
    public void setVerticalPosition(int position) {
        
        contents.setPosition(new Point(contents.getPosition().x, -position));
        redraw();
        applyRedraw();
        
    }
    
    public int getHorizontalPosition() {
        
        return -contents.getPosition().x;
        
    }
    
    public int getVerticalPosition() {
        
        return -contents.getPosition().y;
        
    }
        
    public void updateDimensions() {
        
        super.updateDimensions();
        
        if (scrollbar != null) {
        
            scrollbar.setSize(area.height);
            
        }
        
    }
    
    public void childDimensionsChanged(ADrawableObject child) {
        
        if (child == contents) {
            
            scrollbar.setMaximum(child.getSize().y);
            
        }
        
    }	
	
	public void displayResults(List<IProgramSearchResult> results)
	 {
	 	/* display search resutls */
	 	
	    removeAllChildren();
	    entries.clear();
	    selected = -1;
	    
	 	searchResult = new Heading("Search Results:", new Point (160, 30), new Point(0,0));
		searchResult.setDivider("divider.png");
	 	
		addChild(searchResult);
	 	
	 	int startPos = (int)Math.floor((double)searchResult.getArea().height/AProgramTreeNodeEntry.ENTRY_HEIGHT);
		
		if (results.isEmpty())
		{
			searchResult.setSize(new Point(160,70));
			searchResult.setMessage("Nothing matches your search criteria");
		}
		else
		{
			for (int i=0; i<results.size(); i++)
			{
				IProgramIDDescription result = results.get(i).getProgramDescription();
				ProgramEntry entry = new ProgramEntry(result, startPos+i, parent, 0);
				entries.add(entry);
				addChild(entry);				
			}
		}

		redraw();
		
	 }

}
