package levelGen;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
import com.ld50.game.HF;

import java.util.HashMap;

import Global.Global;

public class Level {
	public int w, h;

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

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

	private int tileSizeX = 64;
	private int tileSizeY = 64;

	public static NewTile[][] newTiles;

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

	public static boolean isPlayerXYGotten;

	public Level(int w, int h) {
		this.w = w;
		this.h = h;
		realTiles = new byte[w][h];
		tiles = new byte[w * h];
		rectangles = new Rectangle[w][h];
		newTiles = new NewTile[w][h];
		isBroke = new Boolean[w][h];

		getMap(HF.TTW, HF.TTH);// draw out the level that it will render. woohoo

		System.out.println("Coords h: " + h);
		System.out.println("Coords w: " + w);
		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				newTiles[x][y] = new NewTile(x * tileSizeX, y * tileSizeY);
			}
		}
	}

	public byte[] getMap(int w, int h) {
		byte[] map = new byte[w * h];
		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...
				float val = MathUtils.random(-100f, 100f);// this is literally a random number. xD
				// System.out.println("val: " + val);
				if (val < -80) {
					map[i] = 4;// deepestwater
					Level.Map.put("" + x + "," + y, 4);
				} else if (val < -39) {
					map[i] = 3;// deepwater
					Level.Map.put("" + x + "," + y, 3);
				} else if (val < 0) {
					map[i] = 0;// water
					Level.Map.put("" + x + "," + y, 0);
				} else if (val >= 0 && val < 30f) {
					map[i] = 7;// sand
					Level.Map.put("" + x + "," + y, 7);
				} else if (val >= 30f && val < 50f) {
					map[i] = 2;// grass
					Level.Map.put("" + x + "," + y, 2);
				} else if (val >= 50f && val < 70f) {
					map[i] = 1;// rock
					Level.Map.put("" + x + "," + y, 1);
				} else if (val >= 70f && val < 90f) {
					map[i] = 5;// dark rock
					Level.Map.put("" + x + "," + y, 5);
				} else if (val > 90f) {
					map[i] = 6;// snow
					Level.Map.put("" + x + "," + y, 6);
				} else {
					map[i] = 8;// Lava
					Level.Map.put("" + x + "," + y, 8);
				}
			}
		}
		return map;
	}

	public void render(HF game, OrthographicCamera camera) {
		for (int y = 0; y < h; y++) {
			for (int x = 0; x < w; x++) {
				newTiles[x][y].RenderNewTile(game, camera);
			}
		}

	}

}
