package zero1hd.polyjet.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.ScreenAdapter;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.ParticleEffect;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

import zero1hd.polyjet.Polyjet;
import zero1hd.polyjet.util.TransitionAdapter;

public class LoadingScreen extends ScreenAdapter {
	private Stage stage;
	Polyjet core;
	Image zero1HD;
	Screen gotoScreen;
	boolean reInit;
	
	public LoadingScreen(Polyjet core, Screen gotoScreen, boolean reInit, boolean timer) {
		this.core = core;
		this.gotoScreen = gotoScreen;
		this.reInit = reInit;
		
		stage = new Stage(new ScreenViewport());
		core.getAssetManager().load("splashlogo.png", Texture.class);
		core.getAssetManager().finishLoading();
		zero1HD = new Image(this.core.getAssetManager().get("splashlogo.png", Texture.class));
		
		zero1HD.setColor(0f,1f,1f,0f);
		stage.addActor(zero1HD);
		
		zero1HD.setPosition(stage.getWidth()/2 - zero1HD.getWidth()/2, stage.getHeight()/2 - zero1HD.getHeight()/2);
		if (timer) {
			zero1HD.addAction(Actions.sequence(Actions.color(Color.WHITE, 1f)));
		}
		core.queueAssets();
	}
	
	float count = 0;
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		stage.act(delta);
		
		if (!zero1HD.hasActions() & core.getAssetManager().update()) {
			Gdx.app.debug("Loading Screen", "queue has all been loaded. Action is done playing.");
			zero1HD.remove();
			
			core.generateFonts();
			core.defineSkinStyles();
			
			if (reInit) {
				((TransitionAdapter) gotoScreen).postTransition();
			}
			
			core.setScreen(gotoScreen);
			core.getAssetManager().unload("splashlogo.png");
			core.getAssetManager().get("standard_thrust.p", ParticleEffect.class).flipY();
		}
		
		stage.draw();
		super.render(delta);
	}
	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
		super.resize(width, height);
	}
}
