/**
 * <p>MyGUI a collection of compatable user inteface objects designed to work with processing.</p>
 * <p>Version: 0.01<br/>
 * Created by John Beech (processing@mkv25.net)<br/>
 * Website: http://mkv25.net/</p>
 * <p>Current features include:
 * <ul>
 * <li>Collection of compatable objects</li>
 * <li>Track focus of single object in collection</li>
 * <li>Peform hit-tests on all objects and call their methods as necessary</li>
 * <li>Simple Buttons, Plain, Text, Image</li>
 * <li>Slider object</li>
 * <li>Style class for global colour/font</li>
 * <li>Checkbox Button</li>
 * <li>Radio Button and Radio Group</li>
 * </ul></p>
 */

public class MyGUI {
  // core variables
  private PApplet _parent;
  private MyGUIObject[] elements;
  private MyGUIStyle style;
  private int focusIndex = -1;
  private int numElements = 0;
  private boolean customMouse = false;
  private int tmouseX = 0, tmouseY = 0;

  // default constructor
  public MyGUI(PApplet _parent) {
    this._parent = _parent;
    elements = new MyGUIObject[10];
    _parent.registerMouseEvent(this);
    _parent.registerDraw(this);
    _parent.registerKeyEvent(this);
    style = new MyGUIStyle();    
  }
  // constructor where you can specify the size of the element array
  public MyGUI(PApplet _parent, int size) {
    this._parent = _parent;
    elements = new MyGUIObject[size];
    _parent.registerMouseEvent(this);
    _parent.registerDraw(this);
    _parent.registerKeyEvent(this);
    style = new MyGUIStyle();
  }
  // constructor where you can specify the size of the element array and set the style
  public MyGUI(PApplet _parent, int size, MyGUIStyle style) {
    this._parent = _parent;
    elements = new MyGUIObject[size];
    _parent.registerMouseEvent(this);
    _parent.registerDraw(this);
    _parent.registerKeyEvent(this);
    this.style = style;
  }
  // cross breed constructor where you can set the style, but the element array stays at the default
  public MyGUI(PApplet _parent, MyGUIStyle style) {
    this._parent = _parent;
    elements = new MyGUIObject[10];
    _parent.registerMouseEvent(this);
    _parent.registerDraw(this);
    _parent.registerKeyEvent(this);
    this.style = style;
  }

  /* Methods which affect the properties of the collection */
  // this flavour of add puts new element additions at the end of the elements array
  // returns the index of the new element, or null if there is no more space in array
  public int add(MyGUIObject newElement) {
    // See if there are any null entries to replace
    for(int i=0; i<numElements; i++) {
      if(elements[i] == null) {
        elements[i] = newElement;
        elements[i].setParent(this);
        elements[i].setID(i);
        return i;
      }
    }
    // If there were no null elements, then add to the end (providing there is space)
    if(numElements < elements.length) {
      // Assign new element to array and increment element counter
      int elementID = numElements;
      numElements++;
      elements[elementID] = newElement;
      elements[elementID].setParent(this);
      elements[elementID].setID(elementID);
      return elementID;
    }
    return -1;
  }  

  public boolean remove(int id) {
    if(id > 0 && id <= numElements) {
      elements[id] = null;
      return true;
    }
    return false;
  }

  // this function sets the style after initialisation
  public void setStyle(MyGUIStyle style) {
    this.style = style;
  }
  
  // this function tells MyGUI that you want to use custom mouse coordinates
  public void useCustomMouse(boolean mode) {
    customMouse = mode;
  }
  
  // this function lets you specify custom mouse coordinates
  public void setMouseCoords(int x, int y) {
    tmouseX = x;
    tmouseY = y;
  }

  /* Methods which return values about the collection */
  // get by focusIndex - currently focused element
  // will return null if there is no focus index
  public MyGUIObject getFocused() {
    if(focusIndex != -1) {
      return elements[focusIndex];
    }
    return null;
  }
  // return the focusIndex
  public int getFocusIndex() {
    return focusIndex;
  }

  // get by specified index
  public MyGUIObject get(int index) {
    if(index < numElements && index > 0) {
      return elements[focusIndex];
    }
    return null;
  }

  // is empty?
  public boolean isEmpty() {
    if(numElements == 0) {
      return true;
    }
    return false;
  }

  // is full? check if there are no null spaces
  public boolean isFull() {
    // quick check, space at end of array?
    if(numElements != elements.length) {
      return false;
    }
    // longer check for null spaces
    for(int i=0; i<numElements; i++) {
      if(elements[i] == null) return false;
    }
    // If numElements == elements.length and there are no null entries:
    return true;
  }  

  // returns the currently assigned style, presumably if you want to alter an already set style
  public MyGUIStyle getStyle() {
    return style;
  }
  
  /* Methods for handling mouse events */
  // track mouse events   
  public void mouseEvent(MouseEvent e) {
    // Re Call mouse events based on type
    if(e.getID() == MouseEvent.MOUSE_PRESSED) mousePressed(e);
    else
    if(e.getID() == MouseEvent.MOUSE_RELEASED) mouseReleased(e);
    else
    if(e.getID() == MouseEvent.MOUSE_DRAGGED) mouseDragged(e);
  }
  
  // Note when this method is called an element in MyGUI is given focus
  // Focus is necessary for a GUI element to perform actions. Elements with higher
  // indexes will get focus priority over lower indexes. Higher index elements
  // are also drawn last and so will appear ontop of other objects.
  // The focus needs to be decided prior to passing mousePressed as mousePressed may rely
  // on the state of focusIndex before it can work properly.
  public void mousePressed(MouseEvent e) {
    // clear focus (will be set again below)
    focusIndex = -1;
    // loop through collection of objects and pass on call to mousePressed
    for(int i=numElements-1; i>=0; i--) {
      // keep track of which element has focus
      if(elements[i].checkForHit()) {
        // match found
        focusIndex = i;
        // kill loop
        i = -1;
      }
    }
    // loop again after focusIndex has been decided
    for(int i=0; i<numElements; i++) {
      // pass on mousePressed command
      elements[i].mousePressed();
    }
  }

  public void mouseReleased(MouseEvent e) {
    // Loop through collection of objects and pass on call to mouseReleased
    for(int i=0; i<numElements; i++) {
      elements[i].mouseReleased();
    }
  }

  public void mouseDragged(MouseEvent e) {
    // Loop through collection of objects and pass on call to mouseDragged
    for(int i=0; i<numElements; i++) {
      elements[i].mouseDragged();
    }
  }

  /* Methods for handling key events */
  public void keyEvent(KeyEvent e) {
    
  }

  /* Draw */
  // Draw has been separated into two logical methods
  // void drawStates() will actually update all the draw methods of components
  // void draw() has a logic switch that prevents it from calling drawStates() if in
  // lightWeightMode. When in lightWeightMode, components will only be updated if 
  // a mouse, or key event is detected.
  public void draw() {
    // Update mouse coordinates if custom mouse coordinates are not provided
    if(!customMouse) {
      tmouseX = mouseX; tmouseY = mouseY;
    }
    // Loop through collection of objects and call their draw methods
    for(int i=0; i<numElements; i++) {
        if(i != focusIndex) elements[i].draw();
    }
    if(focusIndex >= 0 && focusIndex < numElements) {
      if(elements[focusIndex] != null) {
        elements[focusIndex].draw();
      }
    }
  }
}
