package de.samdev.colorrunner.game.world.map;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.XmlReader;
import com.badlogic.gdx.utils.XmlReader.Element;

import de.samdev.colorrunner.game.world.CRGameWorld;
import de.samdev.colorrunner.game.world.entities.StaticEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.DownStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.LeftStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.NoStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.RightStateFloorTileEntity;
import de.samdev.colorrunner.game.world.entities.gameentities.floor.UpStateFloorTileEntity;

public class CRTiledMap {
	public final static int GID_EMPTY_1 = 0;
	public final static int GID_EMPTY_2 = 1;
	
	public final static int GID_MID = 23;
	public final static int GID_UP = 13;
	public final static int GID_RIGHT = 24;
	public final static int GID_DOWN = 33;
	public final static int GID_LEFT = 22;
	public final static int GID_INTER_UP = 16;
	public final static int GID_INTER_DOWN = 17;
	public final static int GID_INTER_RIGHT = 18;
	public final static int GID_INTER_MID = 19;
	public final static int GID_INTER_END = 20;
	
	public int width;
	public int height;
	
	public int[][] map;
	public int[][] interaktionen;

	public String name;

	public static CRTiledMap load(String name, String inputXmlString) {
		CRTiledMap result = new CRTiledMap();
		
		XmlReader reader = new XmlReader();
		Element root = reader.parse(inputXmlString);
		int width = root.getInt("width");
		int height = root.getInt("height");

		result.interaktionen = new int [width][height];
		result.map = new int[width][height];
		result.width = width;
		result.height = height;
		result.name = name;

		Array<Element> elemLayers = root.getChildrenByName("layer");

		for (Element elemLayer:elemLayers) {

			if(elemLayer.getAttribute("name").equalsIgnoreCase("Kachelebene"))
			{
				root.getChildrenByName("layer");
				Element elemData = elemLayer.getChildByName("data");
				Array<Element> tiles = elemData.getChildrenByName("tile");

				for (int i = 0; i < tiles.size; i++) {
					int gid = tiles.get(i).getInt("gid");
					int tx = i % width;
					int ty = height - i / width - 1;

					result.map[tx][ty] = gid;
				}
			}
			else if(elemLayer.getAttribute("name").equalsIgnoreCase("Interaktionen"))
			{
				root.getChildrenByName("layer");
				Element elemData = elemLayer.getChildByName("data");
				Array<Element> tiles = elemData.getChildrenByName("tile");

				for (int i = 0; i < tiles.size; i++) {
					int gid = tiles.get(i).getInt("gid");
					int tx = i % width;
					int ty = height - i / width - 1;

					result.interaktionen[tx][ty] = gid;
				}
			}
			else
			{
				Gdx.app.error("Tiled LayerError", "Layer nicht gefundent");
			}
		}

		return result;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public int getGID(int x, int y) {
		return map[x][y];
	}

	public static StaticEntity CreateTile(CRGameWorld world, int id, float px, float py) {

		switch (id) {

			case CRTiledMap.GID_EMPTY_1:
			case CRTiledMap.GID_EMPTY_2:
				return null;

			case CRTiledMap.GID_MID:
				return new NoStateFloorTileEntity(world, px, py);

			case CRTiledMap.GID_UP:
				return new UpStateFloorTileEntity(world, px, py);

			case CRTiledMap.GID_RIGHT:
				return new RightStateFloorTileEntity(world, px, py);

			case CRTiledMap.GID_DOWN:
				return new DownStateFloorTileEntity(world, px, py);

			case CRTiledMap.GID_LEFT:
				return new LeftStateFloorTileEntity(world, px, py);

			default:
				Gdx.app.error("MapLoad", "Unknown GID: " + id);
				return null;
		}
	}
}
