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

import ddf.minim.spi.*; 
import ddf.minim.signals.*; 
import ddf.minim.*; 
import ddf.minim.analysis.*; 
import ddf.minim.ugens.*; 
import ddf.minim.effects.*; 

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







Minim minim;
AudioPlayer song;
PImage kukka;
PImage greets;
PImage kesa;
PImage syksy;
PImage meri;
PImage pilvi1;
PImage pilvi2;
PImage kyltti;
ArrayList<Sade> vetta;
ArrayList<Lumi> lunta;
int width = 1920;
int height = 1080;
public void setup(){
  size(width, height, P3D);
  frameRate(60);
  kukka = loadImage("flowersun.png");
  greets = loadImage("the_end.png");
  kesa = loadImage("rain_bg.png");
  syksy = loadImage("snow_bg.png");
  meri = loadImage("meri_bg.png");
  pilvi1 = loadImage("cloud_nr1.png");
  pilvi2 = loadImage("cloud_nr2.png");
  kyltti = loadImage("kyltti.png");
  pilvi1.resize(0, 150);
  pilvi2.resize(0, 150);
  imageMode(CENTER);
  vetta = sataa();
  lunta = lunta();
  setupAudio();
}

public void setupAudio() {
  minim = new Minim(this);
  song = minim.loadFile("musa.wav");
}

public void aalto_vaaka(float time){
  for(int i = 1; i < 8; i++){
    float posY = (32 + i) * sin(time*PI/2.5f - i) + height/8;
    float posX = width - (width/8) * i - time * 2.0f;
    if(i%2 == 0){
    image(pilvi2, posX, posY);
    }
    else{
      image(pilvi1, posX, posY);
    }
  }
}

class Sade {
  float koko, xpos, ypos, time, speed;
  Sade(){
    xpos = random(-500, width);
    ypos = random(-900, 0);
    koko = random(3,11);
    if(koko >=3 && koko <=5){
      speed = 9.0f;
    }
    if(koko >=6 && koko <=8){
      speed = 9.5f;
    }
    if(koko >=9 && koko <=10){
      speed = 10.0f;
    }
  }
  public void update(){
    ypos += speed;
    xpos += 1.0f;
    noStroke();
    fill(0,120,200);
    ellipse(xpos, ypos, koko, koko);
    if(koko <=5 && ypos > (height - height/2.5f)){
      xpos = random(-500, width);
      ypos = random(-300, 0);
      koko = random(3,11);
    }
    if(koko >=6 && koko <=8 && ypos > (height - height/4)){
      xpos = random(-500, width);
      ypos = random(-400, 0);
      koko = random(3,11);
    }
    if(ypos > height){
      xpos = random(-500, width);
      ypos = random(-500, 0);
      koko = random(3,11);
    }
  }
}

class Lumi{
  float koko, xpos, ypos, time, speed;
  Lumi(){
    xpos = random(-100, width + 200);
    ypos = random(-800, 0);
    koko = random(8,21);
    if(koko >=8 && koko <=12){
      speed = 3.5f;
    }
    if(koko >=13 && koko <=16){
      speed = 4.0f;
    }
    if(koko >=17 && koko <=20){
      speed = 4.5f;
    }
  }
  public void update(float time){
    ypos += speed;
    xpos += 1.5f * sin(time/2*PI);
    noStroke();
    fill(255,255,255, 127);
    ellipse(xpos, ypos, koko, koko);
    if(koko <=12 && ypos > (height - height/3)){
      xpos = random(-100, width + 200);
      ypos = random(-200, 0);
      koko = random(8,20);
    }
    if(koko >=13 && koko <=15 && ypos > (height - height/4)){
      xpos = random(-100, width + 200);
      ypos = random(-300, 0);
      koko = random(8,20);
    }
    if(ypos > height){
      xpos = random(-100, width + 200);
      ypos = random(-500, 0);
      koko = random(8,20);
    }
  }
}

public ArrayList<Sade> sataa(){
  ArrayList<Sade> sade = new ArrayList<Sade>();
  for(int a=0;a<5000;a++){
    sade.add(new Sade());  
  }
  return sade;
}

public ArrayList<Lumi> lunta(){
  ArrayList<Lumi> lumi = new ArrayList<Lumi>();
  for(int a=0;a<2000;a++){
    lumi.add(new Lumi());  
  }
  return lumi;
}

public void fadeOut(float time, float aika){
  float kert = ((time - aika)/3.0f);
  fill(0, kert * 240 );
  rect(0,0,width,height);
}

public void fadeIn(float time, float kohta){
  float kert2 = ((time - kohta)/3.0f);
     fill(0, (1.0f-kert2) * 255);
     rect(0,0,width,height);
}
public boolean sketchFullScreen(){
    return true;
}

public void kuvarotaatio(float time){
  pushMatrix();
  translate(width - width/6, height - 250);
  float rota = time/2;
  rotateY(rota);
  image(kyltti, 0, 0);
  popMatrix();
  
}
public void pyoriva(float time){
  pushMatrix();
  float koko = 40.0f;
  float angle = -1.0f * time/2;
  noStroke();
  translate((width - width/14), height/10);
  rotate(angle);
  scale(0.15f);
  image(kukka,0,0);
  popMatrix();
}
public void draw(){
  float time = (float)millis()/1000;
  if(time > 6){
      song.play();
  }
  
  if(time <= 22){
     background(kesa);
     for(int i=0;i<vetta.size();i++){
       vetta.get(i).update();
     }
   }
   
   if(time >18){
     fadeOut(time, 19.0f);
   }
   
   if(time > 21){
     background(syksy);
     fadeIn(time, 22.0f);
   }
   
   if(time > 24){
     background(syksy);       
     for(int i=0;i<lunta.size();i++){
       lunta.get(i).update(time +i);
     }
   }
   
   if(time > 41){
     fadeOut(time, 42.0f);
   }
   
   if(time > 44){
     background(meri);
     fadeIn(time, 45.0f);
   }
   
   if(time > 46){
     background(meri);
     aalto_vaaka(time);
     pyoriva(time);
   }
   
   if(time > 64){
     fadeOut(time, 65.0f);
   }
   
   if(time > 67){
     background(greets);
     fadeIn(time, 68.0f);
   }
   if(time > 69){
     background(greets);
     rectMode(CENTER);
     kuvarotaatio(time);
   }
   
   if(time > 86){
     rectMode(CORNER);
     fadeOut(time, 87.0f);
   }
   if(time > 89){
     minim.stop();
     exit();
   }
}
  static public void main(String[] passedArgs) {
    String[] appletArgs = new String[] { "--full-screen", "--bgcolor=#666666", "--hide-stop", "suomenkesa" };
    if (passedArgs != null) {
      PApplet.main(concat(appletArgs, passedArgs));
    } else {
      PApplet.main(appletArgs);
    }
  }
}
