package de.samdev.colorrunner.game.renderer;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

import de.samdev.colorrunner.CRGame;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.CRGameEntity;
import de.samdev.colorrunner.game.world.entities.MovingEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.FloorTileEntity;
import de.samdev.colorrunner.game.world.map.provider.EndlessMapProvider;
import de.samdev.colorrunner.game.world.map.provider.StaticMapProvider;

public class CRGameRenderer extends AbstractGameRenderer {
	private CRGameWorld gameworld;
	public double avgExecTime = 0;
	private Preferences endlessGameInfos = Gdx.app.getPreferences("endlessGameInfos");

	public CRGameRenderer(CRGameWorld _world, float width, float height) {
		super(width, height);

		gameworld = _world;
	}

	@Override
	public void doRender() {
		updateCameraOffset(gameworld.player.bounds.x + gameworld.player.bounds.width/2);
		gameworld.camViewBoundaries = getCamViewRectangle();

		renderMain();
		//renderHud();
		
		if (CRGame.DEBUG) renderDebugBoxes();

		if (CRGame.DEBUG) renderDebugInfo();
	}

	private void renderDebugBoxes() {
		shapeRenderer.begin(ShapeType.Line);
		shapeRenderer.setColor(1, 0, 1, 1);
		
		for (CRGameEntity e : gameworld.entities) {
			shapeRenderer.rect(e.bounds.x, e.bounds.y, e.bounds.width, e.bounds.height);
			
			if (e instanceof MovingEntity) {
				MovingEntity me = (MovingEntity) e;
				
				shapeRenderer.line(me.bounds.getCenter(new Vector2()), me.bounds.getCenter(new Vector2()).add(me.getVelocity().x / 15f, 0));
				shapeRenderer.line(me.bounds.getCenter(new Vector2()), me.bounds.getCenter(new Vector2()).add(0 , me.getVelocity().y / 15f));
			}
		}

		shapeRenderer.end();
		shapeRenderer.begin(ShapeType.Line);

		Rectangle bounds = gameworld.camViewBoundaries;

		float ftw = FloorTileEntity.FLOORTILE_WIDTH;

		int minx = (int)((bounds.getX() + 0) / ftw) - 2;
		int miny = (int)((bounds.getY() + 0) / ftw) - 2;
		int maxx = (int)((bounds.getX() + bounds.getWidth()) / ftw) + 2;
		int maxy = (int)((bounds.getY() + bounds.getHeight()) / ftw) + 2;

		for (int x = minx; x < maxx; x++)
		{
			for (int y = miny; y < maxy; y++)
			{
				switch (gameworld.mapprovider.getTrigger(x, y))
				{
					case RUNBOTTOM:
						shapeRenderer.setColor(1, 0, 0, 1f);
						shapeRenderer.rect(x*ftw+0, y*ftw+0, ftw-0, ftw-0);
						shapeRenderer.rect(x*ftw+4, y*ftw+4, ftw-8, ftw-8);
						shapeRenderer.rect(x*ftw+8, y*ftw+8, ftw-16, ftw-16);
						shapeRenderer.rect(x*ftw+12, y*ftw+12, ftw-24, ftw-24);
						break;
					case RUNTOP:
						shapeRenderer.setColor(0, 1, 0, 1f);
						shapeRenderer.rect(x*ftw+0, y*ftw+0, ftw-0, ftw-0);
						shapeRenderer.rect(x*ftw+4, y*ftw+4, ftw-8, ftw-8);
						shapeRenderer.rect(x*ftw+8, y*ftw+8, ftw-16, ftw-16);
						shapeRenderer.rect(x*ftw+12, y*ftw+12, ftw-24, ftw-24);
						break;
					case FLY:
						shapeRenderer.setColor(0, 0, 1, 1f);
						shapeRenderer.rect(x*ftw+0, y*ftw+0, ftw-0, ftw-0);
						shapeRenderer.rect(x*ftw+4, y*ftw+4, ftw-8, ftw-8);
						shapeRenderer.rect(x*ftw+8, y*ftw+8, ftw-16, ftw-16);
						shapeRenderer.rect(x*ftw+12, y*ftw+12, ftw-24, ftw-24);
						break;
					case END:
						shapeRenderer.setColor(0, 1, 1, 1f);
						shapeRenderer.rect(x*ftw+0, y*ftw+0, ftw-0, ftw-0);
						shapeRenderer.rect(x*ftw+4, y*ftw+4, ftw-8, ftw-8);
						shapeRenderer.rect(x*ftw+8, y*ftw+8, ftw-16, ftw-16);
						shapeRenderer.rect(x*ftw+12, y*ftw+12, ftw-24, ftw-24);
						break;

					case GRAVITY:
						shapeRenderer.setColor(1, 1, 0, 1f);
						shapeRenderer.rect(x*ftw+0, y*ftw+0, ftw-0, ftw-0);
						shapeRenderer.rect(x*ftw+4, y*ftw+4, ftw-8, ftw-8);
						shapeRenderer.rect(x*ftw+8, y*ftw+8, ftw-16, ftw-16);
						shapeRenderer.rect(x*ftw+12, y*ftw+12, ftw-24, ftw-24);
						break;

					case NOTHING:
						break;
				}
			}
		}

		//int px = (int)((gameworld.player.bounds.x + gameworld.player.bounds.width  / 2f) / FloorTileEntity.FLOORTILE_WIDTH);
		//int py = (int)((gameworld.player.bounds.y + gameworld.player.bounds.height / 2f) / FloorTileEntity.FLOORTILE_HEIGHT);
		//shapeRenderer.setColor(0, 0, 1, 1f);
		//shapeRenderer.rect(px * FloorTileEntity.FLOORTILE_WIDTH, py * FloorTileEntity.FLOORTILE_HEIGHT, FloorTileEntity.FLOORTILE_WIDTH, FloorTileEntity.FLOORTILE_HEIGHT);

		shapeRenderer.end();
	}

