/*
	Copyright (c) 2016 Luka Prinčič, All rights reserved.
	This program is free software distributed under
	GNU General Public Licence. See COPYING for more info.
	- - - - - - - - - - - - - - - - - - - - - - - - - - - -

  jitakami.pde
    
                                                                             */


// basic screen settings
void settings() {
  fullScreen(P3D, 2);
  //size(1280, 720, P3D);

}

// load libs
import oscP5.*; // Open Sound Control
import netP5.*;
import processing.video.*;
//import gohai.glvideo.*;

// DECLARATIONS --------------- //

// OSC object
OscP5 oscP5;

String oscVal1;
float oscVal2;
// temporary
float ampColor;


// image pool, a 2D array
PImage[][] imgPool;


// video pool, a 2D array
Movie[][] vidPool;


// screenClean
float screenCleanHue, screenCleanSaturation, screenCleanBrightness,
  screenCleanAlpha, screenCleanFlickrAmount, screenCleanFlickrSpeed;


// drawFlylinesfly
boolean drawFlylinesflyToggle = false;
int drawFlylinesflyCurvesNumInit = 7000, // init 
    drawFlylinesflyCurvesNum = 127, // dynamic
    drawFlylinesflyPixBank = 0, drawFlylinesflyPixId = 1;
float drawFlylinesflySpeed = 20, drawFlylinesflyDirection = 0, drawFlylinesSensitivity = 1,
  drawFlylinesflyBrightDirection = 64, // 0-64-127
  drawFlylinesflyRotation = 0, drawFlylinesflyBrightRotation = 0;

int drawFlylinesHue = 0, drawFlylinesSatur = 0, drawFlylinesBright = 127, drawFlylinesAlpha = 27;
float drawFlylinesBrightAlpha = 127;
int drawFlylinesLineLength = 20;
float drawFlylinesBrightLength = 0;
int drawFlylinesStrokeWeight = 2;
int drawFlylinesPixHue = 127, drawFlylinesPixSatur = 127,
  drawFlylinesPixBright = 127, drawFlylinesPixAlpha = 10;
float[] curveX = new float[drawFlylinesflyCurvesNumInit];
float[] curveY = new float[drawFlylinesflyCurvesNumInit];


// drawPlates
boolean drawPlatesToggle = false;

int drawPlatesimgAbank = 0, drawPlatesimgAid = 9;
int drawPlatesTexAspeed = 1, drawPlatesTexAdirection = 16;
int drawPlatesimgATopLeft = 64, drawPlatesimgATopRight = 64,
  	drawPlatesimgABotRight = 64, drawPlatesimgABotleft = 64;

int drawPlatesimgAalpha = 127, drawPlatesimgAhue = 0, drawPlatesimgAbright = 127;
int drawPlatesimgBbank = 0, drawPlatesimgBid = 1;
int drawPlatesTexBspeed = 1, drawPlatesTexBdirection = 0;
int drawPlatesimgBTopLeft = 64, drawPlatesimgBTopRight = 64,
	  drawPlatesimgBBotRight = 64, drawPlatesimgBBotleft = 64;

int drawPlatesimgBalpha = 64, drawPlatesimgBhue = 64, drawPlatesimgBbright = 127;
int drawPlatesimgCbank = 0, drawPlatesimgCid = 2; 
int drawPlatesTexCspeed = 1, drawPlatesTexCdirection = 96;
int drawPlatesimgCTopLeft = 64, drawPlatesimgCTopRight = 64,
	  drawPlatesimgCBotRight = 64, drawPlatesimgCBotleft = 64;

int drawPlatesimgCalpha = 64, drawPlatesimgChue = 0, drawPlatesimgCbright = 127;
int drawPlatesimgBblendMode = 8, drawPlatesimgCblendMode = 8;

int drawPlatesSaturation = 0;
int drawPlatesCoordError = 0;


