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

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

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

EHDI.GAME.scene.GameScene = function() {
	EHDI.aka.Container.call(this);

	this.bg,
	this.updateFunc,
	this.player,
	this.opponent,
	this.overlay,
	this.playerBoard,
	this.gizmoBoard,
	this.viewModeOverlay,
	this.gameTimer,
	this.playerPointContainer,
	this.gizmoPointContainer,
	this.round = {val: 1, cardsToLeave: 2},
	this.roundLabel,
	this.isPaused = false,
	this.pauseBtn,
	this.super = EHDI.aka.Container,
	this.skipButton,
	this.timeline = new TimelineLite(),
	this.diffContainer;

	this.gameEvents = {
		pause: "PAUSE",
		restart: "RESTART",
		gameOver: "GAMEOVER"
	};

	for(var i in this.gameEvents) {
		EHDI.GAME.eventManager.register(this.gameEvents[i]);
	}

	EHDI.GAME.eventManager.addListener(this.gameEvents["pause"], this.pause.bind(this));
	EHDI.GAME.eventManager.addListener(this.gameEvents["restart"], this.resetGame.bind(this));
	// EHDI.GAME.eventManager.addListener(this.gameEvents["gameOver"], this.gameOver.bind(this));

	EHDI.GAME.CardManager = EHDI.GAME.CardManager || EHDI.GAME.Managers.CardManager.getInstance();
	EHDI.GAME.TimelineManager = EHDI.GAME.TimelineManager || EHDI.GAME.Managers.TimelineManager.getInstance();
	EHDI.GAME.CardAnimationManager = EHDI.GAME.CardAnimationManager || EHDI.GAME.Managers.CardAnimationManager.getInstance();
	EHDI.GAME.CardInteractions = EHDI.GAME.CardInteractions || EHDI.GAME.components.CardInteractions.getInstance();
	EHDI.GAME.EffectList = EHDI.GAME.EffectList || EHDI.GAME.components.EffectList.getInstance();
}

EHDI.GAME.scene.GameScene.prototype = Object.create(EHDI.aka.Container.prototype);

EHDI.GAME.scene.GameScene.prototype.update = function(dt) {
	if(this.pauseBtn.isPaused) {
		this.pause(true);
	} else {
		if(this.isPaused)
			this.pause(false);
	}
}

