package de.samdev.colorrunner.screens.gameScreen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.input.GestureDetector;

import de.samdev.colorrunner.game.renderer.CRGameRenderer;
import de.samdev.colorrunner.game.world.AverageExecutionLogger;
import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.map.provider.MapProvider;
import de.samdev.colorrunner.input.CRGameInputProcessor;

public class GameScreen implements Screen {
	private CRGameWorld world;
	private CRGameRenderer renderer;
	private CRGameInputProcessor input = new CRGameInputProcessor(world);
	
	private AverageExecutionLogger execTime = new AverageExecutionLogger();



	public GameScreen(MapProvider prov) {
		world = new CRGameWorld(prov); // initialize world
		renderer = new CRGameRenderer(world, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); // initialize renderer


		input = new CRGameInputProcessor(world);
	}

	@Override
	public void render(float delta) {
		execTime.On();
		//#######################
		
		
		delta = Math.min(delta, (1/45f));
		
        world.update(delta);
        renderer.render();
        
        input.update();

        
		//#######################
        renderer.avgExecTime = execTime.getAvg();
		execTime.Off();
	}

	@Override
	public void resize(int width, int height) {
		Gdx.app.log("GameScreen", "resize called");
		
		renderer.resize(width, height);
	}

	@Override
	public void show() {
		Gdx.input.setInputProcessor(new InputMultiplexer(input, new GestureDetector(input)));

		Gdx.app.log("GameScreen", "show called");
	}

	@Override
	public void hide() {
		Gdx.input.setInputProcessor(null);

		Gdx.app.log("GameScreen", "hide called");
	}

	@Override
	public void pause() {
		Gdx.app.log("GameScreen", "pause called");
	}

	@Override
	public void resume() {
		Gdx.app.log("GameScreen", "resume called");
	}

	@Override
	public void dispose() {
		Gdx.app.log("GameScreen", "dispose called");
	}
}
