public class TopSpinTester {

    public static final java.util.Scanner INPUT = new java.util.Scanner(System.in);

    // Helper method for asking the user for valid ints
    public static int nextValidInt(String prompt) {
	boolean valid = false;
	int num = 0;
	while (!valid) {
	    try {
		System.out.print(prompt);
		num = INPUT.nextInt();
		INPUT.nextLine();
		valid = true;
	    } catch (java.util.InputMismatchException ime) {
		System.out.println("Sorry, that's not a number.");
		INPUT.nextLine();
	    }
	}
	return num;
    }

    // Main Method for testing
    public static void main(String[] args) {

	int size = nextValidInt("What is the size of the TopSpin? ");
	int spinSize = nextValidInt("What is the length of the spin? ");

	System.out.print("What implementation? (Array or Linked) ");
	String type = INPUT.nextLine();
	TopSpin ts = null;
	if (type.equals("Array")) {
	    ts = new ArrayTopSpin(size, spinSize);
	} else {
	    ts = new LLTopSpin(size, spinSize);
	}

	int moves = nextValidInt("How many random moves would you like for setup? ");
	for (int i = 0; i < moves; i++) {
	    int where = (int)((Math.random() * (size - 1)) + 1);
	    for (int j = 0; j < where; j++) {
		ts.shiftLeft();
	    }
	    ts.spin();
	}

	int choice = 0;
	do {
	    System.out.println(ts);
	    choice = nextValidInt("What move would you like to make?\n" + 
				  "1. Shift\n2. Spin\n3. Quit\n");
	    switch(choice) {
	    case 1:
		System.out.print("Which direction do you want to shift? (L/R) ");
		String dir = INPUT.nextLine();
		int num = nextValidInt("How many shifts in that direction? ");
		for (int i = 0; i < num; i++) {
		    if (dir.equals("L")) {
			ts.shiftLeft();
		    } else {
			ts.shiftRight();
		    }
		}
		break;
	    case 2:
		ts.spin();
		break;
	    case 3:
		break;
	    default:
		System.out.println("That's not a valid choice. ");
	    }
	} while (choice != 3 && !ts.isSolved());
	if (ts.isSolved()) {
	    System.out.println("CONGRATULATIONS!");
	    System.out.println(ts);
	}
    }
}
