import processing.sound.*;

// Tarek
float checkDistance(Log log, Ship ship) {
  float testX = ship.pos.x;
  float testY = ship.pos.y;

  // which edge is closest?
  if (ship.pos.x < log.x)                    testX = log.x; 
  else if (ship.pos.x > log.x+log.logwidth)  testX = log.x+log.logwidth;
  if (ship.pos.y < log.y - log.logheight)                    testY = log.y - log.logheight;
  else if (ship.pos.y > log.y) testY = log.y;

  // get distant
  float distX = ship.pos.x-testX;
  float distY = ship.pos.y-testY;
  float distance = sqrt( (distX*distX) + (distY*distY) );

  // Debug Lines
  // line(testX, testY, ship.pos.x, ship.pos.y);

  // if the distance is less than the radius, collision!
  return distance;
}

// Main Game entry
// Chris, Julia, Marla, Tarek, Max
void play() {
  background(75, 105, 47);


  // Timer
  fill(0);
  textSize(24);
  text(playtime.second(), 40, 20);
  text(playtime.second(), -580, 20);

  // Tutorial
  textAlign(LEFT);
  textSize(16);
  text("You are a robot that has to save Bob. He is stuck in the forest.", -570, 160);
  text("Here is a chainsaw to get through", -570, 180);

  ship_zero.draw();
  saw_zero.drawSaw();
  ship_zero.collect(saw_zero);
  bob.drawBob();
  ship_zero.collect(bob);

  // draw wall
  for(int i = 0; i < walls.length; i++) {
    walls[i].drawLog(i);
  }
  // draw log
  for(int i = 0; i < logs.length; i++) {
    logs[i].drawLog(i);
  }
  // log normal stroke
  for(int i = 0; i < logs.length; i++) {
    logs[i].strokecolor = logs[i].logcolor;
  }
  // nearest log stroke (debug)
  logs[ship_zero.nextLog].strokecolor = color(255, 0, 0);
}

// -----


// global state
boolean isgame = false;
boolean ismenu = true;
boolean isend = false;
boolean isstats = false;
boolean ispause = false;
boolean start_slide = true;

// ----- Objectives
MainMenu main_menu = new MainMenu();
Stats stats_menu = new Stats();
EndMenu end_menu = new EndMenu();

Log[] logs; 

// player
Ship ship_zero;

// collectables
Saw saw_zero;
Bob bob;

// stopwatch
StopWatchTimer playtime = new StopWatchTimer();
Files savefile = new Files();


PImage player_sprite;
PImage player_sprite_bob;
PImage saw_sprite;
PImage bob_sprite;

SoundFile idle_motor;
SoundFile music;


void setup() {
  size(600, 600);
  textAlign(CENTER, CENTER);

  create_level(30);
  println(logs[1]);

  ship_zero = new Ship();
  saw_zero = new Saw(-width+100, 100, 20, 50);
  bob = new Bob(500, 400, 20, 40);

  // Marla
  player_sprite = loadImage("./assets/Sprite-robi.png");
  player_sprite_bob = loadImage("./assets/Sprite-robiwithbob.png");
  saw_sprite = loadImage("./assets/Sprite-saege.png");
  bob_sprite = loadImage("./assets/Sprite-bob.png");

  idle_motor = new SoundFile(this, "./assets/idle_motor.mp3");
  music = new SoundFile(this, "./assets/main_music.wav");

  savefile.loadJson("./saves/save.json");

  music.loop(1, 0.5);
}

// Chris, Max
void draw() {
  // time debug
  // println(millis(), " start: ", playtime.startTime, " pause: ", playtime.pauseTime);

  // game state
  if (isgame) {


    // Go to second or first slide
    if(ship_zero.pos.x > 0) {
      start_slide = false;
    } else {
      start_slide = true;
    }

    // actually move the camera
    if(start_slide) {
      translate(600, 0);
    } else {
      translate(0, 0);
    }

    // Main Entry
    play();

    // Timer
    if (playtime.running) {} else playtime.start();
    if (playtime.pause) playtime.resume();

    // Win
    if(bob.is_attached && dist(ship_zero.pos.x, ship_zero.pos.y ,-400, 300) < 100) {
      isgame = false;
      isend = true;
    }
    fill(150, 0, 0, 50);
    ellipse(-400, 300, 200, 200);

  } else if (ismenu){
    // menu code
    // play();
    main_menu.drawMenu();
    if (playtime.running) playtime.stop();
  } else if (isstats) {
    stats_menu.draw();
  } else if (isend) {
    // end screen code
    end_menu.drawMenu();

    // get into right state
    ship_zero.health = 150;
    ship_zero.pos.x = -width/2;
    ship_zero.pos.y = height/2;
    bob.is_attached = false;
    saw_zero.is_attached = false;
    create_level(30);
    ship_zero.hasSaw = false;
    

    // save
    if(ship_zero.health > 0) if(playtime.second() < stats_menu.best_game_time) stats_menu.best_game_time = playtime.second();
    savefile.savetofile("save");
  } else if (ispause) {
    main_menu.drawMenu();

    if (playtime.running && playtime.pause == false) playtime.pause_start();
  }

  mouse_released = false;
}
