package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.mygdx.game.Character.Mouse;
import com.mygdx.game.Character.Player;
import com.mygdx.game.Dimension.BlockMaterials;
import com.mygdx.game.Dimension.World;
import com.mygdx.game.Dimension.WorldRenderer;
import com.mygdx.game.UI.GUI;

import static com.mygdx.game.Restrictions.*;

public class Main extends ApplicationAdapter {
	private SpriteBatch batch;
	private WorldRenderer worldRenderer;
	private GUI gui;
	private FitViewport fitViewport;
	public static float delta = 0;
	float physicsUpdateSpeed = 1/60f; //how often you want to do a physics-update, in this case every 1/60th of a sec
	float timeSinceLastUpdate = 0f; //accumulator
	public static OrthographicCamera cam;


	@Override
	public void create () {
		Gdx.graphics.setWindowedMode(1024,576);
		cam = new OrthographicCamera(VIEWPORT_WIDTH, VIEWPORT_HEIGHT);
		fitViewport = new FitViewport(VIEWPORT_WIDTH,VIEWPORT_HEIGHT, cam);
		BlockMaterials.create();

		//Starting location of the player
		cam.translate(TILE_SIZE >> 1,  TILE_SIZE >> 1);
		Player.create(0,0);
		cam.zoom = 25f;

		Mouse mouse = new Mouse();
		gui = new GUI(mouse,fitViewport);
		worldRenderer = new WorldRenderer(mouse);
		batch = new SpriteBatch();
	}

	@Override
	public void render () {
		Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

		timeSinceLastUpdate += Gdx.graphics.getDeltaTime(); //Accumulate delta time
		batch.begin();
		if(timeSinceLastUpdate > 1/10f){
			worldRenderer.render(batch);
		}
		Player.render(batch,timeSinceLastUpdate);

		delta +=Gdx.graphics.getDeltaTime();
		if(delta > RENDER_TIME) {
			delta -= RENDER_TIME;
			World.loadChunks();
		}

		gui.render(batch);
		batch.end();
	}


	@Override
	public void dispose () {
		gui.dispose();
		batch.dispose();
		Player.textureAtlas.dispose();
	}
}