EHDI.GAME.scene.GameScene.prototype.screenWillAppear = function() {
	// this.bg = new EHDI.aka.Sprite(EHDI.Assets.fetch("parabuilder_bg").texture);
	this.bg = new EHDI.aka.Sprite(EHDI.Assets.images["parabuilder_bg"]);
	EHDI.GAME.utils.scaleToFit(this.bg);
	this.addChild(this.bg);

	this.pauseBtn = new EHDI.components.pauseButton();

  this.skipButton = new EHDI.displays.ButtonContainer("SKIP           ", {fontFamily: "Exo-Bold", fontSize: "24px", fill: 0xFFFFFF}, EHDI.Assets.images["btn_play"]);
  this.skipButton.position.set(EHDI.GAME.sceneManager.getStageWidth()+this.skipButton.width/2, 450);
  this.skipButton.setOnClickFunction(EHDI.GAME.TimelineManager.skip);

	this.overlay = new EHDI.aka.Graphics();
	this.overlay.beginFill(0x000000, 0.3);
	this.overlay.drawRect(0, 0, EHDI.GAME.sceneManager.getStageWidth(), EHDI.GAME.sceneManager.getStageHeight());
	this.overlay.endFill();
	this.overlay.visible = false;

	this.playerBoard = new EHDI.aka.Graphics();
	this.playerBoard.beginFill(0xFF0000, 0.3);
	this.playerBoard.drawRect(760, 250, EHDI.GAME.sceneManager.getStageWidth()*0.5, EHDI.GAME.sceneManager.getStageHeight()*0.5);
	this.playerBoard.endFill();
	this.playerBoard.pivot.set(this.playerBoard.width/2, this.playerBoard.height/2);
	// EHDI.GAME.utils.center(this.boardBox);
	this.playerBoard.visible = false;
	this.addChild(this.playerBoard);

	this.gizmoBoard = new EHDI.aka.Graphics();
	this.gizmoBoard.beginFill(0xFF0000, 0.3);
	this.gizmoBoard.drawRect(260, 350, EHDI.GAME.sceneManager.getStageWidth()*0.5, EHDI.GAME.sceneManager.getStageHeight()*0.5);
	this.gizmoBoard.endFill();
	this.gizmoBoard.pivot.set(this.gizmoBoard.width/2, this.gizmoBoard.height/2);
	// EHDI.GAME.utils.center(this.boardBox);
	this.gizmoBoard.visible = false;
	this.addChild(this.gizmoBoard);

	// this.viewModeOverlay = new EHDI.aka.Sprite(EHDI.Assets.fetch("parabuilder_conpick").texture);
	this.viewModeOverlay = new EHDI.aka.Sprite(EHDI.Assets.images["parabuilder_conpick"]);
	this.viewModeOverlay.visible = false;
	this.viewModeOverlay.interactive = true;
	this.viewModeOverlay.on("pointertap", function(event) { event.stopPropagation(); });

	this.footer = new EHDI.displays.Footer("SHUFFLING...");

	this.player = new EHDI.GAME.components.Player(this.playerBoard);
	this.opponent = new EHDI.GAME.components.Opponent(this, this.gizmoBoard);
	this.opponent.level = -1;

	this.player.enemyBoardCards = this.opponent.boardCards;
	this.opponent.enemyBoardCards = this.player.boardCards;

	this.gizmoPointContainer = new EHDI.components.ScoreManager(this);
	this.gizmoPointContainer.setXY(130, 520);
	this.opponent.setScoreManager(this.gizmoPointContainer);

	this.playerPointContainer = new EHDI.components.ScoreManager(this);
	this.playerPointContainer.setXY(600, 10);
	this.player.setScoreManager(this.playerPointContainer);

	this.roundLabel = new EHDI.displays.InfoContainer("ROUND 1", {fontFamily: "Exo-Bold", fontSize: "100px", fill: 0xFFFFFF});
	EHDI.GAME.utils.center(this.roundLabel);
	this.roundLabel.position.y = -this.roundLabel.height/2;

	this.diffContainer = new EHDI.aka.Container();

	var easyBtn = new EHDI.displays.ButtonContainer("EASY", {fontFamily: "Exo-Bold", fontSize: "24px", fill: 0xFFFFFF},
		    	EHDI.Assets.images["parabuilder_btnplace"], EHDI.Assets.images["parabuilder_btnclicked"], EHDI.Assets.images["parabuilder_btnhover"]);
	easyBtn.position.set(310, 180);
	easyBtn.setOnClickFunction(this.startGame.bind(this, 0));

	var normBtn = new EHDI.displays.ButtonContainer("NORMAL", {fontFamily: "Exo-Bold", fontSize: "24px", fill: 0xFFFFFF},
		    	EHDI.Assets.images["parabuilder_btnplace"], EHDI.Assets.images["parabuilder_btnclicked"], EHDI.Assets.images["parabuilder_btnhover"]);
	normBtn.position.set(512, 180);
	normBtn.setOnClickFunction(this.startGame.bind(this, 1));

	var hardBtn = new EHDI.displays.ButtonContainer("HARD", {fontFamily: "Exo-Bold", fontSize: "24px", fill: 0xFFFFFF},
		    	EHDI.Assets.images["parabuilder_btnplace"], EHDI.Assets.images["parabuilder_btnclicked"], EHDI.Assets.images["parabuilder_btnhover"]);
	hardBtn.position.set(714, 180);
	hardBtn.setOnClickFunction(this.startGame.bind(this, 2));

	this.disableDiff = function(){
		easyBtn.setDisabled(true);
		normBtn.setDisabled(true);
		hardBtn.setDisabled(true);
	}

  this.diffContainer.addChild(easyBtn);
  this.diffContainer.addChild(normBtn);
  this.diffContainer.addChild(hardBtn);

  this.footer.addChild(this.diffContainer);

	this.gameTimer = new EHDI.GAME.Timer(null, 0);

	this.updateFunc = this.update.bind(this);

	EHDI.GAME.CardManager.initCards(this);
}

