package studio.junk.jam.coloritum.entities.systems;

import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IntervalIteratingSystem;
import com.badlogic.gdx.math.Vector2;

import studio.junk.jam.coloritum.GameState;
import studio.junk.jam.coloritum.entities.Families;
import studio.junk.jam.coloritum.entities.components.PigmentComponent;
import studio.junk.jam.coloritum.entities.components.Source;
import studio.junk.jam.coloritum.entities.components.TilePosition;

public class SourceController extends IntervalIteratingSystem {
	public static final Family family = Families.lightSource;

	protected static int tick;

	public SourceController(int priority) {
		super(family, GameState.PULSE_INTERVAL, priority);
		// TODO Auto-generated constructor stub
	}

	public SourceController() {
		super(family, GameState.PULSE_INTERVAL);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void updateInterval() {
		tick++;
		super.updateInterval();
	}

	@Override
	protected void processEntity(Entity entity) {

		Source source = Source.mapper.get(entity);

		if (tick % source.emissionPeriod == 0) {

			TilePosition pos = TilePosition.mapper.get(entity);
			PigmentComponent pigment = PigmentComponent.mapper.get(entity);

			// Position, init vel, wobble per, pigment
			GameState.state.types.get("bulb").create("bulb", pos.position.cpy().add(0, 1), new Vector2(0, 1),
					source.emissionPeriod, pigment.pigment);
			GameState.state.types.get("bulb").create("bulb", pos.position.cpy().add(1, 0), new Vector2(1, 0),
					source.emissionPeriod, pigment.pigment);
			GameState.state.types.get("bulb").create("bulb", pos.position.cpy().add(0, -1), new Vector2(0, -1),
					source.emissionPeriod, pigment.pigment);
			GameState.state.types.get("bulb").create("bulb", pos.position.cpy().add(-1, 0), new Vector2(-1, 0),
					source.emissionPeriod, pigment.pigment);

		}

	}

}
