import oscP5.*;
import netP5.*;

String lastInput = new String();
String currentInput = new String();

public int Port1 = 10000;
public int Port2 = 11000;
public int Port3 = 12000;
public String OutIP = ("127.0.0.1");
public int InPort = 7400;

public float Clench = 0;
public float Smile = 0;
public float Blink = 0;
public float WinkL = 0;
public float WinkR = 0;
public float Eyebrow = 0;

public float MoveUp = 0;
public float MoveDown = 0;
public float MoveLeft = 0;
public float MoveRight = 0;
public float RotateLeft = 0;
public float RotateRight = 0;

public float Engagement = 0;
public float Frustration = 0;
public float Excitement = 0;
public float Meditation = 0;
public float LTExcitement =0;

int FP = 20;   // Font Position
int FS = 12;   // Font Size
int FF = 150;  // Font Fill Color
int GP = 97;   // Graph Position
int TP = 20;   // Title Position
int TFR = 150; // Title Fill Color RED
int TFG = 150; // Title Fill Color GREEN
int TFB = 0;   // Title Fill Color BLUE
int IFR = 200; // Information Fill Color RED
int IFG = 20; // Information Fill Color GREEN
int IFB = 0;   // Inforation Fill Color BLUE

 int OSCSend = 0;
PFont fontA;
PImage img;

OscP5 oscP5;


void setup() {
  size(480, 600);
  frameRate(30);
  smooth();
  
  oscP5 = new OscP5(this, InPort);

  
  fontA = loadFont("HelveticaNeueLTCom-Th.vlw");
  textFont(fontA, FS);
  
  img = loadImage("l4d.png");
 println("Sending to: "+OutIP);
}

void draw() {
  background(0);
  
 //fill(0);
//text(lastInput, width/2, height/2);
fill(255, 0, 0);
text(currentInput, width/4, height*.90);
  
 image(img, 380, 540, img.width/6, img.height/6);
/* 
  if (OSCSend ==1){
  fill(color(22, 255, 10));
  rect(45, 480, 35, 35);
  delay(20);
 OSCSend = 0;
 
  }
  */
  // draw EXP graph ticks
  int i;
  for (i = 1; i <= 11; i++) {
    stroke(map(i, 1, 11, 0, 255));
    float tickX = map(i, 1, 11, 60, 420);
    line(tickX, 40, tickX, 170);
  }
  //draw COG graph ticks   
  int b;
  for (b = 1; b <= 11; b++) {
    stroke(map(b, 1, 11, 0, 255));
    float tickX = map(b, 1, 11, 60, 420);
    line(tickX, 200, tickX, 330);
  }
  //draw AFF graph ticks
  int c;
  for (c = 1; c <= 11; c++) {
    stroke(map(c, 1, 11, 0, 255));
    float tickX = map(c, 1, 11, 60, 420);
    line(tickX, 360, tickX, 470);
  }
 
  noStroke();
  
  //Draw Titles
  fill(TFR, TFG, TFB);
  text("Expressive", TP, 30);
  fill(TFR, TFG, TFB);
  text("Cognitive", TP, 190);
  fill(TFR, TFG, TFB);
  text("Affective", TP, 350);
  
  
  // Draw Information
  fill(IFR, IFG, IFB);
  text("Receiving local on port "+InPort, 20, 560);
  fill(IFR, IFG, IFB);
  text("Sending to " +OutIP+" on ports "+Port1+" - "+Port2+" - "+Port3, 20, 580);
  
  // draw EXP bar graph
  fill(FF);
  text("Clench", FP, 60);  
  drawEXPBarGraph(Clench, 50);
  fill(FF);
  text("Smile", FP, 80);
  drawEXPBarGraph(Smile, 70);
  fill(FF);
  text("Blink", FP, 100);
  drawEXPBarGraph(Blink, 90);
  fill(FF);
  text("Wink Left", FP, 120);
  drawEXPBarGraph(WinkL, 110);
  fill(FF);
  text("Wink Right", FP, 140);
  drawEXPBarGraph(WinkR, 130);
  fill(FF);
  text("Raise Eyebrow", FP, 160);
  drawEXPBarGraph(Eyebrow, 150);
  
  // draw COG bar graph
  fill(FF);
  text("Move Up", FP, 210);  
  drawEXPBarGraph(MoveUp, 200);
  fill(FF);
  text("Move Down", FP, 230);
  drawEXPBarGraph(MoveDown, 220);
  fill(FF);
  text("Move Left", FP, 250);
  drawEXPBarGraph(MoveLeft, 240);
  fill(FF);
  text("Move Right", FP, 270);
  drawEXPBarGraph(MoveRight, 260);
  fill(FF);
  text("Rotate Left", FP, 290);
  drawEXPBarGraph(RotateLeft, 280);
  fill(FF);
  text("Rotate Right", FP, 310);
  drawEXPBarGraph(RotateRight, 300);
  
  // draw AFF bar graph
  fill(FF);
  text("Engagement", FP, 380);  
  drawEXPBarGraph(Engagement, 370);
  fill(FF);
  text("Frustration", FP, 400);
  drawEXPBarGraph(Frustration, 390);
  fill(FF);
  text("Excitement", FP, 420);
  drawEXPBarGraph(Excitement, 410);
  fill(FF);
  text("Meditation", FP, 440);
  drawEXPBarGraph(Meditation, 430);
  fill(FF);
  text("LT Excitement", FP, 460);
  drawEXPBarGraph(LTExcitement, 450);
  
}

