package ui;

import ui.elem.*;

import java.util.ArrayList;

public abstract class Stage {
    private ArrayList<Element> elementList = new ArrayList<Element>();
    
    private Display gameVar;
    
    private long ticks = 0;
    
    /**
     * Called when this stage becomes the current stage
     */
    public abstract void init();
    
    /**
     * Called whenever a choice is clicked by the mouse
     */
    public abstract void choiceClicked(Element elementVar);
    
    /**
     * Called after a task is scheduled after certain amount of ticks
     */
    public abstract void taskPerformed();
    
    /**
     * Simple task scheduling method, after specified number of ticks, run taskPerformed
     * 1 tick is equivalent to 1 twentieth of a second
     */
    public void scheduleTask(long intVar) { 
        ticks += intVar;
        if(ticks == 0 && intVar == 0) this.taskPerformed();
    }
    
    public void decreaseTicks() {
        if(ticks > 0) {
            ticks--;
            if(ticks == 0) this.taskPerformed();
        }
    }
    
    /**
     * Add a new element to this stage;
     */
    public void addElements(Element... elementVar) {
        for(Element element : elementVar) {
            if(elementList.indexOf(elementVar) < 0) {
                elementList.add(element);
                element.setGameInstance(gameVar);
                element.createElement();
            }
        }
        syncElements();
    }
    
    /**
     * Remove the given elements from this stage
     */
    public void removeElements(Element... elementVar) {
        for(Element element : elementVar) {
            elementList.remove(element);
            element.removeElement();
        }
        syncElements();
    }
    
    public void syncElements() {
        for(int i = 0; i < elementList.size(); i++) {
            Element element = elementList.get(i);
            element.sync();
        }
    }

    public void removeElements() {
        for(int i = 0; i < elementList.size(); i++) {
            Element element = elementList.get(i);
            element.removeElement();
        }
    }
    
    /**
     * Go to the next stage in the list
     */
    public void nextStage() {
        gameVar.nextStage();
    }
    
    /**
     * Go to the previous stage in the list
     */
    public void prevStage() {
        gameVar.prevStage();
    }
    
    public void setGameInstance(Display displayVar) {
        gameVar = displayVar;
    }
}

@@ -84,16 +84,86 @@
 Stage %7B%0A
+    //declare and initialize a new arraylist with the type of element%0A
     priv
@@ -238,37 +238,129 @@
     
-private Display gameVar;%0A    
+//declare a Display instance variable%0A    private Display gameVar;%0A    %0A    //declare a long variable for task scheduling
 %0A   
@@ -994,24 +994,78 @@
 s += intVar;
+ //add the specified number of ticks to the countdown 
 %0A        if(
@@ -1116,77 +1116,220 @@
 d();
-%0A    %7D%0A    %0A    public void decreaseTicks() %7B%0A        if(ticks %3E 0) %7B
+ //if for some reason they are both 0, just call the action method%0A    %7D%0A    %0A    public void decreaseTicks() %7B //called whenever the timer ticks%0A        if(ticks %3E 0) %7B //if the countdown is greater than 0, then
 %0A   
@@ -1345,16 +1345,30 @@
 ticks--;
+ //decrease it
 %0A       
@@ -1404,24 +1404,69 @@
 Performed();
+ //if it just turned 0, run the action method
 %0A        %7D%0A 
@@ -1619,32 +1619,63 @@
  : elementVar) %7B
+ //foreach element in the array
 %0A            if(
@@ -1712,16 +1712,57 @@
 ) %3C 0) %7B
+ //if that element does not already exist
 %0A       
@@ -1787,32 +1787,66 @@
 st.add(element);
+ //then add it to the element list
 %0A               
@@ -1875,24 +1875,62 @@
 ce(gameVar);
+ //sest it's display instance variable
 %0A           
@@ -1962,23 +1962,58 @@
 t();
-%0A       
+ //create it to avoid null pointer exceptions%0A
     
- %7D%0A
     
@@ -2022,39 +2022,25 @@
   %7D%0A        
-syncElements();
+%7D
 %0A    %7D%0A    %0A
@@ -2195,32 +2195,33 @@
  : elementVar) %7B
+ 
 %0A            ele
@@ -2249,79 +2249,146 @@
 nt);
-%0A            element.removeElement();%0A        %7D%0A        syncElements();
+ //remove that element if it exists in the list%0A            element.removeElement(); //invoke the element's removeElement method%0A        %7D
 %0A   
@@ -2523,32 +2523,78 @@
 mentList.get(i);
+ //get the current element in the element list
 %0A            ele
@@ -2605,16 +2605,42 @@
 .sync();
+ //invoke it's sync method
 %0A       
@@ -2824,26 +2824,131 @@
 ement();
-%0A        %7D
+ //invoke it's remove method!%0A        %7D%0A        elementList.clear(); //remove all the elements from the elementList
 %0A    %7D%0A 
@@ -3057,32 +3057,117 @@
 Var.nextStage();
+ //move onto the next stage by invoking the nextstage method from the displayinstance
 %0A    %7D%0A    %0A    
@@ -3280,16 +3280,72 @@
 Stage();
+ //invoke the prevstage method from the display instance
 %0A    %7D%0A 
@@ -3427,21 +3427,75 @@
  displayVar;
+ //set the display instance to the specified parameter
 %0A    %7D%0A%7D%0A

comment code for stage and sync al elements at once