/*
 * Copyright © 2009 Bjoern Guenzel
 */
function HalmaView(halma, halmaController) {
	this.halma = halma;
	this.halmaController = halmaController;

	this.board = $("board");
	makePositioned(this.board);
	this.boardCoordinates = getElementPosition(this.board);
	log("boardCoordinates: "+this.boardCoordinates);
	
}

HalmaView.prototype.createBoard = function(){
	this.tileSize = 29;
	this.spacing = this.tileSize+5;
	
	var width = this.spacing*this.halma.boardType.dimension;

	this.xOffset = 100;//TODO

	setElementDimensions(this.board, new Dimensions(width, width));

	for(var y = 0;y < this.halma.board.length;y++){
		for(var x = 0;x < this.halma.board[y].length;x++){
			var tile = this.halma.getPosition(x,y);
			if(tile != Halma.TILE_ILLEGAL) {
				this.addTile(x, y, Halma.TILE_EMPTY);
				if(tile != Halma.TILE_EMPTY) {
					this.addTile(x, y, tile);
				}
//				var domTile = this.createTile(x, y, tile);
				
			}
		}
	}

	log("board ready");
}

HalmaView.STONE_IMAGE_NAMES = ["blue.png", "lightblue.png", "red.png", "green.png", "purple.png", "yellow.png"];
HalmaView.STONE_IMAGES = map(function(imgName){var img = new Image();img.src = "img/halma/"+imgName;return img;}, HalmaView.STONE_IMAGE_NAMES);

HalmaView.HOLE_IMAGE = new Image();
HalmaView.HOLE_IMAGE.src = "img/halma/hole.png";
HalmaView.HOLE_PUSHED_IMAGE = new Image();
HalmaView.HOLE_PUSHED_IMAGE.src = "img/halma/hole-pushed.png";


HalmaView.prototype.addTile = function(x,y,tile){
	var img = null;
	var type = null;
	var z_index = 0;
	if(Halma.isEmptyTile(tile)) {
		img = "hole.png";
		type = "h";
	} else {
		type = "c";
		img = HalmaView.STONE_IMAGE_NAMES[tile];
		z_index = 1;
	}
	var imgNode = IMG({'src' : 'img/halma/'+img , "id" : type+"_"+x+","+y});
	//log("imgNode: "+imgNode);

	var linkNode = A({'alt' : x+', '+y, 'id' : 'l'+type+'('+x+','+y+')'}, imgNode);
	
	//makePositioned(linkNode);
	var tileX = this.xOffset+x*this.spacing-y*0.5*this.spacing;
	var tileY = y*this.spacing;


	appendChildNodes(this.board, linkNode);

	var coordinates = this.getPixelCoordinates(x,y);

	//log("before setpos, coordinates: "+coordinates.x+", "+coordinates.y);
	setStyle(linkNode, {"position" : "absolute", "z-index" : z_index});
	setElementPosition(linkNode, coordinates);
	connect(linkNode, "onclick", this.halmaController, partial(this.halmaController.clickField, x, y));
}

HalmaView.prototype.getPixelCoordinates = function(x,y) {
	return new Coordinates(this.xOffset+x*this.spacing-y*0.5*this.spacing, y*this.spacing);
}

HalmaView.prototype.colorTiles = function(tiles, imgSrc) {
	forEach(tiles, function(coords){
		var id = 'h_'+coords[0]+','+coords[1];
		log("update tile with id "+id);		
		$(id).src = imgSrc;
	});
}

HalmaView.prototype.hidePossibleMoves = function(moves) {
	log("hide possible moves: "+moves);
	if(moves) {
	    this.colorTiles(moves, HalmaView.HOLE_IMAGE.src);
	}
}

HalmaView.prototype.showPossibleMoves = function(moves) {
	log("show possible moves: "+moves);
	this.colorTiles(moves, HalmaView.HOLE_PUSHED_IMAGE.src);
}

HalmaView.prototype.movePiece = function(src, target, moves, callback) {
	log("movePiece from "+src+" to "+target+", moves: "+moves);
	var piece = $('lc('+src[0]+','+src[1]+')');
	setNodeAttribute(piece, "id",'lc('+target[0]+','+target[1]+')'); //TODO ugly solution to change the id
	log("trying animation, moves: "+moves);

	var visualMoves = map(bind(function(destination){return this.createVisualMove(piece, destination[0], destination[1])}, this), moves.slice(1));
//	log("visualMoves: "+visualMoves.length);

//	new Sequence(visualMoves);

//	var visualMove = this.createVisualMove(piece, target[0], target[1]);
//	var visualMoves = [visualMove];
	new Sequence(visualMoves, {duration: visualMoves.length*0.5, afterFinish: callback});

//	var coordinates = this.getPixelCoordinates(target[0],target[1]);
//	setElementPosition(piece, this.getPixelCoordinates(target[0],target[1]));
	disconnectAll(piece);	
	connect(piece, "onclick", this.halmaController, partial(this.halmaController.clickField, target[0], target[1]));	
}

HalmaView.prototype.createVisualMove = function(piece, i, j) {
    log("createVisualMove, i: "+i+", j: "+j);
    var coordinates = this.getPixelCoordinates(i,j);

    var visualMove =  new Move(piece, {x: coordinates.x, y: coordinates.y, mode: 'absolute', sync: true, duration: 0.5});
    log("visualMove created");
    return visualMove;
};

HalmaView.prototype.updateActivePlayer = function() {
	log("update display of active player");
	for(var i = 0; i< 6;i++){
		if(this.halma.isActivePlayPiece(i)) {
			showElement("player"+i);			
			//removeElementClass("player"+i, "invisible");
		} else {
			hideElement("player"+i);
			//addElementClass("player"+i, "invisible");
		}
	}
	//$("activePlayer").src = HalmaView.STONE_IMAGES[this.halma.activePlayer].src;	
}

HalmaView.prototype.updateWinner = function() {
	if(this.halma.winner != null) {
		alert("winner: player "+this.halma.winner+" (first player is #0)");
	}
}

HalmaView.prototype.addMoveToHistory = function(move) {
	appendChildNodes("log", DIV(repr(move)+","));
}

HalmaView.prototype.setAIMessage = function(message) {
    swapDOM($("aiMessage"), SPAN({id: "aiMessage"}, message));
}

