package de.eightbitboy.ecorealms;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.math.Vector3;

import de.eightbitboy.ecorealms.config.EcoRealmsConfig;
import de.eightbitboy.ecorealms.control.Control;
import de.eightbitboy.ecorealms.control.ControlActionMapping;
import de.eightbitboy.ecorealms.map.Map;
import de.eightbitboy.ecorealms.world.meta.GridLines;
import de.eightbitboy.ecorealms.world.World;
import de.eightbitboy.ecorealms.world.meta.Gizmo;
import de.eightbitboy.ecorealms.world.tool.Highlighter;

public class EcoRealms extends ApplicationAdapter {
	private EcoRealmsConfig config;
	private Map map;
	private World world;

	private Environment environment;
	private PerspectiveCamera camera;
	private ModelBatch modelBatch;

	private Control control;
	private Gizmo gizmo;
	private GridLines gridLines;
	private Highlighter highlighter;

	public EcoRealms() {
		this.config = new EcoRealmsConfig();
	}

	@Override
	public void create() {
		Gdx.app.setLogLevel(Application.LOG_DEBUG);

		createWorld();
		createEnvironment();
		createCamera();

		control = new Control(camera, ControlActionMapping.getInstance(), map);
		Gdx.input.setInputProcessor(control);

		modelBatch = new ModelBatch();
		gridLines = new GridLines(map);
		highlighter = new Highlighter();
		gizmo = new Gizmo();
	}

	@Override
	public void render() {
		Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
		Gdx.gl.glEnable(GL20.GL_BLEND);
		Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

		control.updateCamera();
		ControlActionMapping.getInstance().update();
		camera.update();

		modelBatch.begin(camera);
		renderModels();
		modelBatch.end();
	}

	@Override
	public void dispose() {
		modelBatch.dispose();
	}

	private void renderModels() {
		modelBatch.render(world.getModelInstances(), environment);
		modelBatch.render(gridLines.getModelInstances(), environment);
		modelBatch.render(highlighter.getModelInstances(), environment);

		//noinspection ConstantConditions
		if (config.showGizmo) {
			modelBatch.render(gizmo.getModelInstances(), environment);
		}
	}

	private void createWorld() {
		map = new Map(config.WORLD_SIZE_X, config.WORLD_SIZE_Y);
		world = new World(map);
	}

	private void createEnvironment() {
		environment = new Environment();
		environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1f));
		environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, 1f, 0.5f, -0.8f));
	}

	private void createCamera() {
		camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.position.set(-10f, -10f, 10f);
		camera.rotate(new Vector3(1, 0, 0), 90);
		camera.lookAt(0, 0, -5);
		camera.near = 1f;
		camera.far = 300f;
		camera.update();
	}
}

@@ -1402,22 +1402,21 @@
 UG);%0A%0A%09%09
-create
+setup
 World();
@@ -1418,22 +1418,21 @@
 ld();%0A%09%09
-create
+setup
 Environm
@@ -1440,22 +1440,21 @@
 nt();%0A%09%09
-create
+setup
 Camera()
@@ -2481,17 +2481,16 @@
 ons%0A%09%09if
- 
 (config.
@@ -2502,16 +2502,16 @@
 izmo) %7B%0A
+
 %09%09%09model
@@ -2586,22 +2586,21 @@
 te void 
-create
+setup
 World() 
@@ -2704,22 +2704,21 @@
 te void 
-create
+setup
 Environm
@@ -2950,22 +2950,21 @@
 te void 
-create
+setup
 Camera()

Rename methods