var combatState = {};

$(".friendly").click(function() {
	if (!$(this).hasClass("dead")){
		$(".moveto").removeClass("moveto").unbind();
		select(".friendly", this);
		$(".enemy").addClass("select").removeClass("unselected");
		combatState.attacker = $(this).attr("id");
		highlightMoveLocation($(this));
	}
})

$(".enemy").click(function() {
	if (!$(this).hasClass("dead")){
		if ($(this).hasClass("select")) {
			$(".moveto").removeClass("moveto").unbind();
			select(".enemy", this);
			combatState.defender = $(this).attr("id");
			action("attack", "updateHealth", combatState.attacker, {"target": $(this).attr("id")})
		}
	}
})

var select = function(css, that) {
	$(css).removeClass("select").addClass("unselected");
	$(that).addClass("selected").removeClass("unselected")
}

var updateHealth = function(results) {
	$(results).each(
		function() {
			if (this.id){
				changeHealth(this);
			} else if (this.result) {
				$("#combat").hide();
				$("#winner").show();
			} else if (this.islog) {
				processLog(this.log);
			}
		}
	)
	
	
	count = 0;
	var dudes = $("#" + combatState.attacker + ", #" + combatState.defender);
	$.doTimeout(200, function() {
		if (count < 5) {
			
			dudes.toggleClass("selected").toggleClass("unselected");
			
			count++;
			return true;
		}
		$(".friendly").addClass("select").removeClass("unselected")
		return false;
	});
}

var moveCallback = function(results) {
	$(results).each(
		function() {
			if (this.id) {
				$("#" + this.id).appendTo("#" + this.x + "c" + this.y);
				changeHealth(this);
			} else if (this.islog) {
				processLog(this.log);
			}
		}
	)
}

var changeHealth = function(soldier) {
	$("#" + soldier.id + " .healthbar-inside").width((soldier.hp / 100) * 34)
	if (soldier.dead) {
		$("#" + soldier.id).addClass("dead");
		$("#" + soldier.id + " img").attr("src", "/images/grey-robot.png");
	}
}

var soldier = function (spec) {
	var that = {};
	
	that.getId = function() {
		return spec.id;
	}
	
	that.getMove = function() {
		return spec.move;
	}
	
	return that;
}

var createSoldiersMap = function(soldiersArray) {
	$(soldiersArray).each(function() {
			soldiersMap[this.getId()] = this;
	});
}

var highlightMoveLocation = function(current) {
	var coords = $(current).parent().attr("id").split("c");
	combatState.mover = $(current).attr("id");
	var x = coords[0] * 1;
	var y = coords[1] * 1;
	var range = soldiersMap[combatState.mover].getMove();
	for(var i = -(range); i <= range; i++) {
		var dx = range - Math.abs(i);
		for (var j = -dx; j <= dx; j++) {
			var moveTarget = $("#" + (x + j) + "c" + (y + i));
			if (moveTarget.length != 0 && moveTarget.children("div").length == 0) {
				moveTarget.addClass("moveto");
				moveTarget.click(moveTo);
			}
		}
	}
}

var moveTo = function() {
	var coords = $(this).attr("id").split("c");
	action("move", "moveCallback", combatState.mover, {"x": coords[0], "y": coords[1]});
	$(".moveto").removeClass("moveto").unbind();
}

var processLog = function(logMessages) {
	var parseRegex = /{([^|]*)\|([^}]*)}/g
	$(logMessages).each(function(idx, ele) {
		ele = ele.replace(parseRegex, "<span class=\"$2\">$1</span>")
		$("#log").append("<div>" + ele + "</div>");
		var height = 0;
		$("#log div").each(function() {height += $(this).height()})
		$("#log").scrollTop(height - 102);
	});
}