/*******************************************************************************
* 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 18-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.api.programs.arguments.internal.argumentInfo;

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

import uk.ac.ic.doc.kevlar.api.programs.arguments.IArgumentHelp;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IArgumentVisitor;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IBasicArgumentValue;
import uk.ac.ic.doc.kevlar.api.programs.arguments.ISetArgumentInfo;
import uk.ac.ic.doc.kevlar.api.programs.arguments.ISetValue;
import uk.ac.ic.doc.kevlar.api.programs.arguments.internal.argumentInstance.SetArgumentValue;
import uk.ac.ic.doc.kevlar.publicUtil.collections.IImmutableMap;
import uk.ac.ic.doc.kevlar.publicUtil.collections.IImmutableSet;
import uk.ac.ic.doc.kevlar.publicUtil.collections.ImmutableHashMap;
import uk.ac.ic.doc.kevlar.publicUtil.collections.ImmutableHashSet;
import uk.ac.ic.doc.kevlar.publicUtil.dom.XMLUtil;

/**
 * @author Daniel Burke
 *
 */
public final class SetArgumentInfo extends AbstractArgumentInfo implements ISetArgumentInfo {

    private IImmutableMap<String, ISetValue> setValues;
    private IImmutableSet<String> allChildren;
    private String defaultValue;
    
    public SetArgumentInfo(
            String name, 
			IArgumentHelp help,
            boolean compulsary,
            Collection<ISetValue> setValues,
			String defaultValue
            )
    { 
    	super(name, compulsary, help);
    	
    	if(help.hasValidationMessage())
    	{
    	    throw new IllegalArgumentException("Only text arguments can have validation information");
    	}
    	
        if(setValues == null)
        {
        	throw new IllegalArgumentException("null param");
        }
        
        Map<String, ISetValue> tempSetValues =
            new HashMap<String, ISetValue>();
        for(ISetValue setValue : setValues)
        {
            if(tempSetValues.containsKey(setValue.getName()))
            {
            	throw new IllegalArgumentException("Must not have duplicate set values");
            }
            
            tempSetValues.put(setValue.getName(), setValue);
        }
        
        this.setValues = new ImmutableHashMap<String, ISetValue>(tempSetValues);

        HashSet<String> tempChildren = new HashSet<String>();

        for(ISetValue setValue : this.setValues.values()) {
        	if(!(setValue instanceof SetValue))
        	{
        		throw new IllegalArgumentException("setValues must all be of type SetValue!");
        	}

        	for(String child : ((SetValue)setValue).getChildren())
        	{
        		tempChildren.add(child);
        	}
        }
        
        allChildren = new ImmutableHashSet<String>(tempChildren);
        
        if(!this.setValues.containsKey(defaultValue))
        {
        	throw new IllegalArgumentException("Default value isn't one of the set values");
        }
        
        this.defaultValue = defaultValue;
        
        if(setValues.size() < 2)
        {
        	throw new IllegalArgumentException("There must be at least" +
        			"two set values");
        }
    }
    
    public void takeVisitor(IArgumentVisitor visitor)
    {
        visitor.setArgument(this);
    }
    
	/**
	 * 
	 * @return all the children of this argument
	 */
    public IImmutableSet<String> getChildren()
    {
        return allChildren;
    }
        
    public ISetValue getSetValue(String name)
    {
    	return setValues.get(name);
    }
    
    public IImmutableMap<String, ISetValue> getSetValues()
    {
        return setValues;
    }
	
	public IBasicArgumentValue getInitialValue()
	{
		return new SetArgumentValue(defaultValue);
	}
	
	public String toString()
	{
		return toString(0);
	}
	
	public String toString(int depth)
	{
		StringBuilder text = new StringBuilder();

		depth++;
		depth = XMLUtil.putOpenElem(text, "set-argument", depth);

		XMLUtil.putElement(text, "initial", defaultValue, depth);
		
		depth = XMLUtil.putOpenElem(text, "set-values", depth);
		
		for(ISetValue value : setValues.values())
		{
			text.append(((SetValue)value).toString(depth));
		}
		
		depth = XMLUtil.putCloseElem(text, "set-values", depth);
		
		depth = XMLUtil.putCloseElem(text, "set-argument", depth);
		depth--;

		return super.toString(depth, text.toString());
	}
}