//
//  SuccessScreen.cpp
//  GlobalGameJam15
//
//  Created by Cesar Parent on 25/01/2015.
//  Copyright (c) 2015 Cesar Parent. All rights reserved.
//

#include "SuccessScreen.h"
#include "Defines.h"
#include <math.h>

SuccessScreen::SuccessScreen(Game& game) : Layer(game, kGameWidth/2, kGameHeight/2)
{
	setAnchorPoint(0, 0);
	setPosition(0, 0);
	setBackgroundColor({0x00,0x00,0x00,0xff});
	
	_chest = new SpriteNode("assets/uis/chest_opening.png");
	_chest->setPosition(kGameWidth/4, kGameHeight/4);
	_chest->animate("assets/anim/chest/chest_opening.spt");
	addChild(_chest);
	
	_item = nullptr;
	_label = nullptr;
	_countdown = 800;
}

SuccessScreen::~SuccessScreen()
{
	delete _chest;
	if(_item != nullptr)
	{
		delete _item;
	}
	if(_label != nullptr)
	{
		delete _label;
	}
}

void SuccessScreen::update(int timeInterval)
{
	Layer::update(timeInterval);
	if(_countdown >= 0 && _item == nullptr)
	{
		_countdown -= timeInterval;
	}
	else if(_item == nullptr)
	{
		_item = new SpriteNode("assets/uis/collectors.png");
		int x = (rand() % 4)*kTileSize;
		int y = (rand() % 2)*kTileSize;
		_item->setClipFrame(x, y, kTileSize, kTileSize);
		_item->setXScale(4);
		_item->setYScale(4);
		_item->setAnchorPoint(.5, .5);
		_item->setPosition(kGameWidth/4, kGameHeight/4);
		addChild(_item);
		
		_label = new LabelNode("...what do I do with that?", "assets/uis/font");
		_label->setAnchorPoint(.5, 1);
		_label->setFontSize(30);
		_label->setPosition(kGameWidth/4, (kMapHeight/2)-10);
		addChild(_label);
	}
}


void SuccessScreen::didBecomeChild()
{
	Controls::setDelegate(this);
}

void SuccessScreen::didExitParent()
{
	Controls::setDelegate((ControlDelegate*)_parent);
}

void SuccessScreen::controllerButtonPressBegan(int controller, SDL_GameControllerButton button)
{
	if(_countdown > 0) return;
	if(button == SDL_CONTROLLER_BUTTON_A || button == SDL_CONTROLLER_BUTTON_START)
	{
		_game.popScene();
	}
	else if(button == SDL_CONTROLLER_BUTTON_B)
	{
		_game.quit();
	}
}

void SuccessScreen::keyboardPressBegan(SDL_Scancode key)
{
	if(_countdown > 0) return;
	if(key == SDL_SCANCODE_RETURN)
	{
		_game.popScene();
	}
	else if(key == SDL_SCANCODE_ESCAPE)
	{
		_game.quit();
	}
	else if(key == SDL_SCANCODE_F)
	{
		_game.toggleFullScreen();
	}
}