import java.util.*;
import java.io.*;

public class QuizTree {

    private QuizTreeNode head;

    // PROVIDED
    // Returns the given percent rounded to two decimal places.
    private double roundTwoPlaces(double percent) {
        return (double) Math.round(percent * 100) / 100;
    }

    public int size() {
        return size(head);
    }

    private int size(QuizTreeNode root) {
        if (root == null) {
            return 0;
        }
        return 1 + size(root.left) + size(root.right);
    }

    public QuizTree(Scanner inputFile) {
        this.head = null; 
        this.head = populateTree(inputFile, head);
    }

    private void printTree(QuizTreeNode root) {
        if (root != null) {
            System.out.println(root.data);
            printTree(root.left);
            printTree(root.right);
        }
    }
        
    private QuizTreeNode populateTree(Scanner inputFile, QuizTreeNode root) {
        if (inputFile.hasNextLine()) {
            String next = inputFile.nextLine();
            String data;
            int score;

            // we've reached a result, make a node but dont recurse
            if (next.startsWith("END:")) {
                data = next.substring(4, next.lastIndexOf("-"));
                score = Integer.parseInt(next.substring(next.lastIndexOf("-") + 1));
                return new QuizTreeNode(data, score);
            } 

            // otherwise, we're not at the end yet, make a node and keep going
            else {                 
                data = next.substring(0, next.lastIndexOf("-"));
                score = Integer.parseInt(next.substring(next.lastIndexOf("-") + 1));
                root = new QuizTreeNode(data, score);
                root.left = populateTree(inputFile, root.left);
                root.right = populateTree(inputFile, root.right);
            }
        }
        return root;
    }

    public void takeQuiz(Scanner console) {
        takeQuiz(console, head);
    }

    private void takeQuiz(Scanner console, QuizTreeNode root) {
        if (root.left != null && root.right != null) {
            String left = root.data.substring(0, root.data.lastIndexOf("/"));
            String right = root.data.substring(root.data.lastIndexOf("/")+1, root.data.length());
            System.out.print("Do you prefer " + left + " or " + right + "? ");
            String input = console.nextLine();

            while (!input.equalsIgnoreCase(left) || !input.equalsIgnoreCase(right)) {
                System.out.println("  Invalid response; try again.");
                System.out.print("Do you prefer " + left + " or " + right + "? ");
                input = console.nextLine();
            }

            takeQuiz(console, input.equalsIgnoreCase(left) ? root.left : root.right);
        }
        
        else {
            System.out.println("Your result is: " + root.data);
            System.out.println("Your score is: " + root.score);
        }
    }

    public void export(PrintStream outputFile) {
        export(outputFile, this.head);
    }

    private void export(PrintStream outputFile, QuizTreeNode root) {
        if (root.left == null && root.right == null) {
            outputFile.println("END:" + root.data + "-" + root.score);
        } else {
            outputFile.println(root.data + "-" + root.score);
        }

        if (root.left != null) {
            export(outputFile, root.left);
        }
        if (root.right != null) {
            export(outputFile, root.right);
        }
    }

    //public void addQuestion(String toReplace, String choices, String leftResult, String rightResult) {
    //}

    public void addQuestion(String toReplace, String choices, String leftResult, String rightResult) {
        head = addQuestion(head, toReplace.toLowerCase(), choices, leftResult, rightResult);
    }

    private QuizTreeNode addQuestion(QuizTreeNode root, String toReplace, String choices, String leftResult, String rightResult) {
        if (root == null) {
            return null;
        }

        if (root.left == null && root.right == null && root.data.equalsIgnoreCase(toReplace)) {
            // Parse choices to get left choice, right choice, and score
            String choiceText = choices.substring(0, choices.lastIndexOf("-"));
            int choiceScore = Integer.parseInt(choices.substring(choices.lastIndexOf("-") + 1));

            // Create the new choice node
            QuizTreeNode choiceNode = new QuizTreeNode(choiceText, choiceScore);

            // Parse leftResult to get left result data and score
            String leftData = leftResult.substring(0, leftResult.lastIndexOf("-"));
            int leftScore = Integer.parseInt(leftResult.substring(leftResult.lastIndexOf("-") + 1));
            QuizTreeNode leftNode = new QuizTreeNode(leftData, leftScore);

            // Parse rightResult to get right result data and score
            String rightData = rightResult.substring(0, rightResult.lastIndexOf("-"));
            int rightScore = Integer.parseInt(rightResult.substring(rightResult.lastIndexOf("-") + 1));
            QuizTreeNode rightNode = new QuizTreeNode(rightData, rightScore);

            // Set the left and right children of the new choice node
            choiceNode.left = leftNode;
            choiceNode.right = rightNode;

            return choiceNode;
        } else {
            root.left = addQuestion(root.left, toReplace, choices, leftResult, rightResult);
            root.right = addQuestion(root.right, toReplace, choices, leftResult, rightResult);
            return root;
        }
    }



    public static class QuizTreeNode {

        public final String data;
        public final int score;
        public QuizTreeNode left;
        public QuizTreeNode right;

        public QuizTreeNode(String data, int score) {
            this(data, score, null, null);
        }

        public QuizTreeNode(String data, int score, QuizTreeNode left, QuizTreeNode right) {
            this.data = data; 
            this.score = score; 
            this.left = left; 
            this.right = right;
        }
    }
}
