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

import moonlander.library.*; 
import ddf.minim.*; 
import hypermedia.net.*; 

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 sikio_by_em extends PApplet {

/**
  * Democompo entry for Instanssi 2017
  * by em (Matti Eskelinen)
*/  




int port = 9909;
String ip = "192.168.69.115";
UDP valot;
int conn = 0;

Moonlander moonlander;
PFont titleFont;
PFont credFont;
PShader metab;

public void setup(){
 
  valot = new UDP(this, port);
  //size(640,480,P2D);
  surface.setTitle("SIKI:O"); 
  
  noCursor();
  
  /*
    "Ave Marimba" Kevin MacLeod (incompetech.com)
    Licensed under Creative Commons: By Attribution 3.0 License
    http://creativecommons.org/licenses/by/3.0/
  */
  moonlander = Moonlander.initWithSoundtrack(this, "AveMarimba.mp3", 70, 4);
  moonlander.start();
  titleFont = createFont("Datalegreya-Gradient.otf",144);
  credFont = createFont("Datalegreya-Gradient.otf",72);
}

public void draw(){
  moonlander.update();
  int effect = (int)(moonlander.getValue("effect"));
  int title = (int)(moonlander.getValue("title"));
  float fadeout = (float)moonlander.getValue("fadeout");
  float siittiox = (float)moonlander.getValue("siittiox");
  float blur = (float)moonlander.getValue("blur");
  float dna = (float)moonlander.getValue("dna");
  float split = (float)moonlander.getValue("split");
  float cam = (float)moonlander.getValue("cam");
  float dPos = (float)moonlander.getValue("dPos");
  float cols = (float)moonlander.getValue("cols");
  float cshift = 30*(0.5f*blur);
  int nl = 28;
  for (int i = 0; i<nl; i++){
    byte[] packet = {
      1, // versio
      0, PApplet.parseByte(101), PApplet.parseByte(109), 0, // e m
      1, // efektin tyyppi
      PApplet.parseByte(i), // Valon indeksi
      0,
      PApplet.parseByte(225),
      PApplet.parseByte(180-5*cshift),
      PApplet.parseByte(180-5*cshift)
     };
    valot.send(packet, ip);
  }
  
  switch(effect){
    case 0:
      noStroke();
      background(40);
      fill(240);
      rectMode(CORNER);
      rect(0, 0, width,height/2);
      
      // Siittio
      fill(40);
      int sd = height/20;
      ellipse(width/2, siittiox*(height/2)+sd, sd, sd*1.2f);
      float shd = sd/4.0f;
      float shx;
      for (int i = 0; i < 16; i++){
        shx = siittiox*(height/2)+sd/2-(i*sd/8);
        ellipse(width/2+sin(0.3f*shx), shx, shd, shd);
        shd = shd*0.9f;
      }
      
      // Munasolu
      fill(240, 240-cshift, 240-cshift);
      ellipse(width/2, height+0.5f*(width-height), fadeout*width, fadeout*width);
      
      fill(40);
      if (title==1){
        textSize(height/6);
        
        textFont(titleFont);
        textAlign(CENTER);
        text("SIKI:O",width/2, height/2);
      }
      
      filter(BLUR, blur);
      
      return;
    case 1:
      int pairs = 64*4;
      float dw = width*4/pairs;
      float phi;
      float pdiff = 1.5f*PI;
      float zoom = 1 - 0.875f*(1-cam);
      
      background(240, 240-cshift, 240-cshift);
      rectMode(CORNER);
      pushMatrix();
      rotate(-0.45f*cam);
      translate(cam*(-width/4),cam*(height/5));
      noStroke();
      for(int k = -2; k < 3; k++){
        for(int i = 0; i < 2*pairs; i=i+2){
          phi = dna+(float)(0.15f*i);
          fill(120+100*sin(0.5f*phi),100,50);
          rect(
            zoom * (dw*i),
            height*0.5f + (k * height/3) + zoom *((int)(height*0.05f*sin(phi)-height*split*(pairs/(pairs-i+1)))), 
            zoom * dw,
            zoom * (int)(height*0.1f*sin(phi)));
          fill(100, 120+100*sin(0.5f*(phi+pdiff)), 50);
          rect(
            zoom * (dw*(i+1)),
            height*0.5f + (k * height/3) + zoom *((int)(height*0.05f*sin(phi+pdiff))+height*split*(pairs/(pairs-i+1))),
            zoom * dw, 
            zoom *(int)(height*0.1f*sin(phi+pdiff)));
        }
      }
      popMatrix();
      fill(240, 240-cshift, 240-cshift, fadeout);
      rect(0,0,width, height);
      return;
    case 2:
      background(240, 240-cshift, 240-cshift);
      ellipseMode(CENTER);
      fill(40);
      stroke(255);
      strokeWeight(10);
      hint(ENABLE_STROKE_PURE);
      int offset = width/4;
      for (int i=0; i<=cols; i++){
        ellipse(width/2 + min(i,1)*offset, height/2, dPos*width/5, (2-dPos)*width/5);
        offset = -offset;
      }
      
      textFont(credFont);
      textAlign(CENTER);
      textSize(height/16);
      fill(40);
      if(title>0){
        text("Music: incompetech.com", width*0.5f, height*0.75f);
      }
      if(title>1){
          text("Code: em", width*0.25f, height*0.25f);
      }
      if(title>2){
          text("GFX: em", width*0.75f, height*0.25f);
      }
      
      fill(240, 240-cshift, 240-cshift, fadeout);
      noStroke();
      rect(0,0,width, height);
      
      char[] inst = {'i','n','s','t','a','n','s','s','i',' ','2','0','1','7'};
      
      textSize(height/8);
      if(title>3){
        for (int i = 0; i<14; i++){
            fill(40+215*sin(dna+1.1f*i));
            text(inst, i, i+1, width*0.1f+i*width/16, height*0.5f);
        }
      }
      
      return;
    case 3:
        exit();
  }
  
}
  public void settings() {  fullScreen(); }
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "sikio_by_em" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
