/**
 * Adapted from oscP5parsing by andreas schlegel
 * example shows how to parse incoming osc messages "by hand".
 * it is recommended to take a look at oscP5plug for an
 * alternative and more convenient way to parse messages.
 * oscP5 website at http://www.sojamo.de/oscP5
 */
import oscP5.*;
import netP5.*;
//-------------- Global Variables defined at top --------------------
int firstValue = 0;
int secondValue = 0;
String thirdValue;

OscP5 oscP5;                 //-- Import/Create software object "OscP5"
NetAddress myRemoteLocation; //-- Create variable of type "NetAddress"

void setup() {
  size(400,400); //-- Create canvas 400 x 400 pixels in size
  frameRate(25);
  /* start oscP5, listening for incoming messages at port 8800 */
  oscP5 = new OscP5(this, 8800);  //--Create instance of OscP5 object
  
  /* myRemoteLocation is a NetAddress. a NetAddress takes 2 parameters,
   * an ip address and a port number. myRemoteLocation is used as parameter in
   * oscP5.send() when sending osc packets to another computer, device, 
   * application. usage see below. for testing purposes the listening port
   * and the port of the remote location address are the same, hence you will
   * send messages back to this sketch.
   */
  myRemoteLocation = new NetAddress("127.0.0.1", 8800);
}

void draw() {
  if(mouseX < 200) {
    thirdValue = "Left handed.";
  } else {
    thirdValue = "Right handed.";   
  }
  background(firstValue, secondValue, 50);   //-- color background with R, G, B
}


void mousePressed() {
  /* create a new osc message object */
  OscMessage myMessage = new OscMessage("/test");
  
  myMessage.add(mouseX); /* add an int to the osc message */
  myMessage.add(mouseY); /* add a float to the osc message */
  myMessage.add(thirdValue); /* add a string to the osc message */

  /* send the message */
  oscP5.send(myMessage, myRemoteLocation); 
}

void oscEvent(OscMessage theOscMessage) {
  /* check if theOscMessage has the address pattern we are looking for. */
  
  if(theOscMessage.checkAddrPattern("/test")==true) {
    /* check if the typetag is the right one. */
    if(theOscMessage.checkTypetag("iis")) {
      /* parse theOscMessage and extract the values from the osc message arguments. */
      firstValue = theOscMessage.get(0).intValue();  
      secondValue = theOscMessage.get(1).intValue();
      thirdValue = theOscMessage.get(2).stringValue();  
      print("### received an osc message /test with typetag iis.");
      println(" values: "+firstValue+", "+secondValue+", "+ thirdValue);
      return;
    }  
  } 
  println("### received an osc message. with address pattern "+theOscMessage.addrPattern());
  println(" typetag: "+theOscMessage.typetag());
}