EHDI.GAME.scene.GameScene.prototype.screenDidAppear = function() {
	this.addChild(this.overlay);
	this.addChild(this.pauseBtn);
	this.addChild(this.viewModeOverlay);
	this.addChild(this.footer);
	this.addChild(this.skipButton);
	EHDI.GAME.utils.bringToFront(this.opponent.gizmoHand);
	this.addChild(this.roundLabel);
	// this.addChild(this.roundContainer);

    if(EHDI.GAME.saveData.isFirstTimePlay){
        var htp = new EHDI.popup.HTPPopUp();
        htp.position.set(EHDI.GAME.sceneManager.getStageWidth() * 0.5,
        EHDI.GAME.sceneManager.getStageHeight() * 0.5);
        EHDI.GAME.sceneManager.pushPopUp(htp,
        {alpha : new EHDI.scene.TransitionParameter(0, 1), duration : 0.1});
    }

	this.showFooter(true, "CHOOSE DIFFICULTY: ");

	EHDI.GAME.updateManager.addFrameListener(this.updateFunc);
}

// EHDI.GAME.scene.GameScene.prototype.screenWillDisappear = function() {
// 	EHDI.GAME.updateManager.removeFrameListener(this.updateFunc);
// 	// this.gameOver();
// }

EHDI.GAME.scene.GameScene.prototype.screenDidDisappear = function() {
	this.pauseBtn.endGame();
	this.pauseBtn.dispose();
	this.viewModeOverlay.on = null;
	EHDI.GAME.updateManager.removeFrameListener(this.updateFunc);
	EHDI.GAME.TimelineManager.dispose();
	TweenMax.killAll();
	this.super.prototype.destroy.call(this, {children: true});
}

EHDI.GAME.scene.GameScene.prototype.pause = function(val) {
	if(val) {
		EHDI.GAME.CardAnimationManager.pause();
		this.opponent.holdThinking();
	} else {
		EHDI.GAME.CardAnimationManager.play();
		this.opponent.startThinking();
	}
	this.isPaused = val;
}

EHDI.GAME.scene.GameScene.prototype.startGame = function(difficulty) {
	if(this.opponent.level > -1) return;
	this.disableDiff();
	this.opponent.level = difficulty;
	this.showFooter(false);
	EHDI.GAME.CardManager.shuffle();
}

EHDI.GAME.scene.GameScene.prototype.resetGame = function() {
	this.round.val = 1;
	this.round.cardsToLeave = 2;
	EHDI.GAME.CardManager.resetGame();
}

