public class LoadLog extends PApplet{


  boolean redraw = true;


  int focus = 0;


  ArrayList allRecords ;

  color[] colors;

    

  String[] allDataX = new String[10000];

  String[] allDataY = new String[10000];

  String[] allDataZ = new String[10000];


  void setup(){


    //println("Start");

    allRecords =  loadRecordsWithDateAtBeginning("/Users/lucaslongo/Desktop/Default.log",28);

    size(640,480);

    noStroke();

    colors = new color[]  {

      color(255,0,0),color(0,255,0),color(0,0,255),color(255,0,255),color(255,255,0), color(127,127,127),color(127,0,0),color(0,127,0),color(0,0,127),color(127,0,127),color(127,127,0), color(0,127,127)            };

  }


  void keyPressed(){

    focus++;

    if (focus == 12) focus = 0;

    println("fieldShowing = " + focus);


  }


  void drawData(int xField, int yField, int zField){

    int xMax = 32;

    int xMin = -32;

    int yMax = 32;

    int yMin = -32;

    int zMax = 32;

    int zMin = -32;


    float xIncrement = float(width)/allRecords.size();


    for (int i = 0; i < allRecords.size(); i++){

      int[] thisRecord = (int[]) allRecords.get(i);

      if (thisRecord[xField] > xMax) xMax = thisRecord[xField];

      if (thisRecord[xField] < xMin) xMin = thisRecord[xField];

      if (thisRecord[xField] > yMax) yMax = thisRecord[yField];

      if (thisRecord[yField] < yMin) yMin = thisRecord[yField];

      if (thisRecord[zField] > zMax) zMax = thisRecord[zField];

      if (thisRecord[zField] < zMin) zMin = thisRecord[zField];

      //println(" xMin " + xMin + " xMax " + xMax +" yMin " + yMin +" xMax " + xMax);

    }


    for (int i = 0; i < allRecords.size(); i++){

      int[] thisRecord = (int[]) allRecords.get(i);

      float xposition = int(thisRecord[xField]);

      float yposition =  int(thisRecord[yField]);

      float zposition =  int(thisRecord[zField]);


      xposition = (xposition - xMin)/(xMax-xMin) * height;

      yposition = (yposition - yMin)/(yMax - yMin) * height;

      zposition = (zposition - zMin)/(zMax - zMin) * height;


      allDataX[i] = "allDataX[" + i + "] = " + xposition + ";";

      allDataY[i] = "allDataY[" + i + "] = " + yposition + ";";

      allDataZ[i] = "allDataZ[" + i + "] = " + zposition + ";";

      

      //ellipse(xposition,yposition,1,1);

      stroke(colors[xField]);

      fill(colors[xField]);

      point(i*xIncrement,xposition);


      stroke(colors[yField]);

      fill(colors[yField]);

      point(i*xIncrement,yposition);


      stroke(colors[zField]);

      fill(colors[zField]);

      point(i*xIncrement,zposition);

      

      //println(xposition + " " + yposition);

      //line(int(xposition),yposition, int(xposition), yposition2);

      //println(" fieldOfInterest " + fieldOfInterest + " i = " + i + " thisRecord       " + thisRecord[fieldOfInterest]);

    }

  }


  void draw(){

    background(0);

    drawData(3,4,5);

    saveStrings("/Users/lucaslongo/Desktop/allDataX.txt", allDataX);

    saveStrings("/Users/lucaslongo/Desktop/allDataY.txt", allDataY);

    saveStrings("/Users/lucaslongo/Desktop/allDataZ.txt", allDataZ);

  }


  int[] parseNokiaSensor(byte[] input){

    //System.out.println(input.length);

    int[] returnValues= new int[12]; // stater byte, which device, switch and 9 readings

    returnValues[0] = input[0] & 0x00FF;

    returnValues[1] = input[1] & 0x00FF;

    returnValues[2] = input[2] & 0x00FF;

    int place = 3;

    for(int i = 9; i < input.length-1; i= i + 2){

      returnValues[place] =  (int) ((((short) (input[i+1]& 0x00FF)) ) + (((short) (input[i]& 0x00FF)) * 255) - 32512);

      //println(i + " " + returnValues[place]);

      place++;

    }

    return returnValues;

  }



  ArrayList loadRecordsWithDateAtBeginning(String _url,int _lengthOfDataPart){

    _lengthOfDataPart = _lengthOfDataPart + 1; //for the comma

    ArrayList values = new ArrayList();

    String[] dataLines = loadStrings(_url);

    String data = join(dataLines,"");


    int currentChar = 0;


    while (currentChar < data.length()-1){

      int nextCommaPosition = data.indexOf(",",currentChar);

      if (nextCommaPosition == -1) break;

      String timeStamp = data.substring(currentChar, nextCommaPosition);

      String contents = data.substring(nextCommaPosition+1,nextCommaPosition + _lengthOfDataPart);

      long longTime = Long.parseLong(timeStamp);

      Date d = new Date(longTime);

      int[] parsedRecord = parseNokiaSensor(contents.getBytes());

      values.add(parsedRecord );

      currentChar = nextCommaPosition + _lengthOfDataPart;

      //println(currentChar);

    }


    return values;

  }

}