/**
 * 
 */
package studio.junk.jam.coloritum.screen;

import com.badlogic.ashley.core.Engine;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;

import box2dLight.RayHandler;
import studio.junk.jam.coloritum.GameState;

/**
 * @author held03
 */
public class InGame implements Screen {

	private static boolean showGrid = false;

	public static InGame currentGame;

	public SpriteBatch	batch;
	public Camera		camera;
	public Camera		cameraUI;

	public Engine engineUI;

	public ShapeRenderer	shapeRenderer;
	BitmapFont				font;

	Box2DDebugRenderer debugRenderer;

	public static RayHandler rayLightsHandler;

	/**
	 * 
	 */
	public InGame() {

	}

	private void playMusic(Music... tracks) {
		for (Music music : tracks) {
			music.setLooping(true);
			music.play();
		}
	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#show()
	 */
	@Override
	public void show() {

		Music musicOrange = Gdx.audio.newMusic(Gdx.files.internal("music/orange.wav"));
		Music musicBlue = Gdx.audio.newMusic(Gdx.files.internal("music/blue.wav"));
		Music musicGreen = Gdx.audio.newMusic(Gdx.files.internal("music/green.wav"));
		Music musicPurple = Gdx.audio.newMusic(Gdx.files.internal("music/purple.wav"));
		Music musicRed = Gdx.audio.newMusic(Gdx.files.internal("music/red.wav"));

		playMusic(musicOrange, musicBlue, musicGreen, musicPurple, musicRed);

		font = new BitmapFont();

		batch = new SpriteBatch();
		camera = new OrthographicCamera(GameState.WORLD_WIDTH_M, GameState.WORLD_HEIGHT_M);
		// camera.translate(8f, 4.5f, 0);
		camera.translate(GameState.WORLD_WIDTH_M / 2f, GameState.WORLD_HEIGHT_M / 2f, 0);
		cameraUI = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		cameraUI.translate(Gdx.graphics.getWidth() / 2f, Gdx.graphics.getHeight() / 2f, 0f);

		shapeRenderer = new ShapeRenderer();

		debugRenderer = new Box2DDebugRenderer();

//		engineUI = new Engine();
//
//		engineUI.addSystem(new MenuRender());
//		engineUI.addSystem(new MenuInteraction());
//
//		// initialize menus
//
//		Entity mainMenu = new Entity();
//		mainMenu.add(new Menu("mainMenu", true));
//		Texture lightSourceTex = ((Animation) GameState.state.assets.get("source")).getKeyFrame(8).getTexture();
//		mainMenu.add(new MenuEntries(new MenuEntries.Entry("Test", "showMenu", "addLightSourceMenu", lightSourceTex)));
//
//		Entity addLightSourceMenu = new Entity();
//		addLightSourceMenu.add(new Menu("addLightSourceMenu", true));
//		addLightSourceMenu
//				.add(new MenuEntries(new MenuEntries.Entry("Test", "showMenu", "addLightSourceMenu", lightSourceTex)));
//
//
//		engineUI.addEntity(mainMenu);
//		engineUI.addEntity(addLightSourceMenu);

		currentGame = this;
	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#render(float)
	 */
	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

		camera.update();
		batch.setProjectionMatrix(camera.combined);

		batch.begin();

		// batch.draw

		// debugRenderer.render(state.world, camera.combined);

		batch.draw(GameState.state.backgroundImage, 0, 0, GameState.WORLD_WIDTH_M, GameState.WORLD_HEIGHT_M);
		// batch.draw(new Texture("badlogic.jpg"), 0, 0);

		GameState.state.update(delta);

		batch.end();

		shapeRenderer.setProjectionMatrix(camera.combined);

		if (this.showGrid) {
			shapeRenderer.begin(ShapeType.Line);
			shapeRenderer.setColor(1, 1, 1, 1);

			for (int yPos = 0; yPos <= GameState.WORLD_HEIGHT_M; yPos += 1) {
				shapeRenderer.line(0, yPos, GameState.WORLD_WIDTH_M, yPos);
			}

			for (int xPos = 0; xPos <= GameState.WORLD_WIDTH_M; xPos += 1) {
				shapeRenderer.line(xPos, 0, xPos, GameState.WORLD_HEIGHT_M);
			}

			shapeRenderer.end();
		}

		rayLightsHandler.setCombinedMatrix((OrthographicCamera) camera);
		rayLightsHandler.updateAndRender();

		cameraUI.update();
		batch.setProjectionMatrix(cameraUI.combined);

		batch.begin();

		// TODO draw UI

//		engineUI.update(delta);

		GlyphLayout gl;
		gl = new GlyphLayout(font, "FPS: " + (Gdx.graphics.getFramesPerSecond()));
		font.draw(batch, gl, 30, 30);

		gl = new GlyphLayout(font, "Entities: " + (GameState.state.engine.getEntities().size()));
		font.draw(batch, gl, 30, 50);

		batch.end();
	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#resize(int, int)
	 */
	@Override
	public void resize(int width, int height) {
		cameraUI.viewportWidth = width;
		cameraUI.viewportHeight = height;

		cameraUI.position.set(width / 2f, height / 2f, 0f);
	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#pause()
	 */
	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#resume()
	 */
	@Override
	public void resume() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#hide()
	 */
	@Override
	public void hide() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * @see com.badlogic.gdx.Screen#dispose()
	 */
	@Override
	public void dispose() {
		rayLightsHandler.dispose();
	}

}
