package uk.co.beercave.gunboat.renderers;

import uk.co.beercave.gunboat.ResourceManager;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class FaderRenderer {

	public static FaderRenderer instance;
	private OrthographicCamera camera;
	private SpriteBatch batch;
	
	private FaderRenderer(){}
	public static void initialise(OrthographicCamera camera, SpriteBatch batch){
		if (instance == null) instance = new FaderRenderer();
		instance.batch = batch;
		instance.camera = camera;
	}
	
	private float fade = 0;
	private boolean fading = false;
	private float fadeDirection = 0;
	public void renderFader(float delta){
		if (fading){
			fade += fadeDirection * delta;
			if (fade <= 0){
				fade = 0;
				fading = false;
			}else if (fade >= 1){
				fade = 1;
				fading = false;
			}
		}
		batch.begin();
		batch.setColor(0, 0, 0, fade);
		batch.draw(ResourceManager.blankTx, 0, 0, 800, 480);
		batch.setColor(1,1,1,1);
		batch.end();
	}
	
	public static void render(float delta){
		instance.renderFader(delta);
	}
	
	public static void fadeIn(float speed){
		instance.fading = true;
		instance.fadeDirection = -1 * speed;
	}
	public static void fadeOut(float speed){
		instance.fading = true;
		instance.fadeDirection = speed;
	}
	
}
