package de.frajul.endlessroll.entities.collectables;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.frajul.endlessroll.data.SynchronizedArrayList;
import de.frajul.endlessroll.entities.Entity;
import de.frajul.endlessroll.entities.textures.TexturePack;
import de.frajul.endlessroll.levels.Level;
import de.frajul.endlessroll.levels.PositionData;

/**
 * Created by Julian on 06.09.2017.
 */

public class Collectables extends SynchronizedArrayList<Entity> {

    private List<Star> stars = Collections.synchronizedList(new ArrayList<Star>());
    private Energy energy;

    public void init(Level level, TexturePack textures) {
        for (int i = 0; i < level.getStars().size(); i++) {
            if (level.isStarCollected(i))
                continue;
            addStar(new Star(i, textures.star, level.getStars().get(i)));
        }

        PositionData energyData = level.getEnergyData();
        if (energyData != null && !level.isEnergyCollected()) {
            setEnergy(new Energy(textures.energy, energyData));
        }
    }

    @Override
    public synchronized boolean remove(Object object) {
        if(object instanceof Star)
            stars.remove(object);
        else
            energy = null;
        return super.remove(object);
    }

    private void addStar(Star star) {
        stars.add(star);
        super.add(star);
    }

    private void setEnergy(Energy energy) {
        this.energy = energy;
        super.add(energy);
    }

    public void reset() {
        stars.clear();
        energy = null;
        super.clear();
    }

    public List<Star> getStars() {
        return stars;
    }

    public Energy getEnergy() {
        return energy;
    }
}
