package de.frajul.endlessroll.entities;

import java.util.List;

import de.frajul.endlessroll.data.SynchronizedArrayList;
import de.frajul.endlessroll.data.Vector;
import de.frajul.endlessroll.entities.textures.Texture;

public class BackgroundLayer extends SynchronizedArrayList<Entity> {

    private final float PART_WIDTH = 5;
    private final float HALF_PART_WIDTH = PART_WIDTH / 2f;
    private final float HEIGHT = 2.5f;
    private Texture texture;
    private float speed;

    public BackgroundLayer(Texture texture, float speed) {
        this.texture = texture;
        this.speed = speed;
        super.add(createPart(-HALF_PART_WIDTH));
    }

    private Entity createPart(float xLeftEdge) {
        return new Entity(texture, new Vector(xLeftEdge + HALF_PART_WIDTH, (HEIGHT - 2) / 2), PART_WIDTH, HEIGHT);
    }

    public void move(float playerMovX, float cameraX) {
        Vector movement = new Vector(playerMovX * speed, 0);
        synchronized (this) {
            for (Entity part : this)
                part.move(movement);
        }
        if (!super.isEmpty()) {
            Entity last = super.get(super.size() - 1);
            if (last.getRightEdge() - cameraX < 3) {
                super.add(createPart(last.getRightEdge() - 0.001f));
            }
            if (super.get(0).getRightEdge() - cameraX < -3) {
                super.remove(0);
            }
        }
    }

    public void resetPosition() {
        super.clear();
        super.add(createPart(-HALF_PART_WIDTH));
    }

}
