Capture cam;
Movie movie;
boolean moviePlaying;
PImage fallbackImage;

void movieEvent(Movie m) {
  if (moviePlaying) {
    m.read();
    //if mode = 1..
    sourceManager.setImage(movie.get());
    //movie.jump(random(movie.duration())); //??
  }
}
void newMovie(String path) {
  movie = new Movie(this, path);
}

boolean isVideo(String path) {
  String fpl = path.toLowerCase();
  if (fpl.endsWith(".mp4") || fpl.endsWith(".avi") || fpl.endsWith(".webm") || fpl.endsWith(".mkv") || fpl.endsWith(".mov")) return true;
  else  return false;
}
boolean isImage(String path) {
  String fpl = path.toLowerCase();
  if (fpl.endsWith(".bmp") || fpl.endsWith(".png") || fpl.endsWith(".jpg") || fpl.endsWith(".jpeg") || fpl.endsWith(".gif")) return true; //gif = special case!
  else return false;
}

class SourceManager { //no constructor


  void setSource() { //set source to renderer    
    renderer.beginDraw(); 
    renderer.image(source, 0, 0, renderer.width, renderer.height);
    renderer.endDraw();
  }
  
  void setImage(PImage img) { //set image to source
    source = img.get();
    fallbackImage = source;
    nativeResoX = source.width;
    nativeResoY = source.height;
    println(nativeResoX);
  }

  void fallback() {
    println("Fallback");
    setImage(fallbackImage); //fallbackImage should always be a copy of the last image that worked
  }

  void update() {
    if (source == null) {
      fallback();
    }

    int toW, toH;
    if (source.width > source.height) { 
      toW = renderSize;
      toH = int(map(source.height, 0, source.width, 0, renderSize));
    } else { 
      toW = int(map(source.width, 0, source.height, 0, renderSize));
      toH = renderSize;
    }
    toW = (toW % 2 == 0) ? toW : toW+1;
    toH = (toH % 2 == 0) ? toH : toH+1;

    renderer.setSize(toW, toH);
  }


  // This function returns all the files in a directory as an array of Strings  
  String[] listFileNames(String dir) {
    File file = new File(dir);
    if (file.isDirectory()) {
      String names[] = file.list();
      return names;
    } else {
      return null;
    }
  }


  String dir;
  String[] selection;
  int selId;
  void importURI(String path) {
    File file = new File(path);
    if (file.isDirectory()) {
      dir = path;
      String names[] = file.list();
      ArrayList<String> usableFiles = new ArrayList<String>();
      for (int i = 0; i < names.length; i++) {
        if (isImage(names[i]) || isVideo(names[i])) {
          usableFiles.add(names[i]);
        }
      }
      selection = new String[usableFiles.size()];
      for (int i = 0; i < usableFiles.size(); i++) selection[i] = usableFiles.get(i);
      println("Got new selection from folder. " + selection.length + " usable files.");
      path = dir + "/" + selection[(int)random(selection.length)];
    } 


    if (isImage(path) || isVideo(path)) {
      println("Importing file " + path);
      if (moviePlaying) movie.stop();
      if (isImage(path)) {
        moviePlaying = false;
        setImage(loadImage(path));
      } else if (isVideo(path)) {
        moviePlaying = true;
        newMovie(path);
        movie.loop();
        movie.volume(0);
      }
    }
  }
}