// tiles
boolean drawTilesToggle = false;
int drawTilesBgHue, drawTilesBgSat, drawTilesBgBri = 64, drawTilesHue, drawTilesSat, drawTilesBri = 64;
int drawTilesHueDistance = 20, drawTilesBlendMode = 0;
int drawTilesNumX = 3, drawTilesNumY = 3, drawTilesTexBank = 0, drawTilesTexId = 1;
float drawTilesTexSpeedX = 10, drawTilesTexSpeedY = 10, drawTilesOverlap = 10;
float texX = 0; // globals?
float texY = 0;


// image blender
boolean drawImageBlenderToggle = false;
int drawImageBlenderBank = 0, drawImageBlenderID = 2, iblNum = 1;
float iblX = 0, iblY = 0, iblWidth = 600, iblHeight = 100;
float iblRot; // radians need a float
float iblSpeedX, iblSpeedY;
float iblSpeedXfactor = 0, iblSpeedYfactor = 0;
float[] iblTexXY = new float[2]; // global object for texture coordinates
float iblTexWidth, iblTexHeight;
float iblTexSpeedX, iblTexSpeedY;
float iblTexSpeedXfactor, iblTexSpeedYfactor;
int iblH, iblS = 0, iblB = 100, iblA = 255; // stroke color / tint
int iblBflicker = 0; // flicker (Brightness)
float iblItX = 0, iblItY = 0, iblItTexX = 0, iblItTexY = 0, iblItRot;


// video player
boolean playVideoToggle = false;
boolean playVideoPausePlay = true;
boolean playVideoLoop = false;
int playVideoBank = 0, playVideoID = 0;
int playVideoHue, playVideoSaturation, playVideoBrightness,
  playVideoAlpha, playVideoJump;
int playVideoSpeed;
//float playVideoPosition = 0;


// ?
int flickCount = 2; // 2,3,4 or 5

// generate an array of random numbers
int[] rands = new int[500];

// testPicture
boolean testPictureToggle = false;
PFont testFont;

// fps
boolean displayFpsToggle = false;
PFont fpsFont;

// regular snapshots / improve: snap when buttonPress(SC-GUI)
boolean autoSnapToggle = false;


///////////////////////////////////////////////////////////////////////////////
void setup() { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  
  // framerate
  //frameRate(60); // looks like 60fps by default, see displayFps function
  //smooth(32);

  noCursor();
  background(0);
  
  // setup open sound control
  println("~~~ starting oscP5 ...");
  oscP5 = new OscP5(this,12000); // listening at port 12000
  //oscP5.plug(this,"hhosc","/highhat"); // osc from SuperCollider -> function 'scosc'

  // get all textures into an image pool
  println("\n\n~~~ loading textures into image pool ...\n");
  imgPool = getImages("/images/");

  // get all videos into an video pool
  println("\n\n~~~ loading videos into videos pool ...\n");
  vidPool = getVideos("/videos/");

  // create an array of random value between -250 and 250
  for (int i=0; i < 500; i++) { rands[i] = i-250; }
  shuffle(rands); 

  // testPicture font
	testFont = createFont("Oliver's Barney", 50);
  
  // fps
  fpsFont = createFont("Ubuntu Mono", 12);


  println("~~~ setup() finished. now to the draw() ...");
}




