package net.phatcode.rel;

import java.awt.Graphics2D;

import net.phatcode.rel.math.Vector2D;
import net.phatcode.rel.utils.ImageAtlas;

public class LevelMap
{
	
	private ImageAtlas tileAtlas;
	
	private int width = 0;
	private int height = 0;
	
	private int tileSize = 32;
	
	private Vector2D camera = new Vector2D();
	
	private int tileX = 0;
	private int tileY = 0;
	
	private int pixelOffsetX = 0;
	private int pixelOffsetY = 0;
	
	
	int map[][];
	
	public LevelMap( int width, int height, int tileSize, ImageAtlas tileAtlas )
	{
		this.tileAtlas = tileAtlas;
		this.width = width;
		this.height = height;
		this.tileSize = tileSize;
		
		this.camera.set( 0, ((height-1) * tileSize) - (Constants.SCREEN_HEIGHT) );
		
		map = new int[width][height];
		
		for( int y = 0; y < height; y++ )	
		{
			for( int x = 0; x < width; x++ )
			{
				map[x][y] = 0;
			}
		}
	}
	
	public void reset()
	{
		this.camera.set( 0, ((height-1) * tileSize) - (Constants.SCREEN_HEIGHT) );
	}
	
	public void fill( int value )
	{
		
		for( int y = 0; y < height; y++ )	
		{
			for( int x = 0; x < width; x++ )
			{
				map[x][y] = value;
			}
		}
	}
	
	public void randomize( int max )
	{
		
		for( int y = 0; y < height; y++ )	
		{
			for( int x = 0; x < width; x++ )
			{
				float i = (float)Math.random();
				if( i < 0.2f )
				{
					map[x][y] = (int)(Math.random() * max) + 1;
				}
				else
				{
					map[x][y] = 0;
				}
			}
		}
	}
	
	public void update( float speed )
	{
		updateMap( speed );
	}
	
	public void updateMap( float speed )
	{
		camera.add( 0, -speed);
		
		tileX = (int)( ((int)camera.x) / tileSize); 
		tileY = (int)( ((int)camera.y) / tileSize); 
		
		pixelOffsetX = (int)( ((int)camera.x) % tileSize );
		pixelOffsetY = (int)( ((int)camera.y) % tileSize );
		
		if ( camera.y < (Constants.SCREEN_HEIGHT) )
		{
			camera.y = Constants.SCREEN_HEIGHT;
		}
		
		
		
	}

	public void renderMap( Screen screen, Graphics2D g )
	{
		
		// calculate number of tiles per row and column
		int screenTileX = Constants.SCREEN_WIDTH / tileSize;
		int screenTileY = Constants.SCREEN_HEIGHT / tileSize;
		
		// we need to draw an extra tile at the bottom and right 
		// since we are scrolling
		for( int y = 0; y < (screenTileY + 2); y++ )
		{
			for( int x = 0; x <= screenTileX; x++ ) 
			{
				int tx = tileX + x;		// get relative tile positions
				int ty = tileY + y;
				int i = map[tx][ty];		// get map index
				if( i > 0 )
				{

					int sx = (x * tileSize) - pixelOffsetX;      //Calculate where to put a
					int sy = (y * tileSize) - pixelOffsetY;      //particular tile
					g.drawImage( tileAtlas.getSprite(i-1), 
							 	 sx,
							 	 sy,
							 	 screen );
			
				}
			}
		}
		
		
	}

	public int getWidth()
	{
		return width;
	}

	public int getHeight()
	{
		return height;
	}

	public int getTileSize()
	{
		return tileSize;
	}

	public Vector2D getCamera()
	{
		return camera;
	}

	public void setWidth(int width)
	{
		this.width = width;
	}

	public void setHeight(int height)
	{
		this.height = height;
	}

	public void setTileSize(int tileSize)
	{
		this.tileSize = tileSize;
	}

	public void setCamera(Vector2D camera)
	{
		this.camera = camera;
	}

	public int getTileX()
	{
		return tileX;
	}

	public int getTileY()
	{
		return tileY;
	}

	public int getPixelOffsetX()
	{
		return pixelOffsetX;
	}

	public int getPixelOffsetY()
	{
		return pixelOffsetY;
	}

	public void setTileX(int tileX)
	{
		this.tileX = tileX;
	}

	public void setTileY(int tileY)
	{
		this.tileY = tileY;
	}

	public void setPixelOffsetX(int pixelOffsetX)
	{
		this.pixelOffsetX = pixelOffsetX;
	}

	public void setPixelOffsetY(int pixelOffsetY)
	{
		this.pixelOffsetY = pixelOffsetY;
	}

	public int getNumImages()
	{
		return tileAtlas.getNumImages();
	}
	
	
}
