import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

// What's Next, Captain?
// Global Game Jam 2015

import processing.opengl.*;
import pbox2d.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.common.*;
import org.jbox2d.dynamics.*;
import krister.Ess.*;


// Constants
int PROGRAM_STATE_TITLE_SCREEN = 0;
int PROGRAM_STATE_GAME = 1;
int SCENE_INTRODUCTION = 0;
int SCENE_TBC = 1;
float GRAVITY_DEFAULT = 0.6;
static float WS = 6; // WORLD_SCALE

// Program Properties
boolean keyDown_UP,keyDown_DOWN,keyDown_LEFT,keyDown_RIGHT;
int programState;
int pmillis=0; // previous millis()
float deltaTime=0; // in SECONDS, time between this frame and the last
// Game Properties
boolean isPaused;
float groundY;
Scene currentScene;
// World Properties
PBox2D box2d;

// Program Objects!
Minim minim;
AudioPlayer music;
Camera camera;
MainMenu mainMenu;
SequenceController sequenceController;
Poll poll;
// Characters!
Player player;
Beep beep;
Boop boop;
// Environment Objects
ArrayList boundaries;
ArrayList particles;
ArrayList doors;


void setup() {
  size(1024,768, OPENGL);
  colorMode(HSB);
  frameRate(40);
  rectMode(CENTER);
  
  programState = PROGRAM_STATE_TITLE_SCREEN;

  //Box2D stuff
  box2d = new PBox2D(this);
  box2d.createWorld();
  box2d.setGravity(0, 0);
  
  // Audio
//  Ess.start(this);
//  music = new AudioChannel(sketchPath+"/data/sounds/backgroundMusic.mp3");
//  music = new AudioChannel("data/backgroundMusic.mp3");
//  music.play();

  minim = new Minim(this);
  //music = minim.loadFile(sketchPath+"/data/sounds/backgroundMusic.wav");
  music = minim.loadFile("sounds/backgroundMusic.mp3");
  
  loadImages();
  
  mainMenu = new MainMenu();
  
  //loadScene(SCENE_TBC);
  //loadScene(SCENE_INTRODUCTION);
}


void draw() {
  // deltaTime!
  deltaTime = (millis() - pmillis) / 1000.0;
  
  // MAIN MENU
  if (programState == PROGRAM_STATE_TITLE_SCREEN) {
    mainMenu.draw();
    camera();
  }
  // GAAAAAAMMMEEEEE!!
  else if (programState == PROGRAM_STATE_GAME) {
    sequenceController.update();
    currentScene.draw();
    camera.draw();
  }
  
  
  pmillis = millis();
}






void mousePressed() {
  
  if (programState == PROGRAM_STATE_GAME) {
    if (isPaused) return;
    // If any of the speech bubbles haven't finished being read, then just fast-forward their text
    if (!beep.speechBubble.hasTextBeenRead()) beep.speechBubble.fastForwardText();
    else if (!boop.speechBubble.hasTextBeenRead()) beep.speechBubble.fastForwardText();
    else if (!player.speechBubble.hasTextBeenRead()) player.speechBubble.fastForwardText();
    else {
      sequenceController.alert("mousePressed");
    }
  //  for(int i=0;i<sequenceController.current.length;i++) {
  //    if(sequenceController.current[i].nextSet) {
  //      sequenceController.start(sequenceController.current[i].next);
  //    }
  //  }
  }
  
}

public void stop() { 
  Ess.stop(); 
  super.stop(); 
} 
 

void keyPressed() {
  if (keyCode==UP || keyCode=='w' || keyCode=='W') { keyDown_UP = true; }
  if (keyCode==DOWN || keyCode=='S' || keyCode==DOWN) { keyDown_DOWN = true; }
  if (keyCode==LEFT || keyCode=='A' || keyCode==LEFT) { keyDown_LEFT = true; }
  if (keyCode==RIGHT || keyCode=='D' || keyCode==RIGHT) { keyDown_RIGHT = true; }
  if (keyCode==ENTER) { // Enter to reset
    loadScene(SCENE_INTRODUCTION);
  }
  
  if (key == '1') sequenceController.alert("playerChoice", "optionA");
  else if (key == '2') sequenceController.alert("playerChoice", "optionB");
  else if (key == '3') sequenceController.alert("playerChoice", "optionC");
  else if (key == '4') sequenceController.alert("playerChoice", "optionD");
  
  if (key==27) {
    key = 0;
    isPaused = !isPaused;
  }
}

void keyReleased() {
  //if (key == ' ') { keyDown_SPACE = false; }
  if (keyCode==UP || keyCode=='W' || keyCode=='w') { keyDown_UP = false; }
  if (keyCode==DOWN || keyCode=='S' || keyCode=='s') { keyDown_DOWN = false; }
  if (keyCode==LEFT || keyCode=='A' || keyCode=='a') { keyDown_LEFT = false; }
  if (keyCode==RIGHT || keyCode=='D' || keyCode=='d') { keyDown_RIGHT = false; }
}







