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.render.ModelBatchRenderer;
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 ModelBatchRenderer modelBatchRenderer;

	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);
		setupWorld();
		setupEnvironment();
		setupCamera();
		setupControl();
		setupRendering();
	}

	@Override
	public void render() {
		executeGlFunctions();

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

		modelBatchRenderer.render();
	}

	private void executeGlFunctions() {
		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);
	}

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

	private void setupEnvironment() {
		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 setupCamera() {
		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();
	}

	private void setupControl() {
		control = new Control(camera, ControlActionMapping.getInstance(), map);
		Gdx.input.setInputProcessor(control);
	}

	private void setupRendering() {
		gridLines = new GridLines(map);
		highlighter = new Highlighter();
		gizmo = new Gizmo();

		modelBatchRenderer = new ModelBatchRenderer(environment, camera);
		modelBatchRenderer.add(world);
		modelBatchRenderer.add(gridLines);
		modelBatchRenderer.add(highlighter);

		//noinspection ConstantConditions
		if(config.showGizmo) {
			modelBatchRenderer.add(gizmo);
		}
	}

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

@@ -283,57 +283,8 @@
 nt;%0A
-import com.badlogic.gdx.graphics.g3d.ModelBatch;%0A
 impo
@@ -1201,94 +1201,8 @@
 rol;
-%0A%09private Gizmo gizmo;%0A%09private GridLines gridLines;%0A%09private Highlighter highlighter;
 %0A%0A%09p
@@ -2767,101 +2767,8 @@
 ) %7B%0A
-%09%09gridLines = new GridLines(map);%0A%09%09highlighter = new Highlighter();%0A%09%09gizmo = new Gizmo();%0A%0A
 %09%09mo
@@ -2893,17 +2893,21 @@
 add(
-g
+new G
 ridLines
 );%0A%09
@@ -2902,16 +2902,21 @@
 ridLines
+(map)
 );%0A%09%09mod
@@ -2935,17 +2935,21 @@
 rer.add(
-h
+new H
 ighlight
@@ -2950,16 +2950,18 @@
 hlighter
+()
 );%0A%0A%09%09//
@@ -3047,13 +3047,19 @@
 add(
-g
+new G
 izmo
+()
 );%0A%09

Simplify code