/** 
 * Comparison Checker class for Approach 2
 * Paul Heider, Dan Schulte-Sasse, Jim Finnessy
 * March 24, 2001
 * V 2.0
 *
 * This class compares two strings containing the
 * output of the Authority Program and User Program.
 *
 */

import OurSinglyLinkedList2;

public class ComparisonChecker2 {

    protected int[] statistics; // This array holds any future stats we wish
                                // to maintain between cases.

    public static void main(String[] args) {
        SimpleOutput out = new SimpleOutput();
        int var;

        var = isCharByChar("bob martha \n bob","bob   martha \n bob");
        out.println(var);
    }

    public static int compareResults(String authority, String user) {

        return isCharByChar(authority, user);
        // 4 = isCharByChar
        // 3 = isLineAndOrder
        // 2 = isOrder
        // 1 = isNotOrder
        // 0 = None
    }

    private static int isCharByChar (String authority, String user) {
        // This method compares output to see if they are identical

        if(authority.compareTo(user)==0)
            return 4;
        return isLineAndOrder(authority, user);
    }

    private static int isLineAndOrder (String authority, String user) {
        // This method compares output to see if they have the elements
        // in order by line

        OurSinglyLinkedList2 authList = parseLine(authority);
        OurSinglyLinkedList2 userList = parseLine(user);

        if(authList.toString().compareTo(userList.toString())==0)
            return 3;

        return isOrder(authority, user);
    }

    private static int isOrder (String authority, String user) {
        // This method compares output to see if they are in the same order

        OurSinglyLinkedList2 authList = parseString(authority);
        OurSinglyLinkedList2 userList = parseString(user);

        if(authList.getSize() != userList.getSize())
            return 0;

        if(authList.toString().compareTo(userList.toString()) == 0)
            return 2;

        return isNotOrder (authList, userList);
    }

    private static int isNotOrder (OurSinglyLinkedList2 authList,
                                   OurSinglyLinkedList2 userList) {

        // This method compares output to see if they
        // contain the same key elements.

        for(;!authList.isEmpty(); authList.removeFromHead()) {
            if(userList.isEmpty()) {
                return 0;
            } else if (userList.remove(authList.head.value())==null) {
                return 0;
            }
        }

        return 1;

    }

    public int[] getStatistics () {
        // This method returns the current statistics being kept on the cases.

        return this.statistics;
    }

    private static OurSinglyLinkedList2 parseString (String text) {
        // turns each word of a string into an element of a list ignoring lines

        OurSinglyLinkedList2 wordList = new OurSinglyLinkedList2();

        for(int i=0; i<text.length();) {
            boolean flag = true;

            for(int j = i; flag; j++) {

                if(j==text.length()) {
                    wordList.add(text.substring(i,j));
                    return wordList;
                } else if(Character.isWhitespace(text.charAt(j))) {
                    if(i==j) {
                        i++;
                    } else {
                        wordList.add(text.substring(i,j));
                        flag = false;
                        j++;
                        i = j;
                    }
                }
                }
        }

        return wordList;
    }

    private static OurSinglyLinkedList2 parseLine (String text) {
        // turns each word of a string into an element of a list as well
        // as all the newline chars

        OurSinglyLinkedList2 wordList = new OurSinglyLinkedList2();
        SimpleOutput out = new SimpleOutput();

        for(int i=0; i<text.length();) {
            boolean flag = true;

            for(int j = i; flag; j++) {

                if(j==text.length()) {
                    wordList.add(text.substring(i,j));
                    return wordList;
                } else if(Character.getType(text.charAt(j)) == 15) {
                    wordList.add(text.substring(i,j));
                    //wordList.add("\\newline");
                } else if(Character.isWhitespace(text.charAt(j))) {
                    if(i==j) {
                        i++;
                    } else {
                        wordList.add(text.substring(i,j));
                        flag = false;
                        j++;
                        i = j;
                    }
                }
                }
        }
        return wordList;
    }

}

