import drop.*;
import controlP5.*;

import java.util.*;
import processing.net.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import processing.video.*;
import com.hamoid.*;

VideoExport videoExport;

int maxFx = 15; //possible amount of effect layers
SDrop drop;
PImage source;
PGraphics renderer;
PImage viewport;
SourceManager sourceManager;
String frameName; //to save frame as image
int uptime; //logs how many frames this software has rendered before (inaccurate, it's actually way, way more)
boolean recording; //save every frame?
int renderSize = 500; //px
String[] saveData;
int w, h;

boolean spawnPlaying = true;

boolean videoRecord;

String[] params;

PImage droppedImage;
void dropEvent(DropEvent theDropEvent) {

  sourceManager.importURI(theDropEvent.filePath());
}

SecondApplet gui;

void setup() {
  size(800, 600);
  w = width;
  h = height;
  frameRate(60);
  surface.setResizable(true);
  surface.setLocation(570, 240);
  noSmooth(); //saves resources but makes sum look shiddy
  imageMode(CENTER);
  drop = new SDrop(this);
  saveData = loadStrings(dataPath("saves.sav"));
  println("Loading Savefile...");
  for (int i = 0; i < saveData.length; i ++) {
    String[] temp;
    if (saveData[i].contains("uptime")) {
      temp = split(saveData[i], " = ");
      uptime = int(temp[1]);
      println("Uptime: " + uptime);
    }
  }
  fallbackImage = loadImage(dataPath("0.png"));
  bin = loadImage(dataPath("bin.png"));


  String[] args = {"SecondFrame"};
  gui = new SecondApplet();
  PApplet.runSketch(args, gui);

  renderer = createGraphics(width, height);
  renderer.noSmooth();
  viewport = createImage(renderer.width, renderer.height, ARGB);
  sourceManager = new SourceManager();
  //sourceManager.importURI("/home/giers/.local/bin/fast-style-transfer/data/train2014");
}

void draw() {
  uptime+=1;
  background(255);

  sourceManager.update(); //will write some image to the renderer
  sourceManager.setSource();
  gui.shaderManager.applyShader(); //boolean animate (iterative calcs), boolean apply (draw)

  background(color(0));
  viewport = renderer.get();
  
  int imgw, imgh;
  if (renderer.width < renderer.height) {
    imgh = (int)height;
    imgw = int((float(renderer.width)/float(renderer.height))*height);
  } else {
    imgw = (int)width;
    imgh = int((float(renderer.height)/float(renderer.width))*width);
  }
  image(viewport, width/2, height/2, imgw, imgh);

  if (videoRecord) {
    videoExport.saveFrame();
    fill(#ff0000);
    ellipse(width-100, height-100, 30, 30);
  }


  if (recording) {
    snapshot();
    fill(#ff0000);
    ellipse(width-100, height-100, 30, 30);
  }
}

void snapshot() {
  frameName = str(uptime);
  renderer.save(dataPath("")+"/snapshots/" + frameName + ".png");
  saveData[1] = "uptime = " + frameName;
  println("Rendered " + frameName + ".png at " + dataPath("")+"/snapshots/ with a resolution of " + renderer.width + " x " + renderer.height);
  saveStrings(dataPath("saves.sav"), saveData);
}
