package com.bmesta.intellij;

import java.awt.*;
import java.util.ArrayList;
import javax.swing.*;

import com.intellij.openapi.editor.Editor;
import com.intellij.ui.JBColor;

/**
 * @author Baptiste Mesta
 */
public class ParticleContainer extends JComponent {


    public static final int SIZE = 100;
    private final JComponent parent;
    private boolean dir;

    public ParticleContainer(Editor editor) {
        parent = editor.getContentComponent();
        parent.add(this);
        setVisible(true);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {

                    updateParticles();

                    try {
                        Thread.sleep(1000 / 60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }


        }).start();
    }

    private void shakeEditor(JComponent parent, int dx, int dy, boolean dir) {
        final Rectangle bounds = parent.getBounds();
        parent.setBounds(bounds.x + (dir ? -dx : dx), bounds.y + (dir ? -dy : dy), bounds.width, bounds.height);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        renderParticles(g);
    }

    private void updateParticles() {

        for (int i = 0; i <= particles.size() - 1; i++) {
            if (particles.get(i).update())
                particles.remove(i);
        }
        this.repaint();

    }

    private ArrayList<Particle> particles = new ArrayList<Particle>(500);

    public void addParticle(boolean bool, int x, int y) {
        int dx, dy;
        if (bool) {
            dx = (int) (Math.random() * 3);
            dy = (int) (Math.random() * 3);
        } else {
            dx = (int) (Math.random() * -3);
            dy = (int) (Math.random() * -3);
        }
        int size = (int) (Math.random() * 5);
        int life = (int) (Math.random() * (120)) + 380;
        final Particle e = new Particle(x, y, dx, dy, size, life, JBColor.green);
        particles.add(e);
    }

    public void renderParticles(Graphics g2d) {
        for (int i = 0; i <= particles.size() - 1; i++) {
            particles.get(i).render(g2d);
        }
    }


    public void update(Point point) {
        final int midX = SIZE / 2;
        final int midY = SIZE / 2;
        this.setBounds(point.x - midX, point.y - midY, SIZE, SIZE);
        addParticle(true, midX, midY);
        addParticle(false, midX, midY);
        addParticle(true, midX, midY);
        addParticle(false, midX, midY);
        addParticle(true, midX, midY);
        addParticle(false, midX, midY);
        shakeEditor(parent, 10, 10, dir);
        dir = !dir;
        this.repaint();
    }
}

@@ -1340,25 +1340,24 @@
 rticles() %7B%0A
-%0A
         for 
@@ -1631,22 +1631,8 @@
 cle(
-boolean bool, 
 int 
@@ -1667,32 +1667,8 @@
 dy;%0A
-        if (bool) %7B%0A    
     
@@ -1704,94 +1704,15 @@
  * 3
-);%0A            dy = (int) (Math.random() * 3);%0A        %7D else %7B%0A            dx = (int)
+ + 1) *
  (Ma
@@ -1718,36 +1718,37 @@
 ath.random()
- * -3);%0A    
+%3E0.5?-1:1) ;%0A
         dy =
@@ -1773,28 +1773,23 @@
 m() * -3
-);%0A        %7D
+ + 1);%0A
 %0A       
@@ -1823,17 +1823,17 @@
 dom() * 
-5
+4
 );%0A     
@@ -1961,13 +1961,16 @@
 lor.
-green
+darkGray
 );%0A 
@@ -2355,38 +2355,32 @@
     addParticle(
-true, 
 midX, midY);%0A   
@@ -2388,39 +2388,32 @@
     addParticle(
-false, 
 midX, midY);%0A   
@@ -2421,38 +2421,32 @@
     addParticle(
-true, 
 midX, midY);%0A   
@@ -2454,39 +2454,32 @@
     addParticle(
-false, 
 midX, midY);%0A   
@@ -2499,14 +2499,8 @@
 cle(
-true, 
 midX
@@ -2532,15 +2532,8 @@
 cle(
-false, 
 midX
@@ -2573,14 +2573,12 @@
 nt, 
-10, 10
+5, 5
 , di

enhance particle system