package com.eyeofmidas.cardtrick;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Interpolation;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.Scaling;

public class LoadingScreen implements Screen {

	private CardtrickGame game;
	private float percent;
	private boolean loaded = false;

	public LoadingScreen() {
		game = (CardtrickGame) Gdx.app.getApplicationListener();
	}

	@Override
	public void render(float delta) {
		game.getStage().act(delta);

		if (percent == 1.0f) {
			if (!loaded) {
				loaded = true;
				//game.getStage().addAction(Actions.color(Color.BLACK));
				//game.getStage().addAction(Actions.alpha(0f));
				game.getStage().clear();
				game.goMainMenu();
			}

		}
		if (game.getAssetManager().update()) {
			percent = 1.0f;
		}

		if (!loaded) {
			Gdx.gl.glClearColor(1, 1, 1, 1);
			Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

			game.getStage().draw();
			drawLoadingBar();
		}
	}

	private void drawLoadingBar() {

		game.getShapeRenderer().setProjectionMatrix(game.getStage().getCamera().combined);
		game.getShapeRenderer().begin(ShapeType.Filled);
		game.getShapeRenderer().setColor(Color.GRAY);
		game.getShapeRenderer().rect(game.getStage().getWidth() / 4, game.getStage().getHeight() / 2 - 40, game.getStage().getWidth() / 2, 40);
		game.getShapeRenderer().end();

		game.getShapeRenderer().begin(ShapeType.Filled);
		game.getShapeRenderer().setColor(Color.WHITE);
		percent = Interpolation.linear.apply(percent, game.getAssetManager().getProgress(), 0.1f);
		game.getShapeRenderer().rect(game.getStage().getWidth() / 4 + 5, game.getStage().getHeight() / 2 - 35, percent * game.getStage().getWidth() / 2 - 10, 30);
		game.getShapeRenderer().end();

	}

	@Override
	public void resize(int width, int height) {
		game.getStage().getViewport().update(width, height, true);
	}

	@Override
	public void show() {
		game.getStage().clear();
		loaded = false;
		game.getAssetManager().load("loading.atlas", TextureAtlas.class);
		game.getAssetManager().finishLoading();

		Table root = new Table();
		root.setFillParent(true);

		TextureAtlas loadingPack = game.getAssetManager().get("loading.atlas");
		Sprite logosprite = loadingPack.createSprite("libgdx_logo");
		Image logo = new Image(logosprite);
		logo.setScaling(Scaling.fit);
		root.add(logo).padBottom(70);

		game.getStage().addActor(root);

		game.getAssetManager().load("wallpaper.png", Texture.class);
		game.getAssetManager().load("fonts/liberation-32-white.fnt", BitmapFont.class);
		game.getAssetManager().load("fonts/liberation-64-white.fnt", BitmapFont.class);
		game.getAssetManager().load("data/cards.atlas", TextureAtlas.class);
		game.getAssetManager().load("data/uiskin.atlas", TextureAtlas.class);
	}

	@Override
	public void hide() {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

}