//////////////////////////////////////////////////////////////////////////////
void draw() { // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


  // clean screen
  blendMode(BLEND);
  screenClean
    ( screenCleanHue,
      screenCleanSaturation,
      screenCleanBrightness,
      screenCleanAlpha,
      screenCleanFlickrAmount,
      screenCleanFlickrSpeed );
  

  drawFlylines
    ( drawFlylinesflyToggle,
      drawFlylinesflyCurvesNum,
      drawFlylinesflyPixBank, drawFlylinesflyPixId,
      drawFlylinesflySpeed, drawFlylinesflyDirection, drawFlylinesSensitivity,
      drawFlylinesflyBrightDirection,
      drawFlylinesflyRotation, drawFlylinesflyBrightRotation,
      drawFlylinesHue, drawFlylinesSatur, drawFlylinesBright,
      drawFlylinesAlpha, drawFlylinesBrightAlpha,
      drawFlylinesLineLength, drawFlylinesBrightLength, drawFlylinesStrokeWeight,
      drawFlylinesPixHue, drawFlylinesPixSatur, drawFlylinesPixBright, drawFlylinesPixAlpha );



  drawPlates
    ( drawPlatesToggle,
      drawPlatesimgAbank, drawPlatesimgAid,
      drawPlatesTexAspeed, drawPlatesTexAdirection,
      drawPlatesimgATopLeft, drawPlatesimgATopRight,
      drawPlatesimgABotRight, drawPlatesimgABotleft,
      drawPlatesimgAalpha, drawPlatesimgAhue, drawPlatesimgAbright,

      drawPlatesimgBbank, drawPlatesimgBid,
      drawPlatesTexBspeed, drawPlatesTexBdirection,
      drawPlatesimgBTopLeft, drawPlatesimgBTopRight,
      drawPlatesimgBBotRight, drawPlatesimgBBotleft,
      drawPlatesimgBalpha, drawPlatesimgBhue, drawPlatesimgBbright,

      drawPlatesimgCbank, drawPlatesimgCid,
      drawPlatesTexCspeed, drawPlatesTexCdirection,
      drawPlatesimgCTopLeft, drawPlatesimgCTopRight,
      drawPlatesimgCBotRight, drawPlatesimgCBotleft,
      drawPlatesimgCalpha, drawPlatesimgChue, drawPlatesimgCbright,

      drawPlatesimgBblendMode, drawPlatesimgCblendMode, 
      drawPlatesSaturation,
      drawPlatesCoordError // 0-127
      );


  drawTiles
    ( drawTilesToggle,
      drawTilesBgHue, drawTilesBgSat, drawTilesBgBri, drawTilesHue, drawTilesSat, drawTilesBri,
      drawTilesHueDistance,                 // tile hue distance
      drawTilesBlendMode, // blendMode
      drawTilesNumX,                // number of drawTiles on X axis
      drawTilesNumY, // number of drawTiles on Y axis
      drawTilesTexBank,  // texture bank number
      drawTilesTexId,  // texture number/id
      drawTilesTexSpeedX, // texture speed X
      drawTilesTexSpeedY, // texture speed Y
      drawTilesOverlap // overlap. 127 = 300%
      );


  drawImageBlender
    ( drawImageBlenderToggle,
      drawImageBlenderBank, drawImageBlenderID,
      //     iblImg1,
      iblNum,
      iblX,  iblY,  iblWidth,  iblHeight,
      iblRot,
      iblSpeedX,  iblSpeedY,
      iblSpeedXfactor,  iblSpeedYfactor,
      iblTexWidth,  iblTexHeight,
      iblTexSpeedX,  iblTexSpeedY,
      iblTexSpeedXfactor,  iblTexSpeedYfactor,
      iblH,  iblS,  iblB,  iblA,
      iblBflicker,
      iblItX,  iblItY,
      iblItTexX,  iblItTexY,  iblItRot
      );



  playVideo
    ( playVideoToggle,
      playVideoPausePlay,
      playVideoLoop,
      playVideoBank, playVideoID,
      playVideoHue, playVideoSaturation,
      playVideoBrightness, playVideoAlpha, playVideoJump, playVideoSpeed
      //playVideoPosition
      );

  if(playVideoJump != 1000) {
    playVideoJump = 1000;
};



  // draw test picture
  testPicture(testPictureToggle);

  // frames per second
  displayFps(displayFpsToggle);

  // document
  autoSnap(autoSnapToggle);
  
 

} ///////////////////////////////////////////////////////////////////////////







//void mousePressed() {
//  vidPool[0][0].jump(map(mouseX, 0, width, 0, vidPool[0][0].duration()));
//}
