/*******************************************************************************
* 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 21-Oct-2004
 *
 */
package uk.ac.ic.doc.kevlar.api.programs.descriptions;

import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;

import uk.ac.ic.doc.kevlar.core.framework.PipelineProgram;
import uk.ac.ic.doc.kevlar.publicInterfaces.IProgramDescription;
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;
import uk.ac.ic.doc.kevlar.publicUtil.strings.StringUtil;

/**
 * @author Daniel Burke
 *  
 */
public class ProgramDescription implements IProgramDescription {

	private String name;
	private String author;
	private URL website;
	private int majorVersion;
	private int minorVersion;
	private String copyrightOwner;
	private int copyrightYear;
	private String description;
	private String programHelp;
	
	private boolean keywordTag;

    IImmutableSet<String> keywords;

    public ProgramDescription(
    		String name,
			int majorVersion,
			int minorVersion,
			String description
		)
    {
        setup(name, majorVersion, minorVersion, description);
        keywords = new ImmutableHashSet<String>(new ArrayList<String>());
    }

    public ProgramDescription(
    		String name,
			String author,
			URL website,
			int majorVersion,
			int minorVersion,
			String copyrightOwner,
    		int copyrightYear,
			Collection<String> keywords,
			String description,
			String programHelp)
    {
        setup(name, majorVersion, minorVersion, description);
        
        this.author = author;
        this.website = website;
        this.copyrightOwner = copyrightOwner;
        this.copyrightYear = copyrightYear;
        this.programHelp = programHelp;
        
        if(keywords == null)
        {
        	keywordTag = false;
            this.keywords = new ImmutableHashSet<String>(new ArrayList<String>());
        }
        else
        {
        	keywordTag = true;
            this.keywords = new ImmutableHashSet<String>(keywords);
        }
    }
    
    private void setup(
            String name, 
            int majorVersion, 
            int minorVersion, 
            String description)
    {
        if(name == null || description == null)
        {
            throw new IllegalArgumentException("Description and name may not be null");
        }
        
        if(!StringUtil.isSimpleIdentifier(name))
        {
        	throw new IllegalArgumentException("Name must be a simple identifier (alphanumerics and - and _)");
        }
        
        this.name = name;
        this.majorVersion = majorVersion;
        this.minorVersion = minorVersion;
        this.description = description;
    }
    
    
    public ProgramDescription(IProgramDescription progDesc) {
        setup(
                progDesc.getFriendlyName(), 
                progDesc.getMajorVersion(),
                progDesc.getMinorVersion(),
                progDesc.getDescription());
       
        this.author = progDesc.getAuthor();
        this.website = progDesc.getWebsite();
        this.copyrightOwner = progDesc.getCopyrightOwner();
        this.copyrightYear = progDesc.getCopyrightYear();
        this.keywords = progDesc.getKeywords();
        this.programHelp = progDesc.getProgramHelp();
        
        this.keywordTag = ((ProgramDescription)progDesc).keywordTag;

    }

    public String getDescription() {
        return description;
    }

    public String getFriendlyName() {
        return name;
    }

    public IImmutableSet<String> getKeywords() {
        return keywords;
    }
    
    public boolean hasAuthor()
    {
        return author != null;
    }
    
	public String getAuthor() {
		return author;
	}

	public boolean hasCopyright()
	{
	    return copyrightOwner != null;
	}
	
	public String getCopyrightOwner() {
		return copyrightOwner;
	}
	public int getCopyrightYear() {
		return copyrightYear;
	}
	public int getMajorVersion() {
		return majorVersion;
	}
	public int getMinorVersion() {
		return minorVersion;
	}
	
    public boolean hasProgramHelp()
    {
        return programHelp != null;
    }
	
	public String getProgramHelp() {
		return programHelp;
	}
	
    public boolean hasWebsite()
    {
        return website != null;
    }
	
	public URL getWebsite() {
		return website;
	}
	
	public String toString()
	{
		return toString(0);
	}
	
	public String toString(int depth)
	{
	    StringBuilder text = new StringBuilder();
	    
		depth = XMLUtil.putOpenElem(text, "description-model", depth);
		
	    XMLUtil.putElement(text, "name", name, depth);
	    depth = XMLUtil.putOpenElem(text, "version", depth);
	    
	    XMLUtil.putElement(text, "major", Integer.toString(majorVersion), depth);
	    XMLUtil.putElement(text, "minor", Integer.toString(minorVersion), depth);
	    
	    depth = XMLUtil.putCloseElem(text, "version", depth);
	    
	    XMLUtil.putElement(text, "description", description, depth );
	    
	    if(author != null)
	    {
	    	XMLUtil.putElement(text, "author", author, depth );
	    }
	    
	    if(website != null)
	    {
	    	XMLUtil.putElement(text, "website", website.toString(), depth );
	    }
	    
	    if(copyrightOwner != null)
	    {
	    	depth = XMLUtil.putOpenElem(text, "copyright", depth );
		    
	    	XMLUtil.putElement(text, "name", copyrightOwner, depth );
		    XMLUtil.putElement(text, "year", Integer.toString(copyrightYear), depth);
		    
		    depth = XMLUtil.putCloseElem(text, "copyright", depth );
	    }

	    if(keywordTag)
	    {
		    depth = XMLUtil.putOpenElem(text, "keywords", depth );
		    
		    for(String keyword : keywords)
		    {
		    	XMLUtil.putElement(text, "keyword", keyword, depth);
		    }
		    
		    depth = XMLUtil.putCloseElem(text, "keywords", depth );
		}
	    
	    if(programHelp != null)
	    {
	    	XMLUtil.putElement(text, "program-help", programHelp, depth);
	    }
	    
	    depth = XMLUtil.putCloseElem(text, "description-model", depth);
	    
	    return text.toString();
	}
	/*
	public boolean equals(Object obj) {
		if (!(obj instanceof PipelineProgram))
		{
			return false;
		}
	
		PipelineProgram p = (PipelineProgram)obj;
		return false;
		//return p.getCanonicalName().equals(this.getCanonicalName()); 
	}*/
}