	private void renderMain() {
		spriteBatch.begin();

		for (CRGameEntity e : gameworld.entities) {
			TextureRegion tr = new TextureRegion(e.getTexture());
			spriteBatch.draw(new TextureRegion(e.getTexture()), 
					e.bounds.x + (e.bounds.width - tr.getRegionWidth())/2, e.bounds.y + (e.bounds.height - tr.getRegionHeight())/2, 
					tr.getRegionWidth()/2, tr.getRegionHeight()/2, 
					tr.getRegionWidth(), tr.getRegionHeight(), 
					e.visualZoomX, e.visualZoomY, 
					e.visualRotation, true);
			
		}


		spriteBatch.enableBlending(); 
		Vector3 cw_coords = cam.unproject(new Vector3(Gdx.graphics.getWidth(), 0, 0));
		spriteBatch.draw(CRAssets.TEX_COLORWHEEL, cw_coords.x - 70, cw_coords.y - 70, 64, 64);
		spriteBatch.disableBlending();

		spriteBatch.end();
	}

	private void renderDebugInfo() {
		beginDebug();

		renderDebug("FPS: " + (int)gameworld.fps.getFPS());

		renderDebug("Entitys: " + gameworld.entities.size());

		renderDebug("ExecTime: " + (int)(avgExecTime*100) / 100.0+ " / " + (int)(10000/gameworld.fps.getFPS())/10.0 + "ms (" + (int)((avgExecTime/(1000/gameworld.fps.getFPS()))*1000) / 10.0 + "%)");

		renderDebug("CameraOffset: (" + (int)cam.position.x + "|" + (int)cam.position.y + ")");

		renderDebug(
				"Player(x): " + (int)gameworld.player.bounds.x +
				" | Touch [T;L;B;R] := [" + (gameworld.player.isTouching_TOP()?1:0) +
				";" + (gameworld.player.isTouching_LEFT()?1:0) +
				";" + (gameworld.player.isTouching_BOTTOM()?1:0) +
				";" + (gameworld.player.isTouching_RIGHT()?1:0) + "]");

		if (gameworld.mapprovider instanceof EndlessMapProvider)
			renderDebug("Procedural Piece: \"" + ((EndlessMapProvider)gameworld.mapprovider).getCurrentSection(gameworld.player.bounds).piece_name + "\"");
		else if (gameworld.mapprovider instanceof StaticMapProvider)
			renderDebug("Level Progress: " + ((StaticMapProvider)gameworld.mapprovider).mapPlayerPos + "/" + ((StaticMapProvider)gameworld.mapprovider).mapMaxWidth);

		renderDebug("Player(tile): [" + (int)(gameworld.player.bounds.x / FloorTileEntity.FLOORTILE_WIDTH) + "|" + (int)(gameworld.player.bounds.y / FloorTileEntity.FLOORTILE_WIDTH) + "]");
		renderDebug("Player(trigger): " + gameworld.player.getTrigger());
		renderDebug("Player(velo): [" + gameworld.player.velocity.x + " | " + gameworld.player.velocity.y + "]");

		endDebug();
	}

	private void renderHud(){
		beginHud();
		renderHud("Points: " + gameworld.scoreMeter, 10);
		renderHud("Last Best: " + endlessGameInfos.getInteger("lastBest", 0), (Gdx.graphics.getWidth() / 2) - 10);
		if (gameworld.mapprovider instanceof EndlessMapProvider)renderDebug("Procedural Piece: \"" + ((EndlessMapProvider)gameworld.mapprovider).getCurrentSection(gameworld.player.bounds).piece_name + "\"");
		endHud();
	}
}
