package de.frajul.particlelab.main;

import android.content.Context;

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

import de.frajul.particlelab.data.Vector;
import de.frajul.particlelab.entities.Entity;
import de.frajul.particlelab.entities.particles.ParticleSource;
import de.frajul.particlelab.entities.particles.ParticleSystem;
import de.frajul.particlelab.entities.textures.TexturePack;
import de.frajul.particlelab.rendering.Timer;

/**
 * Created by Julian on 20.07.2016.
 */
public class Scene extends ArrayList<Entity> {

    public float cameraX;

    private Vector screenSize;

    private ParticleSystem particleSystem;
    private TexturePack textures;

    public Scene(Context context, TexturePack texturePack, ParticleSystem particleSystem) {
        this.particleSystem = particleSystem;
        setTexturePack(texturePack);
        new ParticleSource(new Vector(0, 0), particleSystem.firework).start();
    }

    public void setTexturePack(TexturePack texturePack) {
        this.textures = texturePack;

    }

    public void update(Timer timer) {
        Iterator<Entity> iterator = super.iterator();
        while (iterator.hasNext()) {
            Entity entity = iterator.next();
            Vector movement = entity.getMovement();
            Vector finalMovement = new Vector(movement).mul(timer.getFrameTime());
            entity.move(finalMovement);
            if (entity.getRightEdge() - cameraX < -3f) {
                iterator.remove();
                super.remove(entity);
            }
        }
    }

    public Vector calcWorldFromScreenCoords(float screenX, float screenY) throws Exception {
        if (screenSize == null)
            throw new Exception("ScreenSize not set");
        float glCoordWidth = (2f * screenSize.x / screenSize.y);
        float x = ((screenX / screenSize.x) * 2f - 1f) * glCoordWidth / 2;
        x += cameraX;
        float y = -((screenY / screenSize.y) * 2f - 1f);
        return new Vector(x, y);
    }

    public void setScreenSize(Vector screenSize) {
        this.screenSize = screenSize;
    }

    public ParticleSystem getParticleSystem() {
        return particleSystem;
    }
}
