package levelGen;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.math.Rectangle;
import com.ld52.agent.Agent;
import com.ld52.agent.GameScreen;

import java.util.HashMap;

import Global.Global;

public class LevelTmx {
	public int w, h;

	public byte[] tiles;
	public byte[] data;
	public byte[][] realTiles;

	public Rectangle[][] rectangles;
	public Boolean[][] isBroke;

	private int tileSizeX = 64;// was 64
	private int tileSizeY = 64;// was 64

	// public static NewTile[][] newTiles;

	public static HashMap<String, Integer> Map = new HashMap<String, Integer>();
	// public static ObjectMap<String, Integer> Map = new ObjectMap<String, Integer>();

	public static boolean isPlayerXYGotten;
	// test the tilemap editor:
	private TmxMapLoader maploader;
	private TiledMap map;
	private OrthogonalTiledMapRenderer renderer;

	public LevelTmx(int w, int h, GameScreen screen) {
		this.w = w;
		this.h = h;
		tiles = new byte[w * h];
		rectangles = new Rectangle[w][h];
		isBroke = new Boolean[w][h];
		// tmx map loader:
		maploader = new TmxMapLoader();
		map = maploader.load("tilemaps/tilemap.tmx");
		renderer = new OrthogonalTiledMapRenderer(map);
		// getMap(HF.TTW + 10, HF.TTH + 10, map);// draw out the level that it will render. woohoo

		System.out.println("Coords h: " + h);
		System.out.println("Coords w: " + w);
	}

	public byte[] getMap(int w, int h, TiledMap tileMap) {
		byte[] map = new byte[w * h];
		System.out.println("All Layers: " + tileMap.getLayers().size());
		// i need to draw each layer?! :O
		for (int L = 0; L < tileMap.getLayers().size(); L++) {
			System.out.println("layer: " + L);
			TiledMapTileLayer tileLayer = (TiledMapTileLayer) tileMap.getLayers().get(L);
			for (int y = 0; y < h; y++) {
				for (int x = 0; x < w; x++) {
					// int i = x + y * w;
					if (!isPlayerXYGotten) {
						isPlayerXYGotten = true;
						Global.P.x = 0;
						Global.P.y = 0;
						System.out.println("x: " + Global.P.x);
						System.out.println("y: " + Global.P.y);
					}
					// draw out a map somehow...
					// Cell cell = tileLayer.getCell(x, y);
					// int val = cell.getTile().getId();// get the id of the tile.
					// System.out.println("val: " + val);
					// float val = MathUtils.random(-100f, 100f);// this is literally a random number. xD best map ever. haha
					// how do we want to draw the map? For now i just want to make a path you can walk on. so,
					// choose a path at the bottom row, then randomly insert another block in the path. idk, just make it up.
					System.out.println("val: ");// + val);
//					 if (val < -80) {
//					 map[i] = 4;// deepestwater
//					 LevelTmx.Map.put("" + x + "," + y, 4);
//					 } else if (val < -39) {
//					 map[i] = 3;// deepwater
//					 LevelTmx.Map.put("" + x + "," + y, 3);
//					 } else if (val < 0) {
//					 map[i] = 0;// water
//					 LevelTmx.Map.put("" + x + "," + y, 0);
//					 } else if (val >= 0 && val < 30f) {
//					 map[i] = 7;// sand
//					 LevelTmx.Map.put("" + x + "," + y, 7);
//					 } else if (val >= 30f && val < 50f) {
//					 map[i] = 2;// grass
//					 LevelTmx.Map.put("" + x + "," + y, 2);
//					 } else if (val >= 50f && val < 70f) {
//					 map[i] = 1;// rock
//					 LevelTmx.Map.put("" + x + "," + y, 1);
//					 } else if (val >= 70f && val < 90f) {
//					 map[i] = 5;// dark rock
//					 LevelTmx.Map.put("" + x + "," + y, 5);
//					 } else if (val > 90f) {
//					 map[i] = 6;// snow
//					 LevelTmx.Map.put("" + x + "," + y, 6);
//					 } else {
//					 map[i] = 8;// Lava
//					 LevelTmx.Map.put("" + x + "," + y, 8);
//					 }
				}
			}
		}
		return map;
	}

	/*
	 * What i want to try to do, to achieve an effect of walking through the grass is to randomize the tiles as they are spawned on the screen, but keep the
	 * locations fixed in the scrolling of the camera to the world. so, that means that each 'layer' of the grass needs to be rendered in order based on what
	 * number it is. The player will then be rendered in between one of the sets of grass tiles. any that are... on the same or lower number. as the player
	 * moves 'up' the screen, there must be a way to track whether it should be behind or on top of that grass tile? hmmm. i dunno. thinking.
	 * 
	 */
	public void render(Agent game, OrthographicCamera camera, float dt) {
		renderer.setView(camera);
		renderer.render();
	}
}