//Input IP

void keyPressed()
{
if(key == ENTER)
{
  OutIP = currentInput;
  lastInput = currentInput = currentInput + key;
  currentInput = "";
  
}
else if(key == BACKSPACE && currentInput.length() > 0)
{
  currentInput = currentInput.substring(0, currentInput.length() - 1);
}
else if(key == CODED) {
 // ignore Special Keys...
}
else
{
  currentInput = currentInput + key;
}

}


// Draw Bar Graph 

void drawEXPBarGraph(float expVal, int barY) {
  if(expVal >= 0.5) {
    fill(color(22, 255, 113));
  } else {
    fill(color(255, 0, 0));
  }
  float len = map(expVal, 0.0, 1.0, 0, 323);
  rect(GP, barY, len, 10);
  
  
}

void drawOSCSend() {
  
}

void oscEvent(OscMessage theOscMessage) {
  
int OSCSend = 1;
  
  // EXPRESSIVE OSC ROUTER
  
  if(theOscMessage.checkAddrPattern("/EXP/CLENCH") == true) {
    NetAddress P1 = new NetAddress(OutIP,Port1);
  Clench = theOscMessage.get(0).floatValue();
    OscMessage ClenchMessage = new OscMessage("/EXP/Clench");
    ClenchMessage.add(Clench);
    oscP5.send(ClenchMessage, P1);
  }
  
  if(theOscMessage.checkAddrPattern("/EXP/SMILE") == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  Smile = theOscMessage.get(0).floatValue();
    OscMessage SmileMessage = new OscMessage("/EXP/Smile");
    SmileMessage.add(Smile);
    oscP5.send(SmileMessage, P1); 
  }
  
  if(theOscMessage.checkAddrPattern("/EXP/BLINK") == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  Blink = theOscMessage.get(0).floatValue();
    OscMessage BlinkMessage = new OscMessage("/EXP/Blink");
    BlinkMessage.add(Blink);
    oscP5.send(BlinkMessage, P1);
  }
  
  if(theOscMessage.checkAddrPattern("/EXP/WINK_LEFT") == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  WinkL = theOscMessage.get(0).floatValue();
    OscMessage WinkLMessage = new OscMessage("/EXP/Wink_Left");
    WinkLMessage.add(WinkL);
    oscP5.send(WinkLMessage, P1);
  }
      
  if(theOscMessage.checkAddrPattern("/EXP/WINK_RIGHT") == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  WinkR = theOscMessage.get(0).floatValue();
    OscMessage WinkRMessage = new OscMessage("/EXP/Wink_Right");
    WinkRMessage.add(WinkR);
    oscP5.send(WinkRMessage, P1);
  }
  
  if(theOscMessage.checkAddrPattern("/EXP/EYEBROW") == true) {
     NetAddress P1 = new NetAddress(OutIP,Port1);
  Eyebrow = theOscMessage.get(0).floatValue();
    OscMessage EyebrowMessage = new OscMessage("/EXP/Eyebrow");
    EyebrowMessage.add(Eyebrow);
    oscP5.send(EyebrowMessage, P1);
  }
  
  
  // COGNITIVE ROUTER
  if(theOscMessage.checkAddrPattern("/COG/LIFT") == true) {
    NetAddress P2 = new NetAddress(OutIP, Port2);
  MoveUp = theOscMessage.get(0).floatValue();
    OscMessage MoveUpMessage = new OscMessage("/COG/Lift");
    MoveUpMessage.add(MoveUp);
    oscP5.send(MoveUpMessage, P2);
  }
  
  if(theOscMessage.checkAddrPattern("/COG/DROP") == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  MoveDown = theOscMessage.get(0).floatValue();
    OscMessage MoveDownMessage = new OscMessage("/COG/Drop");
    MoveDownMessage.add(MoveDown);
    oscP5.send(MoveDownMessage, P2);
  }
  
  if(theOscMessage.checkAddrPattern("/COG/LEFT") == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  MoveLeft = theOscMessage.get(0).floatValue();
    OscMessage MoveLeftMessage = new OscMessage("/COG/Left");
    MoveLeftMessage.add(MoveLeft);
    oscP5.send(MoveLeftMessage, P2);
  }
  
  if(theOscMessage.checkAddrPattern("/COG/RIGHT") == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  MoveRight = theOscMessage.get(0).floatValue();
    OscMessage MoveRightMessage = new OscMessage("/COG/Right");
    MoveRightMessage.add(MoveRight);
    oscP5.send(MoveRightMessage, P2);
  }
  
  if(theOscMessage.checkAddrPattern("/COG/ROTATE_LEFT") == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  RotateLeft = theOscMessage.get(0).floatValue();
    OscMessage RotateLeftMessage = new OscMessage("/COG/Rotate_Left");
    RotateLeftMessage.add(RotateLeft);
    oscP5.send(RotateLeftMessage, P2);
  }
   
  if(theOscMessage.checkAddrPattern("/COG/ROTATE_RIGHT") == true) {
    NetAddress P2 = new NetAddress(OutIP,Port2);
  RotateRight = theOscMessage.get(0).floatValue();
    OscMessage RotateRightMessage = new OscMessage("/COG/Rotate_Right");
    RotateRightMessage.add(RotateRight);
    oscP5.send(RotateRightMessage, P2);
  }
  
  // AFFECTIVE ROUTER
  
  if(theOscMessage.checkAddrPattern("/AFF/Engaged/Bored") == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  Engagement = theOscMessage.get(0).floatValue();
    OscMessage EngagementMessage = new OscMessage("/AFF/Engaged/Bored");
    EngagementMessage.add(Engagement);
    oscP5.send(EngagementMessage, P3);
  }
  
  if(theOscMessage.checkAddrPattern("/AFF/Frustration") == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  Frustration = theOscMessage.get(0).floatValue();
    OscMessage FrustrationMessage = new OscMessage("/AFF/Frustration");
    FrustrationMessage.add(Frustration);
    oscP5.send(FrustrationMessage, P3);
  }
  
  if(theOscMessage.checkAddrPattern("/AFF/Excitement") == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  Excitement = theOscMessage.get(0).floatValue();
    OscMessage ExcitementMessage = new OscMessage("/AFF/Excitement");
    ExcitementMessage.add(Excitement);
    oscP5.send(ExcitementMessage, P3);
  }
  
  if(theOscMessage.checkAddrPattern("/AFF/Meditation") == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  Meditation = theOscMessage.get(0).floatValue();
    OscMessage MeditationMessage = new OscMessage("/AFF/Meditation");
    MeditationMessage.add(Meditation);
    oscP5.send(MeditationMessage, P3);
  }
  
  if(theOscMessage.checkAddrPattern("/AFF/Excitement Long Term") == true) {
    NetAddress P3 = new NetAddress(OutIP,Port3);
  LTExcitement = theOscMessage.get(0).floatValue();
    OscMessage LTExcitementMessage = new OscMessage("/AFF/Excitement Long Term");
    LTExcitementMessage.add(LTExcitement);
    oscP5.send(LTExcitementMessage, P3);
  }
  
  
  
 } // oscEvent Function End

