/**
 * Tests the implementation of a Revolver for correctness.
 * Runs a series of tests, printing expected results and actual results
 * to the screen.
 *
 * @author Samuel "Henry" Colt
 */
public class RevolverTester {

  public static void main(String[] args) {

    System.out.println("Creating a new Revolver.");
    Revolver gun = new Revolver();
    //gun should be closed and emtpy
    System.out.println("isOpen [false]: " + gun.isOpen());
    System.out.println("fire() [false]: " + gun.fire());

    //error checking
    System.out.println();
    System.out.print("countShells [IllegalStateEx]: ");
    try {
      System.out.println(gun.countShells());
    }catch (IllegalStateException ise) {
      System.out.println(ise);
    }
    System.out.print("countEmptyChambers [IllegalStateEx]: ");
    try {
      System.out.println(gun.countEmptyChambers());
    }catch (IllegalStateException ise) {
      System.out.println(ise);
    }
    System.out.print("load(1) [IllegalStateEx]: ");
    try {
      gun.load(1);
    }catch (IllegalStateException ise) {
      System.out.println(ise);
    }
    System.out.print("empty() [IllegalStateEx]: ");
    try {
      gun.empty();
    }catch (IllegalStateException ise) {
      System.out.println(ise);
    }
    //should do nothing
    gun.close();

    System.out.println();
    System.out.println("open()");
    gun.open();
    System.out.println("isOpen [true]: " + gun.isOpen());
    //error checking
    System.out.print("fire() [IllegalStateEx]: ");
    try {
      System.out.println(gun.fire());
    }catch (IllegalStateException ise) {
      System.out.println(ise);
    }

    //load gun
    System.out.println();
    System.out.println("load(4)");
    gun.load(4);
    System.out.println("countShells [4]: " + gun.countShells());
    System.out.println("countEmptyChambers [2]: " + gun.countEmptyChambers());
    System.out.println("spin()");
    gun.spin();
    System.out.println("close()");
    gun.close();

    //fire
    System.out.println();
    System.out.print("fire() * 6 [true * 4 and false * 2 in cyclic order]: ");
    for (int i = 0; i < 6; i++) {
      System.out.print(gun.fire() + " ");
    }
    System.out.println();

    //checking that fired rounds don't fire again
    System.out.println("spin()");
    gun.spin();
    System.out.print("fire() * 6 [false * 6]: ");
    for (int i = 0; i < 6; i++) {
      System.out.print(gun.fire() + " ");
    }
    System.out.println();

    //open gun again
    System.out.println();
    System.out.println("open()");
    gun.open();
    System.out.println("countShells [4]: " + gun.countShells());
    System.out.println("countEmptyChambers [2]: " + gun.countEmptyChambers());
    System.out.print("load(3) [IllegalArgumentEx]: ");
    try {
      gun.load(3);
    }catch (IllegalArgumentException iae) {
      System.out.println(iae);
    }
    System.out.println("countShells [4]: " + gun.countShells());
    System.out.println("countEmptyChambers [2]: " + gun.countEmptyChambers());


    //empty gun
    System.out.println();
    System.out.println("empty()");
    gun.empty();
    System.out.println("countShells [0]: " + gun.countShells());
    System.out.println("countEmptyChambers [6]: " + gun.countEmptyChambers());

    //checking spin
    System.out.println();
    System.out.println("Russian roulette with an unlucky friend...");
    System.out.println("load(2); close();");
    gun.load(2);
    gun.close();
    System.out.println("Firing until first round fires...");
    while (!gun.fire());
    System.out.println("spin()");
    gun.spin();
    System.out.println("Firing [5/6 times you run this, there should be clicks before the boom]: ");
    while(true) {
      if (gun.fire()) {
        System.out.print("BOOM!");
        break;
      }else {
        System.out.print("click...");
      }
    }
    System.out.println();

    System.out.println("DONE.");
  }

}
