'use strict';

angular.module('gameBoard.services')
/**
 * @ngdoc service
 * @requires null
 * @name gameBoard.services:cellService
 * @description
 * This service for cell.
 * @function
 * @author Cenk Bircanoglu
 */
	.factory('cellService', function () {

		return {
			/**
			 * @name Cell
			 * @param x
			 * @param y
			 * @param player
			 * @constructor
			 */
			Cell: function (x, y, player) {
				this.x = x;
				this.y = y;
				this.player = player;
				this.active = false;
				this.location = [x, y];
				this.cellNo = x + '' + y;
				this.decoration = "";

				this.activate = function () {
					this.active = true;
				};

				this.addPlayer = function (player) {
					this.player = player;
				};

				this.deactivate = function () {
					this.active = false;
				};

				/**
				 * @function equals
				 * @param other
				 * @returns {boolean}
				 */
				this.equals = function (other) {
					return this.x == other.x && this.y == other.y;
				};

				/**
				 * @function getIcon
				 * @returns {CSSStyleDeclaration.icon|*|icon}
				 */
				this.getIcon = function () {
					return this.occupant ? this.occupant.icon : "";
				};

				/**
				 * @function hasPlayer
				 * @returns {boolean}
				 */
				this.hasPlayer = function () {
					return !(this.player == undefined);
				};

				/**
				 * @function addOccupant
				 * @param obj
				 */
				this.addOccupant = function (obj) {
					this.occupant = obj;
				};

				this.removeOccupant = function () {
					this.occupant = undefined;
				};

				this.removePlayer = function () {
					this.player = undefined;
				};

				/**
				 * @function token
				 * @returns {*|string}
				 */
				this.token = function () {
					return this.player && this.player.token + "Token";
				};

				/**
				 * @function tokenIs
				 * @param token
				 * @returns {*|boolean}
				 */
				this.tokenIs = function (token) {
					return this.player && this.player.token == token;
				};

				/**
				 * @function unowned
				 * @returns {boolean}
				 */
				this.unowned = function () {
					return this.player == undefined;
				};

				/**
				 * @function unoccupied
				 * @returns {undefined}
				 */
				this.unoccupied = function () {
					return this.occupant = undefined;
				};

				/**
				 * @function statusToken
				 * @returns {string}
				 */
				this.statusToken = function () {
					return this.active ? "active" : "inactive";
				};

				/**
				 * @function ownedBy
				 * @param player
				 * @returns {*|boolean}
				 */
				this.ownedBy = function (player) {
					return this.player && player.id == this.player.id;
				};
			}
		};

	});