ig.module(
	'weltmeister.tile-select'
)
.defines(function(){

wm.TileSelect = ig.Class.extend({
	
	pos: {x:0, y:0},
	
	layer: null,
	selectionBegin: null,
	
	init: function( layer ) {
		this.layer = layer;
	},
	
	
	setPosition: function( x, y ) {
		this.selectionBegin = null;
		var tile = this.layer.currentTile - 1;
		this.pos.x = 
			( x / this.layer.tilesize ).floor() * this.layer.tilesize 
			- ( tile * this.layer.tilesize ).floor() % this.layer.tiles.width;
			
		this.pos.y = 
			( y / this.layer.tilesize ).floor() * this.layer.tilesize 
			- ( tile * this.layer.tilesize / this.layer.tiles.width ).floor() * this.layer.tilesize
			- (tile == -1 ? this.layer.tilesize : 0);
			
		this.pos.x = this.pos.x.limit( 0, ig.system.width - this.layer.tiles.width - (ig.system.width % this.layer.tilesize) );
		this.pos.y = this.pos.y.limit( 0, ig.system.height - this.layer.tiles.height - (ig.system.height % this.layer.tilesize)  );
	},
	
	
	beginSelecting: function( x, y ) {
		this.selectionBegin = {x:x, y:y};
	},
	
		
	endSelecting: function( x, y ) {
		var r = this.getSelectionRect( x, y);
		
		var mw = ( this.layer.tiles.width / this.layer.tilesize ).floor();
		var mh = ( this.layer.tiles.height / this.layer.tilesize ).floor();
		
		var brush = [];
		for( var ty = r.y; ty < r.y+r.h; ty++ ) {
			var row = [];
			for( var tx = r.x; tx < r.x+r.w; tx++ ) {
				if( tx < 0 || ty < 0 || tx >= mw || ty >= mh) {
					row.push( 0 );
				}
				else {
					row.push( ty * ( this.layer.tiles.width / this.layer.tilesize ).floor() + tx + 1 );
				}
			}
			brush.push( row );
		}
		this.selectionBegin = null;
		return brush;
	},
	
	
	getSelectionRect: function( x, y ) {
		var sx = this.selectionBegin ? this.selectionBegin.x : x,
			sy = this.selectionBegin ? this.selectionBegin.y : y;
			
		var
			txb = ( (sx - this.pos.x) / this.layer.tilesize ).floor(),
			tyb = ( (sy - this.pos.y) / this.layer.tilesize ).floor(),
			txe = ( (x - this.pos.x) / this.layer.tilesize ).floor(),
			tye = ( (y - this.pos.y) / this.layer.tilesize ).floor();
		
		return {
			x: Math.min( txb, txe ),
			y: Math.min( tyb, tye ),
			w: Math.abs( txb - txe) + 1,
			h: Math.abs( tyb - tye) + 1
		}
	},	
	
	
	draw: function() {
		ig.system.clear( "rgba(0,0,0,0.8)" ); 
		if( !this.layer.tiles.loaded ) {
			return;
		}
		
		// Tileset
		ig.system.context.lineWidth = 1;
		ig.system.context.strokeStyle = wm.config.colors.secondary;
		ig.system.context.fillStyle = wm.config.colors.clear;
		ig.system.context.fillRect( 
			this.pos.x * ig.system.scale, 
			this.pos.y * ig.system.scale, 
			this.layer.tiles.width * ig.system.scale, 
			this.layer.tiles.height * ig.system.scale
		);
		ig.system.context.strokeRect( 
			this.pos.x * ig.system.scale - 0.5, 
			this.pos.y * ig.system.scale - 0.5, 
			this.layer.tiles.width * ig.system.scale + 1, 
			this.layer.tiles.height * ig.system.scale + 1
		);
		
		this.layer.tiles.draw( this.pos.x, this.pos.y );
		
		// Selected Tile
		var tile = this.layer.currentTile - 1;
		var tx = ( tile * this.layer.tilesize ).floor() % this.layer.tiles.width + this.pos.x;
		var ty = 
			( tile * this.layer.tilesize / this.layer.tiles.width ).floor()
			* this.layer.tilesize + this.pos.y 
			+ (tile == -1 ? this.layer.tilesize : 0);
		
		ig.system.context.lineWidth = 1;
		ig.system.context.strokeStyle = wm.config.colors.highlight;
		ig.system.context.strokeRect( 
			tx * ig.system.scale - 0.5, 
			ty * ig.system.scale - 0.5, 
			this.layer.tilesize * ig.system.scale + 1, 
			this.layer.tilesize * ig.system.scale + 1
		);
	},
	
	
	drawCursor: function( x, y ) {  
		var cx = ( x / this.layer.tilesize ).floor() * this.layer.tilesize;
		var cy = ( y / this.layer.tilesize ).floor() * this.layer.tilesize;
		
		var r = this.getSelectionRect( x, y);
		
		ig.system.context.lineWidth = 1;
		ig.system.context.strokeStyle = wm.config.colors.selection;
		ig.system.context.strokeRect( 
			(r.x * this.layer.tilesize + this.pos.x) * ig.system.scale - 0.5, 
			(r.y * this.layer.tilesize + this.pos.y) * ig.system.scale - 0.5, 
			r.w * this.layer.tilesize * ig.system.scale + 1, 
			r.h * this.layer.tilesize * ig.system.scale + 1
		);
	}
});

});