/*******************************************************************************
* 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.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

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.IBoolArgumentInfo;
import uk.ac.ic.doc.kevlar.api.programs.arguments.internal.argumentInstance.BoolArgumentValue;
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;

/**
 * Argument info for a boolean type argument.
 * 
 * @author Daniel Burke
 */
public final class BoolArgumentInfo extends AbstractArgumentInfo implements
        IBoolArgumentInfo {

    // Arguments that should be available when this argument is true
    private IImmutableSet<String> trueChildren;

    // Arguments that should be available when this argument is false
    private IImmutableSet<String> falseChildren;

    // Cached union of trueChildren and falseChildren
    private IImmutableSet<String> allChildren;

    // cached empty collection
    private static final Collection<String> EMPTY_COLLECTION = new ArrayList<String>();

    // Argument is initially on or off
    private boolean initialValue;

    /**
     * 
     * @param name
     *            Argument name
     * @param compulsary
     *            Is argument compulsary
     * @param help
     *            Argument context sensitive help
     * @param trueChildren
     *            argument names that should become available when this argument
     *            is true. null value = no children
     * @param falseChildren
     *            argument names that should become available when this argument
     *            is false. null value = no children
     * @param initialValue
     *            Should the argument have an initial value of on or off?
     * @throws IllegalArgumentException
     *             if the help contains an unnecessary validation message.
     */
    public BoolArgumentInfo(String name, boolean compulsary,
            IArgumentHelp help, Collection<String> trueChildren,
            Collection<String> falseChildren, boolean initialValue) {
        super(name, compulsary, help);

        if (help.hasValidationMessage()) {
            throw new IllegalArgumentException(
                    "Only text arguments can have validation information");
        }

        // Null children is equivalent to no children
        if (trueChildren == null) {
            trueChildren = EMPTY_COLLECTION;
        }

        if (falseChildren == null) {
            falseChildren = EMPTY_COLLECTION;
        }

        this.trueChildren = new ImmutableHashSet<String>(trueChildren);
        this.falseChildren = new ImmutableHashSet<String>(falseChildren);

        // generate union of children
        Set<String> tempChildren = new HashSet<String>();
        for (String child : this.trueChildren) {
            tempChildren.add(child);
        }

        for (String child : this.falseChildren) {
            tempChildren.add(child);
        }

        allChildren = new ImmutableHashSet<String>(tempChildren);

        this.initialValue = initialValue;
    }

    // implementation of the visitor pattern
    public void takeVisitor(IArgumentVisitor visitor) {
        visitor.boolArgument(this);
    }

    public IImmutableSet<String> getChildren() {
        return allChildren;
    }

    public IBasicArgumentValue getInitialValue() {
        return new BoolArgumentValue(initialValue);
    }

    /**
     * @return Returns the argument names to be visible when this argument is
     *         false
     */
    public IImmutableSet<String> getFalseChildren() {
        return falseChildren;
    }

    /**
     * @return Returns the argument names to be visible when this argument is
     *         true
     */
    public IImmutableSet<String> getTrueChildren() {
        return trueChildren;
    }

    public String toString() {
        return toString(0);
    }

    public String toString(int depth) {
        StringBuilder text = new StringBuilder();

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

        if (initialValue) {
            XMLUtil.putElement(text, "initially-on", null, depth);
        }

        if (trueChildren.size() > 0) {
            depth = XMLUtil.putOpenElem(text, "true-children", depth);
            for (String child : trueChildren) {
                XMLUtil.putElement(text, "child", child, depth);
            }
            depth = XMLUtil.putCloseElem(text, "true-children", depth);
        }

        if (falseChildren.size() > 0) {
            depth = XMLUtil.putOpenElem(text, "false-children", depth);
            for (String child : trueChildren) {
                XMLUtil.putElement(text, "child", child, depth);
            }
            depth = XMLUtil.putCloseElem(text, "false-children", depth);
        }

        depth = XMLUtil.putCloseElem(text, "bool-argument", depth);
        depth--;

        // Use super class' helper method for printing <argument> XML
        return super.toString(depth, text.toString());
    }
}
