import processing.core.*; 
import processing.data.*; 
import processing.event.*; 
import processing.opengl.*; 

import java.util.HashMap; 
import java.util.ArrayList; 
import java.io.File; 
import java.io.BufferedReader; 
import java.io.PrintWriter; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.io.IOException; 

public class sketch_160502a extends PApplet {

PImage map;
PFont f;
JSONArray data;
JSONArray currdata;
float xoffset;
float yoffset;

float ax = -100;
float ay = -100;
int aradius;

float bx = -100;
float by = -100;
int bradius;

int rbix = 850; //radius button offset, but im not tryna write that out a million times
int rbiy = 75;
int rbiy2 = 200;
int rbs = 50;

int tix = 850; //toggle intersection x
int tiy = 300;
int tis = 30;
boolean intersect = false;

int dix = 850;
int diy = 375;
int dis = 30;
boolean morning = true;
boolean afternoon = true;
boolean night = true;

int rix = 850;
int riy = 550;
int ris = 30;
boolean resolved = true;
boolean unresolved = true;

public void drawRadiusButtons(){
  textFont(f, 36);
  fill(200, 200, 200);
  strokeWeight(2);
  text("Radius A", rbix + 50, rbiy-25);
  stroke(75,75,75);
  rect(rbix, rbiy, rbs, rbs);
  rect(rbix + 2*rbs, rbiy, rbs, rbs);
  rect(rbix+4*rbs, rbiy, rbs, rbs);
  
  fill(255, 0, 0);
  ellipse(rbix + rbs/2, rbiy + rbs/2, 10, 10);
  ellipse(rbix+rbs/2+2*rbs, rbiy + rbs/2, 20, 20);
  ellipse(rbix+rbs/2+4*rbs, rbiy + rbs/2, 35, 35);

  fill(200, 200, 200);
  text("Radius B", rbix + 50, rbiy2-25);
  stroke(75,75,75);
  rect(rbix, rbiy2, rbs, rbs);
  rect(rbix + 2*rbs, rbiy2, rbs, rbs);
  rect(rbix+4*rbs, rbiy2, rbs, rbs);
  
  fill(0, 0, 255);
  ellipse(rbix + rbs/2, rbiy2 + rbs/2, 10, 10);
  ellipse(rbix+rbs/2+2*rbs, rbiy2 + rbs/2, 20, 20);
  ellipse(rbix+rbs/2+4*rbs, rbiy2 + rbs/2, 35, 35);
}

public void drawToggleRadius(){
  noFill();
  if(intersect)fill(200,200,200);
  strokeWeight(4);
  rect(tix, tiy, tis, tis);
  textFont(f, 18);
  fill(200,200,200);
  text("Show Intersection Only", tix+tis+10, tiy+25);
}

public void drawToggleMorning(){
  noFill();
  stroke(255, 127, 80);
  if(morning)fill(127,63,40);
  strokeWeight(4);
  rect(dix, diy, dis, dis);
  textFont(f, 18);
  fill(200,200,200);
  text("Show Morning", dix+dis+10, diy+25);
}
public void drawToggleAfternoon(){
  noFill();
  stroke(255, 215, 0);
  if(afternoon)fill(127, 107, 0);
  strokeWeight(4);
  rect(dix, diy+50, dis, dis);
  textFont(f, 18);
  fill(200,200,200);
  text("Show Afternoon", dix+dis+10, diy+75);
}
public void drawToggleNight(){
  noFill();
  stroke(25, 25, 112);
  if(night)fill(12, 12, 66);
  strokeWeight(4);
  rect(dix, diy+100, dis, dis);
  textFont(f, 18);
  fill(200,200,200);
  text("Show Night", dix+dis+10, diy+125);
}

public void drawToggleResolved(){
  noFill();
  stroke(0,0,255);
  if(resolved)fill(0,0,127);
  strokeWeight(4);
  rect(rix, riy, ris, ris);
  textFont(f, 18);
  fill(200,200,200);
  text("Show Resolved", rix+ris+10, riy+25);
  
  noFill();
  stroke(255, 0,0);
  if(unresolved)fill(127,0,0);
  strokeWeight(4);
  rect(rix, riy+50, ris, ris);
  textFont(f, 18);
  fill(200,200,200);
  text("Show Unresolved", rix+ris+10, riy+75);
}

public void drawButtons(){
  drawRadiusButtons();
  drawToggleRadius();
  drawToggleMorning();
  drawToggleAfternoon();
  drawToggleNight();
  drawToggleResolved();
}

public void setup(){
  
  f = createFont("Arial",16,true);
  textFont(f, 18);
  xoffset = -122.433701f;
  yoffset = 37.767683f;
  map = loadImage("map.png");
  data = loadJSONArray("houses.json");
  background(0);
  aradius = 100;
  bradius = 100;
  update();
  //drawButtons();
}

public boolean inA(int x, int y){
  int dx = x-(int)ax;
  int dy = y-(int)ay;
  int dr = aradius/2;
  return (dx*dx + dy*dy < dr*dr);
}
public boolean inB(int x, int y){
  int dx = x-(int)bx;
  int dy = y-(int)by;
  int dr = bradius/2;
  return (dx*dx + dy*dy < dr*dr);
}
public boolean inBoth(int x, int y){
  return (inA(x, y) && inB(x, y));
}

public boolean wrongTimeofDay(int t){
  if(t >= 2 && t < 10 && !morning)return true;
  if(t >= 10 && t < 18 && !afternoon)return true;
  if(t < 2 || t >= 18 && ! night)return true;
  return false;
}
public boolean wrongResolution(String s){
  if(s.equals("NONE")){
    if(!unresolved)return true;
  }
  else{
    if(!resolved)return true;
  }
  return false;
}
public void drawRadii(){
    fill(255, 0, 0);
    stroke(255, 0, 0);
    ellipse(ax, ay, 8, 8);
    noFill();
    ellipse(ax, ay, aradius, aradius);
    
    fill(0, 0, 255);
    stroke(0, 0, 255);
    ellipse(bx, by, 8, 8);
    noFill();
    ellipse(bx, by, bradius, bradius);
}

public void update(){
  background(0);
  image(map, 0, 0, 830, 606);
  fill(0, 255, 0);
  noStroke();
  strokeWeight(2);
  for (int i = 0; i < data.size(); i++){
    fill(255, 255, 0);
    JSONObject curr = data.getJSONObject(i);
    JSONArray loc = curr.getJSONArray("Location");
    float y = loc.getInt(1);
    float x = loc.getInt(0);
  /*  if (inA(x, y))fill(255,0,0);
    else if (inB(x, y))fill(0, 0, 255);
    if (inBoth(x, y))fill(255,0,255);
    else{
      if(intersect)noFill();
    }*/
    fill(255, 0, 255);
    ellipse(x, y, 6, 6);
  }
    drawRadii();
   // drawButtons();
}
public void mouseLeft(int x, int y){
  ax = x;
  ay = y;
  print(x);
  print(y);
  update();
  //667, 334
}

public void mouseRight(int x, int y){
  bx = x;
  by = y;
  update();
}
public void radiusMenuButtons(int x, int y){
  if(y > rbiy && y < rbiy + rbs){
    if(x > rbix && x < rbix +rbs)
        aradius = 100;
    if(x > rbix+rbs*2 && x < rbix+rbs*3)
        aradius = 200;
    if(x > rbix+rbs*4 && x < rbix+rbs*5)
        aradius = 400;
    update();
  }
  else if(y > rbiy2 && y < rbiy2 + rbs){
    if(x > rbix && x < rbix +rbs)
        bradius = 100;
    if(x > rbix+rbs*2 && x < rbix+rbs*3)
        bradius = 200;
    if(x > rbix+rbs*4 && x < rbix+rbs*5)
        bradius = 400;
    update();
  }
}

public void intersectionMenu(int x, int y){
  if(x > tix && x < tix + tis && y > tiy && y < tiy + tis)
    if(intersect)intersect = false;
    else intersect = true;
    update();
}
public void daytimeMenu(int x, int y){
  if(x > dix && x < dix + dis && y > diy && y < diy + dis)
    if(morning)morning = false;
    else morning = true;
    update();
    
  if(x > dix && x < dix + dis && y > diy+50 && y < diy + dis+50)
    if(afternoon)afternoon = false;
    else afternoon = true;
    update();
    
  if(x > dix && x < dix + dis && y > diy+100 && y < diy + dis+100)
    if(night)night = false;
    else night = true;
    update();
}

public void resolutionMenu(int x, int y){
  if(x > rix && x < rix + ris && y > riy && y < riy + ris)
    if(resolved)resolved = false;
    else resolved = true;
    update();
    
  if(x > rix && x < rix + ris && y > riy+50 && y < riy + ris+50)
    if(unresolved)unresolved = false;
    else unresolved = true;
    update();  
}

/*
void menuButtons(int x, int y){
  radiusMenuButtons(x,y);
  intersectionMenu(x,y);
  daytimeMenu(x,y);
  resolutionMenu(x,y);
}
*/
/*void mouseClicked(){
  if(mouseX > 0 && mouseY > 0 && mouseY < 750){
     if(mouseX < 750){
       if (mouseButton == LEFT)mouseLeft(mouseX, mouseY);
       else if (mouseButton == RIGHT)mouseRight(mouseX, mouseY);
     }
     else if (mouseX < 1240){
       menuButtons(mouseX, mouseY);
     }
   }
 }*/
public void draw(){    
  if (keyPressed){
    if (key == 'p');//showColor();
  }
}
  public void settings() {  size(1240, 750); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "--present", "--window-color=#666666", "--stop-color=#cccccc", "sketch_160502a" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
