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

import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;

import studio.junk.jam.coloritum.GameState;
import studio.junk.jam.coloritum.entities.components.Lights;

public class LightWobbleController extends IteratingSystem {

	public static final Family family = Family.all(Lights.class).get();

	protected float stateTime;

	public static final float	growTime	= 0.17f;
	public static final float	minGlow		= 0.8f;

	public LightWobbleController(int priority) {
		super(family, priority);
	}

	public LightWobbleController() {
		super(family);
	}

	@Override
	public void update(float deltaTime) {
		stateTime += deltaTime / GameState.PULSE_INTERVAL;
		if (stateTime > 1) {
			stateTime -= 1;
		}
		super.update(deltaTime);
	}

	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		Lights lights = Lights.mapper.get(entity);
		for (int i = 0; i < lights.lights.length; i++) {
			if (stateTime < growTime) {
				lights.lights[i].setDistance(lights.distance[i] * (minGlow + (1 - minGlow) * stateTime / growTime));

			} else {
				lights.lights[i].setDistance(
						lights.distance[i] * (minGlow + (1 - minGlow) * (1 - (stateTime - growTime) / (1 - growTime))));

			}
		}
	}
}