EHDI.GAME.scene.GameScene.prototype.gameOver = function() {
	var highScore = EHDI.GAME.HIGH_SCORE || 0;

	this.pauseBtn.endGame();
	// this.pauseBtn.dispose();

	if(highScore <= this.player.points)
		EHDI.GAME.HIGH_SCORE = this.player.points;

	var stars = 0;
	if(this.player.points < 60 && this.player.points > 100)
		stars = 1;
	else if(this.player.points >= 100 && this.player.points <= 140)
		stars = 2;
	else if(this.player.points > 140) {
		if(this.player.points < this.opponent.points)
			stars = 2;
		else
			stars = 3;
	}
	/*
	var popup = new EHDI.popup.PostGamePopUp( stars, this.player.points );
	popup.x = EHDI.GAME.sceneManager.getStageWidth() * 0.5;
	popup.y = EHDI.GAME.sceneManager.getStageHeight() * 0.5;
	EHDI.GAME.sceneManager.pushPopUp( popup );
	popup.highscoreText.text = "High Score: " + EHDI.GAME.HIGH_SCORE;
	
	if(this.player.points > this.opponent.points){
		EHDI.GAME.soundManager.playVO("vo_way_to_go");
		popup.gameOverText.text = "YOU WIN!";
	}else if(this.player.points < this.opponent.points){
		EHDI.GAME.soundManager.playVO("vo_sorry");
		popup.gameOverText.text = "YOU LOSE!";
	}else{
		EHDI.GAME.soundManager.playVO("vo_awesome");
		popup.gameOverText.text = "IT\'S A TIE!";
	}*/

	if(this.player.points > EHDI.GAME.saveData.highScore) {
        EHDI.GAME.saveData.highScore = this.player.points;
        EHDI.sbGame.saveGameData(EHDI.GAME.saveData, "DEFAULT", function(){
            console.log("data saved.");
        });
    }
    EHDI.sbGame.end(this.player.points);
}

EHDI.GAME.scene.GameScene.prototype.showOverlay = function() {
	// EHDI.GAME.utils.bringToFront(this.overlay);
	this.overlay.visible = true;
	this.overlay.interactive = true;
}

EHDI.GAME.scene.GameScene.prototype.setCardSelectionMode = function(val) {
	this.viewModeOverlay.visible = val;
	this.showFooter(val, " ");
	// this.footer.visible = true;
}

EHDI.GAME.scene.GameScene.prototype.showFooter = function(val, text) {
	// EHDI.GAME.utils.resetTimeline(this.timeline);
	if(text)
		this.footer.changeText(text);
	if(val)
		// TweenLite.to(this.footer.position, 0.4, {y: EHDI.GAME.sceneManager.getStageHeight() - this.footer.height});
		TweenMax.to(this.footer.position, 0.4, {y: EHDI.GAME.sceneManager.getStageHeight() - this.footer.height});
	else
		// TweenLite.to(this.footer.position, 0.4, {y: EHDI.GAME.sceneManager.getStageHeight()});
		TweenMax.to(this.footer.position, 0.4, {y: EHDI.GAME.sceneManager.getStageHeight()});

	// EHDI.GAME.Managers.TimelineManager.getInstance().add(this.timeline);
}

EHDI.GAME.scene.GameScene.prototype.showSkipButton = function(val) {
	// EHDI.GAME.utils.resetTimeline(this.timeline);
	var tl = new TimelineLite();

	if(val)
		tl.to(this.skipButton.position, 0.4, {x: EHDI.GAME.sceneManager.getStageWidth() - this.skipButton.width/2});
	else {
		this.skipButton.interactive = false;
		tl.to(this.skipButton.position, 0.4, {x: EHDI.GAME.sceneManager.getStageWidth() + this.skipButton.width/2});
		tl.call((function() {
			this.interactive = true;
		}).bind(this.skipButton))
	}
	// EHDI.GAME.TimelineManager.add(this.timeline);
}

EHDI.GAME.scene.GameScene.prototype.showRound = function(callback) {
	this.roundLabel.setLabel("ROUND "+this.round.val);
	EHDI.GAME.utils.resetTimeline(this.timeline);

	this.timeline.to(this.roundLabel.position, 0.2, {y: 250});
	this.timeline.to(this.roundLabel.position, 0.6, {y: 350});
	this.timeline.to(this.roundLabel.position, 0.2, {y: EHDI.GAME.sceneManager.getStageHeight() + this.roundLabel.height/2});
	this.timeline.set(this.roundLabel.position, {y: -this.roundLabel.height/2});
	this.timeline.call(callback);
	this.timeline.call(this.opponent.nextTurn.bind(this.opponent));

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