package hu.bme.mit.spaceship;

/**
* A simple spaceship with two proton torpedos and four lasers
*/
public class GT4500 implements SpaceShip {

  private TorpedoStore primaryTorpedoStore;
  private TorpedoStore secondaryTorpedoStore;

  private boolean wasPrimaryFiredLast = false;

  public GT4500() {
    this.primaryTorpedoStore = new TorpedoStore(10);
    this.secondaryTorpedoStore = new TorpedoStore(10);
  }

  public boolean fireLasers(FiringMode firingMode) {
    // TODO not implemented yet
    return false;
  }

  /**
  * Tries to fire the torpedo stores of the ship.
  *
  * @param firingMode how many torpedo bays to fire
  * 	SINGLE: fires only one of the bays.
  * 			- For the first time the primary store is fired.
  * 			- To give some cooling time to the torpedo stores, torpedo stores are fired alternating.
  * 			- But if the store next in line is empty the ship tries to fire the other store.
  * 			- If the fired store reports a failure, the ship does not try to fire the other one.
  * 	ALL:	tries to fire both of the torpedo stores.
  *
  * @return whether at least one torpedo was fired successfully
  */
  @Override
  public boolean fireTorpedos(FiringMode firingMode) {

    boolean firingSuccess = false;

    switch (firingMode) {
      case SINGLE:
        if (wasPrimaryFiredLast) {
          // try to fire the secondary first
          if (! secondaryTorpedoStore.isEmpty()) {
            firingSuccess = secondaryTorpedoStore.fire(1);
            wasPrimaryFiredLast = false;
          }
          else {
            // although primary was fired last time, but the secondary is empty
            // thus try to fire primary again
            if (! primaryTorpedoStore.isEmpty()) {
              firingSuccess = primaryTorpedoStore.fire(1);
              wasPrimaryFiredLast = true;
            }

            // if both of the stores are empty, nothing can be done, return failure
          }
        }
        else {
          // try to fire the primary first
          if (! primaryTorpedoStore.isEmpty()) {
            firingSuccess = primaryTorpedoStore.fire(1);
            wasPrimaryFiredLast = true;
          }
          else {
            // although secondary was fired last time, but primary is empty
            // thus try to fire secondary again
            if (! secondaryTorpedoStore.isEmpty()) {
              firingSuccess = secondaryTorpedoStore.fire(1);
              wasPrimaryFiredLast = false;
            }

            // if both of the stores are empty, nothing can be done, return failure
          }
        }
        break;

      case ALL:
        // try to fire both of the torpedos
	if (! primaryTorpedoStore.isEmpty()) {
            firingSuccess = primaryTorpedoStore.fire(1);
            wasPrimaryFiredLast = true;
          }
	 if (! secondaryTorpedoStore.isEmpty()) {
              firingSuccess = secondaryTorpedoStore.fire(1);
              wasPrimaryFiredLast = false;
            }
        //TODO implement feature

        break;
    }

    return firingSuccess;
  }

}

@@ -1275,24 +1275,236 @@
  SINGLE:%0A   
+     firingSuccess = single();%0A        break;%0A%0A      case ALL:%0A        firingSuccess = all();%0A%0A        break;%0A    %7D%0A%0A    return firingSuccess;%0A  %7D%0A%0A  private boolean single()%7B%0A    boolean firingSuccess = false;%0A%0A
      if (was
@@ -2112,36 +2112,33 @@
 done, return fai
-lure
+$
 %0A          %7D%0A   
@@ -2667,37 +2667,24 @@
            %7D
-%0A%0A           
  // if both 
@@ -2739,20 +2739,17 @@
 turn fai
-lure
+$
 %0A       
@@ -2839,17 +2839,24 @@
 orpedos%0A
-%09
+        
 if (! pr
@@ -2995,17 +2995,24 @@
       %7D%0A
-%09
+        
  if (! s
@@ -3167,63 +3167,412 @@
   %7D%0A
+%0A
       
-  //TODO implement feature%0A%0A        break;%0A    %7D%0A
+return firingSuccess;%0A  %7D%0A%0A  private boolean all()%7B%0A    boolean firingSuccess = false;%0A%0A    if (! primaryTorpedoStore.isEmpty()) %7B%0A            firingSuccess = primaryTorpedoStore.fire(1);%0A            wasPrimaryFiredLast = true;%0A          %7D%0A         if (! secondaryTorpedoStore.isEmpty()) %7B%0A              firingSuccess = secondaryTorpedoStore.fire(1);%0A              wasPrimaryFiredLast = false;%0A   
 %0A   
@@ -3602,7 +3602,13 @@
   %7D%0A
-%0A%7D
+  %0A%0A%7D%0A%0A 
 %0A

switch case number reduction at GT4500.java