package de.frajul.endlessroll.entities.particles;

import android.content.Context;

import de.frajul.endlessroll.entities.textures.TextureLoader;
import de.frajul.endlessroll.main.game.Timer;

/**
 * Created by Julian on 02.08.2016.
 */
public class ParticleSystem {

    public static final float TRANSFER_VALUE = 0.002f;
    public final ParticleEffect stasis;
    public final ParticleEffect testFire;
    public final ParticleEffect colorChange;
    public final ParticleEffect explosion;
    public final ParticleEffect magnet;
    public final ParticleEffect starCollect;
    public final ParticleEffect energyCollect;
    public final ParticleEffect firework;
    public final ParticleEffect superPower;
    private ParticleEffect[] effects;

    private TextureLoader textureLoader;

    public ParticleSystem(Context context) throws Exception {
        this.textureLoader = new TextureLoader(context);
        ParticleReader reader = new ParticleReader(context);
        stasis = reader.read("particleEffects/stasis.pe");
        testFire = reader.read("particleEffects/test_fire.pe");
        colorChange = reader.read("particleEffects/colorChange.pe");
        explosion = reader.read("particleEffects/explosion.pe");
        magnet = reader.read("particleEffects/magnet.pe");
        starCollect = reader.read("particleEffects/collectStar.pe");
        energyCollect = reader.read("particleEffects/collectEnergy.pe");
        firework = reader.read("particleEffects/firework.pe");
        superPower = reader.read("particleEffects/superPower.pe");

        effects = new ParticleEffect[]{stasis, testFire,colorChange, explosion, magnet, starCollect, energyCollect, firework, superPower};
    }

    public void update(Timer timer) {
        synchronized (effects) {
            for (ParticleEffect effect : effects)
                effect.update(timer);
        }
    }

    public void loadTextures() throws Exception {
        synchronized (effects) {
            for (ParticleEffect effect : effects)
                effect.setTexture(
                        textureLoader.loadTexture("particleEffects/" + effect.getTextureName()));
        }
    }

    public void deleteAllSources() {
        synchronized (effects) {
            for (ParticleEffect effect : effects) {
                effect.deleteSources();
            }
        }
    }

    public synchronized ParticleEffect[] getEffects() {
        return effects;
    }
}
