/*******************************************************************************
* 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 24-Nov-2004
 *
 */
package uk.ac.ic.doc.kevlar.gui.internal.showpane;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import uk.ac.ic.doc.kevlar.core.framework.IShowExecutionManager;
import uk.ac.ic.doc.kevlar.core.framework.ShowExecutionManagerProvider;
import uk.ac.ic.doc.kevlar.core.framework.typecheck.IInterfaceClass;
import uk.ac.ic.doc.kevlar.gui.showers.AnyShowerProvider;
import uk.ac.ic.doc.kevlar.gui.showers.IShower;
import uk.ac.ic.doc.kevlar.gui.showers.IShowerProvider;
import uk.ac.ic.doc.kevlar.gui.showers.image.ImageShowerProvider;
import uk.ac.ic.doc.kevlar.publicInterfaces.IType;

/**
 * @author Daniel Burke
 *
 */
public class ShowPaneExecutionManager implements IShowExecutionManager {

    ShowPaneManager showPane;
    IShowerProvider anyProvider;
    
    Set<IShowerProvider> showerProviders = new HashSet<IShowerProvider>();
    Map<IShowerProvider, Map<String, IShower>> activeShowers = 
        new HashMap<IShowerProvider, Map<String, IShower>>();
    
    public ShowPaneExecutionManager(ShowPaneManager showPane) {
        
        this.showPane = showPane;
        
        loadShowers();
        
        ShowExecutionManagerProvider.setShowExecutionManager(this);
    }
    
    private void loadShowers()
    {
        //anyProvider = new ImageShowerProvider();//
        anyProvider = new AnyShowerProvider();
        /*
         * TODO : toa02 You just can't do this like this.
         * Annoyingly using the eclipse internal run target it lets u think u can,
         * but thats only because the whole code-base is added to the class-path.
         * ITextType isn't available at run-time from this class'es classloader,
         * so you can't read ITextType here.  As we discussed briefly, showers
         * need to be dynamically loaded from jar files, something that can be done
         * over xmas.  But for the moment the toString() method of the objects
         * you get should suffice, I hope.
         * 
         * I've created a new basic provider and basic shower and they work by just 
         * taking the IType and calling toString on them. For the purposes of the moment,
         * that should be adequate.
         * Sorry for having to re-work ur code like this, but if we want people to be able to
         * use this 'product' outside of eclipse it has to be done.
         * T
         */
        //showerProviders.add( new TextShowerProvider() );
        showerProviders.add( anyProvider );
        showerProviders.add( new ImageShowerProvider() );
    }

    /*
    public ShowPaneExecutionHandler makeExecutionHandler(IHialContext context)
    {
        return new ShowPaneExecutionHandler(context, this);
    }*/

    public synchronized IShower getShower(String name, String showerName, IInterfaceClass type) {
        
        IShowerProvider chosenProvider = null;
        Class<? extends IType> searchType = type.getTypeClass();
        
        if(showerName != null)
        {
	        for(IShowerProvider provider : showerProviders)
	        {
	            if(provider.getName().equals(showerName) || 
	                    provider.getClass().getCanonicalName().equals(showerName))
	            {
	                if(!provider.getType().isAssignableFrom(searchType))
	                {
	                    break;
	                }
	                chosenProvider = provider;
	                break;
	            }
	        }
        }

        if(chosenProvider == null)
        {
	        for(IShowerProvider provider : showerProviders)
	        {
	            // TODO: deb02: Use code Steve provides to find the closest type match
	            if(provider.getType().equals(searchType))
	            {
	                chosenProvider = provider;
	                break;
	            }
	        }
        }

        if(chosenProvider == null)
        {
            chosenProvider = anyProvider;
        }
        
        return getShower(name, chosenProvider);
    }
    
    private IShower getShower(String name, IShowerProvider provider)
    {    	
        IShower newShower = showPane.getShowerByDetails(name, provider);
        return newShower;
    }
    
}
