var EHDI = EHDI || Object.create(null);

EHDI.GAME = EHDI.GAME || Object.create(null);

EHDI.GAME.components = EHDI.GAME.components || Object.create(null);

EHDI.GAME.components.Opponent = function(container, cardBoard) {
	EHDI.GAME.components.CardPlayer.call(this, cardBoard);

	this.playableCards = [];
	this.moveSpeed = 100;
	this.recentMove;
	this.pointsGained = 0;
	this.moveTimer = new EHDI.GAME.Timer(null, 0);
	this.madeMove = false;
	this.boardPositionY = 230;
	this.level = 0;
	this.ai = new EHDI.GAME.components.OpponentAI(this.playableCards, this.handCards, this.boardCards, this.enemyBoardCards);

	this.gizmoHand = new EHDI.aka.Sprite(EHDI.Assets.images["parabuilder_gizmohand"]);
	this.gizmoHand.position.set(260, EHDI.GAME.sceneManager.getStageHeight());
	this.gizmoHand.anchor.x = 0.5;
	container.addChild(this.gizmoHand);
}

EHDI.GAME.components.Opponent.prototype = Object.create(EHDI.GAME.components.CardPlayer.prototype);

EHDI.GAME.components.Opponent.prototype.chooseFromHand = function() {
	var card;
	this.ai.updateCardSets({playableCards: this.playableCards, handCards: this.handCards, boardCards: this.boardCards, enemyBoardCards: this.enemyBoardCards});
	card = this.ai.chooseFromHand(this.level);
	return card;
}

EHDI.GAME.components.Opponent.prototype.nextMove = function() {
	var card = this.chooseFromHand();

	this.recentMove = card;
	EHDI.GAME.components.CardPlayer.prototype.nextMove.call(this, card);
	this.madeMove = true;
}

EHDI.GAME.components.Opponent.prototype.addCardToBoardAnim = function(card) {
	var positionsY = [340];
	var positionsX = [110];
	var xIndex = 0;
	var yIndex = 0;
	var nextX;
	var lastCard = this.handCards.length <= 0;

	EHDI.GAME.utils.resetTimeline(this.timeline);
	EHDI.GAME.CardInteractions.removeZoomedCard(this.enemyBoardCards);

	if(this.boardCards.length > 3) {
		if(this.boardCards.length > 6) {
			positionsY = [280, 340, 400];
			positionsX = [110, 160, 210];
		} else {
			positionsY = [280, 400];
			positionsX = [110, 160];
		}
	}

	if(card) {
		this.timeline.to(this.gizmoHand.position, 0.5, {x: card.x, y: card.y/*,
			onStart: EHDI.GAME.soundManager.playSFX, onStartParams: ["card_select"]*/
		});
		this.timeline.call(EHDI.GAME.utils.bringToFront, [card]);
	}

	nextX = positionsX[xIndex];
	for(var i = 0; i < this.boardCards.length; i++) {
		var label = (i == this.boardCards.length-1)? "movecard": "";

		this.timeline.to(this.boardCards[i].position, 0.3, {x: nextX, y: positionsY[yIndex],
			onStart: EHDI.GAME.soundManager.playSFX, onStartParams: ["card_place"],
			onComplete: EHDI.GAME.CardInteractions.setOnBoardListeners.bind(this, this.boardCards[i])}, label);
		if(i == this.boardCards.length-1) {
			if(card)
				this.timeline.to(this.gizmoHand.position, 0.2, {x:nextX, y: positionsY[yIndex]/*, onComplete: this.hideGizmoHand.bind(this)*/}, "movecard");
		}

		nextX += 110;
		if((i+1) % 3 == 0) {
			xIndex++;
			nextX = positionsX[xIndex];
			yIndex++;
		}
	}
	if(card)
		this.timeline.to(this.gizmoHand.position, 0.2, {x: 260, y: EHDI.GAME.sceneManager.getStageHeight()});

	EHDI.GAME.TimelineManager.add(this.timeline);
}

EHDI.GAME.components.Opponent.prototype.nextTurn = function() {
	this.madeMove = false;
	this.moveTimer = new EHDI.GAME.Timer(this.nextMove.bind(this), this.moveSpeed);
}

EHDI.GAME.components.Opponent.prototype.skipThinkTime = function() {
	if(!this.madeMove) {
		this.moveTimer.pause();
		this.nextMove();
	}
}

EHDI.GAME.components.Opponent.prototype.startThinking = function() {
	if(this.moveTimer.getTimeLeft() > 0 && !this.madeMove)
		this.moveTimer.start();
}

EHDI.GAME.components.Opponent.prototype.holdThinking = function() {
	this.moveTimer.pause();
}

EHDI.GAME.components.Opponent.prototype.stopThinking = function() {
	if(!this.madeMove) {
		this.moveTimer.pause();
	}
}


EHDI.GAME.components.Opponent.prototype.showRecentMove = function() {
	if(this.recentMove) {
		this.recentMove.setFacedUp();
		// this.points += this.pointsGained;
		EHDI.GAME.CardInteractions.setOnBoardListeners(this.recentMove);
	}
}

EHDI.GAME.components.Opponent.prototype.chooseFromBoard = function(nCards) {
	this.ai.updateCardSets({boardCards: this.boardCards, draftedCards: this.draftedCards});
	var cardsToLeave = this.ai.pickCardsToLeave(this.level, nCards);

	for(var i = 0; i < cardsToLeave.length; i++) {
		this.draftedCards.push(cardsToLeave[i]);
		cardsToLeave[i].setDrafted(true);
		cardsToLeave[i].changeFrame();
	}
	// console.log(this.draftedCards)
	return cardsToLeave;
}

EHDI.GAME.components.Opponent.prototype.hideGizmoHand = function() {
	this.timeline.to(this.gizmoHand.position, 0.2, {x: 260, y: EHDI.GAME.sceneManager.getStageHeight()});
}