package zero1hd.rhythmbullet.screens;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
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.rhythmbullet.RhythmBullet;

public class LoadingScreen extends ScreenAdapter implements ApplicationListener {
	private Stage stage;
	RhythmBullet core;
	Image zero1HD;
	private boolean done;
	public LoadingScreen(RhythmBullet core) {
		this.core = core;
		core.getAssetManager().load("splashlogo.png", Texture.class);
		core.getAssetManager().finishLoading();
		
		stage = new Stage(new ScreenViewport());
		
		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);
		zero1HD.addAction(Actions.sequence(Actions.color(Color.WHITE, 1f), Actions.fadeOut(0.5f)));
		
		core.queueAssets();
	}
	
	@Override
	public void show() {
		super.show();
	}
	
	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()) {
			moveOn();
		}
		stage.draw();
		super.render(delta);
	}
	
	private void moveOn() {
		if (!done) {
			Gdx.app.debug("Loading Screen", "queue has all been loaded. Action is done playing.");
			done = true;
			
			core.generateFonts(core.getPrefs().getInteger("screen-height"));
			core.defineSkinStyles();

			core.setScreen(new MainMenu(core));
			zero1HD.remove();
			core.getAssetManager().unload("splashlogo.png");
			core.getAssetManager().get("standard_thrust.p", ParticleEffect.class).flipY();
		}
	}
			
	@Override
	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
		super.resize(width, height);
	}
	
	@Override
	public void hide() {
		core.setInitComplete();
		super.hide();
	}

	@Override
	public void create() {
		
	}

	@Override
	public void render() {
	}
}
