package org.quangmire.tiledmaps;

import java.util.ArrayList;

import org.quangmire.ftales.client.Polygon;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class IsometricMap {
	private IsometricTile[][] mapData;
	private int WIDTH, HEIGHT;
	public float x = IsometricTile.TILE_WIDTH*IsometricTile.SCALE*8,y;
	private int screenWidth = Gdx.graphics.getWidth(), screenHeight = Gdx.graphics.getHeight();
//	private NinePatch rect;
	private int tilesRendered;
	public Polygon mapBorder;
	public ArrayList<Polygon> polys = new ArrayList<Polygon>();

	public IsometricMap() {}
	
	public void setData(IsometricTile[][] data) {
		mapData = data;
		WIDTH = data.length;
		HEIGHT = data[0].length;
		if(WIDTH*IsometricTile.TILE_WIDTH*IsometricTile.SCALE <= Gdx.graphics.getWidth())
			x = (Gdx.graphics.getWidth()-WIDTH*IsometricTile.TILE_WIDTH*IsometricTile.SCALE)/2 + WIDTH*IsometricTile.TILE_WIDTH*IsometricTile.SCALE/2;
		if(HEIGHT*IsometricTile.TILE_HEIGHT*IsometricTile.SCALE <= Gdx.graphics.getHeight())
			y = (Gdx.graphics.getHeight()-HEIGHT*IsometricTile.TILE_HEIGHT*IsometricTile.SCALE)/2;
		mapBorder = new Polygon(mapData[0][0].getTop().add(x, y), mapData[WIDTH-1][0].getRight().add(x,y), mapData[WIDTH-1][HEIGHT-1].getBottom().add(x,y), mapData[0][HEIGHT-1].getLeft().add(x, y));
	}
	
	public void setOffset(float x, float y) {
		float deltaX = x - this.x;
		float deltaY = y - this.y;
		this.x = x;
		this.y = y;
		for(Polygon poly : polys) {
			poly.translate(deltaX,deltaY);
		}
		mapBorder.translate(deltaX, deltaY);
	}
	
	public IsometricMap(int width, int height) {
//		rect = DataLoader.getNinePatch("rect");
		WIDTH = width;
		HEIGHT = height;
		mapData = new IsometricTile[WIDTH][HEIGHT];
		for(int x = 0; x < WIDTH; x++) {
			for(int y = 0; y < HEIGHT; y++) {
				mapData[x][y] = new IsometricTile(x,y,false);
			}
		}
		if(WIDTH*IsometricTile.TILE_WIDTH*IsometricTile.SCALE <= Gdx.graphics.getWidth()/2)
			x = (Gdx.graphics.getWidth()/2-WIDTH*IsometricTile.TILE_WIDTH*IsometricTile.SCALE)/2 + WIDTH*IsometricTile.TILE_WIDTH*IsometricTile.SCALE/2;
		if(HEIGHT*IsometricTile.TILE_HEIGHT*IsometricTile.SCALE <= Gdx.graphics.getHeight()/2)
			y = (Gdx.graphics.getHeight()/2-HEIGHT*IsometricTile.TILE_HEIGHT*IsometricTile.SCALE)/2;
		mapBorder = new Polygon(mapData[0][0].getTop().add(x, y), mapData[WIDTH-1][0].getRight().add(x,y), mapData[WIDTH-1][HEIGHT-1].getBottom().add(x,y), mapData[0][HEIGHT-1].getLeft().add(x, y));
	}
	
	public void render(SpriteBatch batch, float cameraX, float cameraY, float playerX, float playerY) {
		renderSquare(batch, cameraX - x, cameraY - y);
		batch.setColor(Color.WHITE);
//		rect.draw(batch, cameraX, cameraY, screenWidth, screenHeight);
		IsometricTile.select((int)IsometricTile.screenToI(Gdx.input.getX() + cameraX - x,  Gdx.input.getY() + cameraY - y),(int)IsometricTile.screenToJ(Gdx.input.getX() + cameraX - x,  Gdx.input.getY() + cameraY - y));
	}
	
	public ArrayList<Polygon> getPolys() {return polys;}
	public Polygon getMapBorder() {return mapBorder;}
	public int getTilesRendered() {return tilesRendered;}
	public float tileCoordsToX(int x, int y) {
		return IsometricTile.isoToX(x, y) + this.x - IsometricTile.TILE_WIDTH*IsometricTile.SCALE/2;
	}
	
	public float tileCoordsToY(int x, int y) {
		return IsometricTile.isoToY(x, y) + this.y;
	}
	int topLeftX, topLeftY, curWidth, curHeight, curX, curY;
	public void renderSquare(SpriteBatch batch, float xOffset, float yOffset) {
		tilesRendered = 0;
		topLeftX = (int) IsometricTile.screenToI(xOffset, yOffset) - 2;
		topLeftY = (int) IsometricTile.screenToJ(xOffset, yOffset);
		curWidth = 0;
		curHeight = 0;
		curX = topLeftX;
		curY = topLeftY;
		boolean even = true;
		while(curHeight < screenHeight + IsometricTile.TILE_HEIGHT*IsometricTile.SCALE/2 * 4) {
			while(curWidth < screenWidth + IsometricTile.TILE_WIDTH*IsometricTile.SCALE * 2) {
				if(curX >= 0 && curX < WIDTH && curY >= 0 && curY < HEIGHT && mapData[curX][curY] != null) {
					mapData[curX][curY].render(batch, x, y);
					tilesRendered++;
				}
				curX++;
				curY--;
				curWidth += IsometricTile.TILE_WIDTH*IsometricTile.SCALE;
			}
			curWidth = 0;
			curHeight += IsometricTile.TILE_HEIGHT*IsometricTile.SCALE/2;
			if(even) {
				topLeftX++;
			} else {
				topLeftY++;
			} 
			curX = topLeftX;
			curY = topLeftY;
			even = !even;
		}
	}

}