package oracle.ostern;

import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PVector;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by mrzl on 07.04.2016.
 */
public class EmojiEasterEgg extends
        EasterEgg {


    ArrayList<C> flakes = new ArrayList<C>();
    static int nextIndex = 0;

    int MAX_FLAKES = 10;
    int INIT_FLAKES = 4;
    float FLAKE_CHANCE = 0.02f;

    Range radiusRange = new Range(12,30);
    Range xMoveSpeedRange = new Range(0.04f,0.08f);
    Range yMoveSpeedRange = new Range(0.3f,0.7f);
    Range xMoveRandomRange = new Range(-0.1f,0.1f);
    Range xMoveMaxRange = new Range(0.5f,1f);

    private PImage emojiSheet;

    ArrayList<PImage> emojiSet = new ArrayList<PImage>();


    public EmojiEasterEgg ( PApplet parent, float durationSeconds ) {
        super( parent, durationSeconds );

        emojiSheet = parent.loadImage("sheet_32.png");
        emojiSet.add(getEmoji(3, 7)); // heart
        for(int i=0; i < INIT_FLAKES;i++)
            flakes.add(new C());
    }

    @Override
    public void drawBefore () {
        if ( isRunning( ) ) {
            for (Iterator<C> iterator = flakes.iterator(); iterator.hasNext(); ) {
                C c = iterator.next();
                if (c.done) {
                    iterator.remove();
                } else {
                    c.draw();
                }
            }
        }
        if(flakes.size()  < MAX_FLAKES && parent.random(1) < FLAKE_CHANCE)
            flakes.add(new C());
    }

    @Override
    public void drawAfter () {
        if ( isRunning( ) ) {
            // after text
            //parent.noStroke( );
            //parent.fill( 255, 0, 0 );
            //parent.rect( parent.random( 0, parent.width ), parent.random( 0, parent.width ), parent.random( 0, parent.width ), parent.random( 0, parent.width ) );
        }
    }




    public boolean isRunning() {
        return super.isRunning() && flakes.size() == 0;
    }

    class C {


        PVector pos;
        boolean done = false;
        private int index;
        PImage emoji;
        float radius;
        float phaseShift = parent.random(parent.TAU);
        float xMoveSpeed;
        float yMoveSpeed;
        float xMoveMax;

        C() {
            radius = radiusRange.get();
            pos = new PVector(parent.random(parent.width), 0);
            xMoveSpeed = xMoveSpeedRange.get();
            yMoveSpeed = yMoveSpeedRange.get();
            xMoveMax = xMoveMaxRange.get();
            index = nextIndex++;
            emoji = emojiSet.get((int)parent.random(emojiSet.size()));
        }

        void updatePos() {
            float xOff = parent.sin(phaseShift+(index+parent.frameCount)*xMoveSpeed) * xMoveMax
                    +xMoveRandomRange.get();
            float yAdd = parent.map(parent.abs(xOff), 0, 1, 0.4f, -0.2f);

            pos.add(xOff, yMoveSpeed + yAdd);
            if (pos.y > parent.height + radius) {
                done = true;
            }
        }

        void draw() {
            updatePos();
            parent.image(emoji,pos.x,pos.y,radius,radius);
        }
    }


    PImage getEmoji(int x, int y) {
        return emojiSheet.get(x*32, y*32, 32, 32);
    }

    class Range {

        float min, max;

        Range(float min, float max) {
            this.min = min;
            this.max = max;
        }

        float get() {
            return parent.random(min, max);
        }
    }



}

@@ -283,17 +283,21 @@
 rayList%3C
-C
+Flake
 %3E flakes
@@ -313,17 +313,21 @@
 rayList%3C
-C
+Flake
 %3E();%0A   
@@ -1048,33 +1048,37 @@
  flakes.add(new 
-C
+Flake
 ());%0A    %7D%0A%0A    
@@ -1141,32 +1141,74 @@
 sRunning( ) ) %7B%0A
+            parent.println(%22EMOJI TIME%22);%0A
             for 
@@ -1217,17 +1217,21 @@
 terator%3C
-C
+Flake
 %3E iterat
@@ -1294,17 +1294,21 @@
         
-C
+Flake
  c = ite
@@ -1413,16 +1413,33 @@
  %7D else 
+if(!c.foreground)
 %7B%0A      
@@ -1514,16 +1514,53 @@
       if
+(super.isRunning()) %7B%0A            if 
 (flakes.
@@ -1565,17 +1565,16 @@
 s.size()
- 
  %3C MAX_F
@@ -1619,32 +1619,36 @@
 CE)%0A            
+    
 flakes.add(new C
@@ -1650,13 +1650,27 @@
 new 
-C());
+Flake());%0A        %7D
 %0A   
@@ -1764,260 +1764,297 @@
     
-// after text%0A            //parent.noStroke( );%0A            //parent.fill( 255, 0, 0 );%0A            //parent.rect( parent.random( 0, parent.width ), parent.random( 0, parent.width ), parent.random( 0, parent.width ), parent.random( 0, parent.width ) );
+for (Iterator%3CFlake%3E iterator = flakes.iterator(); iterator.hasNext(); ) %7B%0A                Flake c = iterator.next();%0A                if (c.done) %7B%0A                    iterator.remove();%0A                %7D else if(c.foreground)%7B%0A                    c.draw();%0A                %7D%0A            %7D
 %0A   
@@ -2180,14 +2180,17 @@
 ass 
-C
+Flake
  %7B%0A%0A
-%0A
     
@@ -2206,16 +2206,44 @@
 or pos;%0A
+        boolean foreground;%0A
         
@@ -2466,24 +2466,25 @@
 veMax;%0A%0A
+%0A
         
 C() %7B%0A  
@@ -2475,17 +2475,21 @@
         
-C
+Flake
 () %7B%0A   
@@ -2828,24 +2828,73 @@
 t.size()));%0A
+            foreground = parent.random(1) %3C0.5f;%0A
         %7D%0A%0A 
@@ -3734,13 +3734,10 @@
 %7D%0A    %7D%0A
-%0A%0A%0A
 %7D%0A

emojie adjust. still not running man