/*******************************************************************************
* 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.HashSet;
import java.util.regex.Pattern;

import uk.ac.ic.doc.kevlar.api.programs.arguments.IArgumentHelp;
import uk.ac.ic.doc.kevlar.api.programs.arguments.IBasicArgumentValue;
import uk.ac.ic.doc.kevlar.api.programs.arguments.ITextArgumentInfo;
import uk.ac.ic.doc.kevlar.api.programs.arguments.internal.argumentInstance.TextArgumentValue;
import uk.ac.ic.doc.kevlar.publicUtil.collections.IImmutableSet;
import uk.ac.ic.doc.kevlar.publicUtil.collections.ImmutableHashSet;
import uk.ac.ic.doc.kevlar.publicUtil.dom.XMLUtil;

/**
 * @author Daniel Burke
 *
 */
public abstract class TextArgumentInfo extends AbstractArgumentInfo implements ITextArgumentInfo { 
    
    private Pattern validationPattern;
    //private boolean isMultiline;
    private boolean isFilearg;
    private String initialValue;
    private static final IImmutableSet<String> emptySet = 
    	new ImmutableHashSet<String>(new HashSet<String>());
    
    public TextArgumentInfo(
            String name, 
            boolean compulsary,
			IArgumentHelp help,
            String initialValue,
            boolean isFilearg,
            Pattern validationPattern) 
    {
        super(name, compulsary, help);
        
        if(initialValue == null)
        {
        	initialValue = "";
        }
        

        
        this.initialValue = initialValue;
        //this.isMultiline = isMultiline;
        this.isFilearg = isFilearg;
        this.validationPattern = validationPattern;
        
    	if(help.hasValidationMessage() && !hasValidationPattern())
    	{
    	    throw new IllegalArgumentException("Validation help is only allowed if a regex is present for the argument");
    	}
    }

    public IImmutableSet<String> getChildren()
    {
        return emptySet;
    }
    
    public boolean hasValidationPattern()
    {
        return validationPattern != null;
    }
    
    public Pattern getValidationPattern()
    {
        return validationPattern;
    }

    /**
     * @return Returns the value that the argument should start with.
     */
    public IBasicArgumentValue getInitialValue() 
    {
        return new TextArgumentValue(initialValue);
    }
    
	public String toString()
	{
		return toString(0);
	}	
	public String toString(int depth)
	{
		StringBuilder text = new StringBuilder();
		
		depth++;
		depth = XMLUtil.putOpenElem(text, "text-argument", depth);

		XMLUtil.putElement(text, "initial", initialValue, depth);
	
		if(this instanceof MultilineArgumentInfo)
		{
		    XMLUtil.putElement(text, "is-multiline", null, depth);
		}
		
		if(isFilearg)
		{
		    XMLUtil.putElement(text, "is-file-arg", null, depth);
		}
		
		if(validationPattern != null)
		{
			XMLUtil.putElement(text, "regex", validationPattern.toString(), depth);
		}

		
		depth = XMLUtil.putCloseElem(text, "text-argument", depth);
		depth--;
		
		return super.toString(depth, text.toString());
	}
	
    public boolean isFilearg()
    {
        return isFilearg;
    }
    

    /*public boolean isMultiline()
    {
        return isMultiline;
    }*/
}
