package toy;

/**
 * ToyWarningHash basically encapsulates the relevant data in the warning list.
 * This object is generally useful in passing compiler warnings from the
 * Virtual Machine to the GUI interface.
 * The three main fields are:
 * <ul>
 * <li>a line of text
 * <li>the line of code it cites
 * <li>the index of the warning it belogs to (there can be more than one line
 * of text to a warning)
 * </ul>
 *
 * @author Brian Tsang
 * @version 7.0
 */

public class ToyWarningHash
{
    /**
     * The maximum number of warning lines that will be maintained.
     * Essentially, this prevents huge amounts of processing time wasted when
     * parsing a poorly written program.
     */
    public static final int MAX_WARNING = 100;

    /**
     * The array containing the text of a line.
     */
    private String textArray[];
    /**
     * The array containing the line of code cited.
     */
    private int citationArray[];
    /**
     * The array containing the index of the warning.
     */
    private int indexArray[];

    /**
     * The number of warning lines currently in this object.
     */
    private int cardinality;

    /**
     * Constructs a completely blank ToyWarningHash.
     */
    public ToyWarningHash()
    {
        cardinality = 0;
        textArray = new String[MAX_WARNING];
        citationArray = new int[MAX_WARNING];
        indexArray = new int[MAX_WARNING];
    }

    ////////////////////////////////////////////////////////////////////////////
    // add() adds a warning to this list (the text string is automatically
    // word-wrapped and indented)
    public void add(int line, String head, String text)
    {
        if (!maxedOut())
        {
            String wrappedString[] = toy.dialog.ToyToolkit.wordWrap(text, 76);

            if (head != null)
            {
                String oldWrappedString[] = wrappedString;
                wrappedString = new String[wrappedString.length + 1];

                wrappedString[0] = head;
                for (int ctr = 1; ctr < wrappedString.length; ctr++)
                    wrappedString[ctr] = oldWrappedString[ctr - 1];
            }


            for (int ctr = 0; ctr < Math.min(wrappedString.length, MAX_WARNING - cardinality); ctr++)
            {
                citationArray[cardinality + ctr] = line;
                textArray[cardinality + ctr] = wrappedString[ctr];
                indexArray[cardinality + ctr] = cardinality;
            }

            cardinality += Math.min(wrappedString.length, MAX_WARNING - cardinality);
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    // getLine() returns the line number in the code which corresponds to this
    // warning item
    public int getCitation(int index)
    {
        if (index >= 0 && index < cardinality)
            return citationArray[index];
        else
            return -1;
    }

    ////////////////////////////////////////////////////////////////////////////
    // getGroup() returns the group identifier for a particular item of the
    // warning list which will display the warnings
    public int getIndex(int index)
    {
        if (index >= 0 && index < cardinality)
            return indexArray[index];
        else
            return -1;
    }


    ////////////////////////////////////////////////////////////////////////////
    // getWarning() returns a string that corresponds to the item of the warning
    // list which will display the warnings
    public String getText(int index)
    {
        if (index >= 0 && index < cardinality)
            return textArray[index];
        else
            return "";
    }

    ////////////////////////////////////////////////////////////////////////////
    // getCardinality() returns the number of items that the warning list should
    // display
    public int getCardinality()
    {
        return cardinality;
    }

    ////////////////////////////////////////////////////////////////////////////
    // maxedOut() returns wheter or not the limit of items has been reached this
    // feature is meant to protect the user in case he accidently pastes a
    // really horrendous piece of code in the programTextArea
    public boolean maxedOut()
    {
        return cardinality >= MAX_WARNING;
    }
}
