#include "UGTitleScene.h"

#include "../game/UGStaticData.h"

#include "../player/UGPlayerManager.h"


UGTitleScene::UGTitleScene(){
}

UGTitleScene::~UGTitleScene(){
}
void UGTitleScene::init(){
	UGPlayer* player = staticData->getObjectManager()->getPlayer();
	player->setHPMax(100);
	player->setHP(player->getHPMax());
}

void UGTitleScene::quit(){
}

void UGTitleScene::process(){
	staticData->clearScreen(0,10,0);
	// draw blocks
	staticData->getBlockManager()->draw(staticData->getImageManager(),
		staticData->getGraphics()->getScreen(),
		staticData->getScrollManager()->getScroller(0));
		
	bool isMove = staticData->getGame()->isTheWorld() == false &&
		staticData->getGame()->isMenu() == false &&
		staticData->getGame()->isScenario() == false;
/*

	/////////////////////////////
	// オブジェクトなんたら
	staticData->getObjectManager()->update();
*/
//	staticData->getObjectManager()->getNeutralManager()->process(isMove);

	UGPlayer* player = staticData->getObjectManager()->getPlayer();
//	UGPlayer* aibo = staticData->getObjectManager()->getPlayer()->aibo;

	// drawplayer
	player->draw(staticData->getImageManager());
//	aibo->draw(staticData->getImageManager());

	
	if(isMove){
		// collision check
		UGPlayerManager playerManager;
		// fall and hit to collision
		HPLVector velocityAngle = player->getVelocityAngle();
		HPLVector center = player->getCenter();
		playerManager.collisionCheck(
			staticData->getObjectManager()->getPlayer(),
			&center, &velocityAngle);
		//center.add(velocityAngle);
		//player->setCenter(center);
		//player->setVelocityAngle(velocityAngle);
		
		//velocityAngle = aibo->getVelocityAngle();
		//center = aibo->getCenter();
		//playerManager.collisionCheck(
		//	staticData->getObjectManager()->getPlayer()->aibo,
		//	&center, &velocityAngle);
		//center.add(velocityAngle);
		//aibo->setCenter(center);
		//aibo->setVelocityAngle(velocityAngle);

		// input moving
		velocityAngle = player->getVelocityAngle();
		playerManager.move(
			player, &velocityAngle);
		player->setVelocityAngle(velocityAngle);

		//velocityAngle = aibo->getVelocityAngle();
		//playerManager.move(
		//	aibo, &velocityAngle);
		//aibo->setVelocityAngle(velocityAngle);

		//////
		// attack
		//velocityAngle = player->getVelocityAngle();
		//center = player->getCenter();
		//playerManager.attack(
		//	player, &center, &velocityAngle);
		//center.add(velocityAngle);
		//player->setCenter(center);
		//player->setVelocityAngle(velocityAngle);

		//velocityAngle = aibo->getVelocityAngle();
		//center = aibo->getCenter();
		//playerManager.attack(
		//	aibo, &center, &velocityAngle);
		////center.add(velocityAngle);
		//aibo->setCenter(center);
		//aibo->setVelocityAngle(velocityAngle);
	}
/*	// draw and move enemy
	bool isMove = true;
*/

	///
	// enemy
	staticData->getObjectManager()->getEnemyManager()->process(isMove);
/*	// item
	staticData->getObjectManager()->getItemManager()->process(isMove);


	// shot
	staticData->getObjectManager()->getShotManager()->process(isMove);


	staticData->getObjectManager()->getEffectManager()->process(isMove);

/*
	// draw front blocks(tiles)
*/





	////////
	// adjust scroll
	{
		HPLScroller* scroller = staticData->getScrollManager()->getScroller(0);

		HPLVector scrPoint(
			staticData->getSetting()->windowWidth / 2.f,
			staticData->getSetting()->windowHeight / 2.f);

		bool isAdjust = true;

		// tag
		scroller->matchPoint(
			scrPoint, staticData->getObjectManager()->getPlayer()->getCenter(),
			isAdjust);
	}


	HPLSDLKanjiManager* sdlkanji = staticData->getSDLKanjiManager();
	{
		//////
		// キー操作説明
		int fontID = 0;
		int r = 255, g = 255, b = 0;
		
		float left = 300;
		float top = 50;
		float INTERVAL_Y = 20;

		sdlkanji->drawString(staticData->getGraphics()->getScreen(),
			left, top, fontID, r,g,b,
			"■キー操作");
			top += INTERVAL_Y;
		
		std::list<std::string> lst;
		lst.push_back(HPLString::chrToStr(
			" %s:ザ・ワールド",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::J).c_str()));
		lst.push_back(HPLString::chrToStr(
			" %s:リセット",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::K).c_str()));
/*		lst.push_back(HPLString::chrToStr(
			" %s,%s:左右方向変更",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::LEFT).c_str(),
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::RIGHT).c_str()
			));
		lst.push_back(HPLString::chrToStr(
			" %s,%s:アニメーション切り替え",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::UP).c_str(),
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::DOWN).c_str()
			));
		lst.push_back(HPLString::chrToStr(
			" %s:アニメーション初期化",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::A).c_str()));*/
		/*
		lst.push_back(HPLString::chrToStr(
			" %s:攻撃（おしっぱでちゃああじ！）",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::A).c_str()));
		lst.push_back(HPLString::chrToStr(
			" %s:ジャンプ",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::B).c_str()));
		lst.push_back(HPLString::chrToStr(
			" %s,%s:魔法タイプ切り替え",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::C).c_str(),
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::D).c_str()
			));
		lst.push_back(HPLString::chrToStr(
			" %s:ひぎぃテスト",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::E).c_str()
			));
		lst.push_back(HPLString::chrToStr(
			" %s:魔力MAXで上級魔法",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::F).c_str()
			));
		lst.push_back(HPLString::chrToStr(
			" %s:キャラクタ入れ替え",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::G).c_str()
			));
		lst.push_back(HPLString::chrToStr(
			" %s:リセット",
			staticData->getInputManager()->getStringFromMyKey(UGEnum::KeyMap::K).c_str()
			));
*/

		for(std::list<std::string>::iterator it = lst.begin(); it != lst.end(); it ++){
			sdlkanji->drawString(staticData->getGraphics()->getScreen(),
				left, top, fontID, r,g,b,
				it->c_str());
			top += INTERVAL_Y;
		}
	}
	// 仮：Kキー＝SDLK_eでマップ再読み込み
	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::K)){
		staticData->startFadeOut(-1, 0);
	}

/*	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::LEFT)){
		player->setFaceDirection(FaceDirection::LEFT);
	}
	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::RIGHT)){
		player->setFaceDirection(FaceDirection::RIGHT);
	}
	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::UP)){
		int seqID = player->getSequenceID();
		seqID --;
		if(seqID < 0){
			seqID = UGEnum::Udonge::MAX_AVAILABLE - 1;
		}
		player->setSequenceID(seqID);
	}
	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::DOWN)){
		int seqID = player->getSequenceID();
		seqID ++;
		if(seqID >= UGEnum::Udonge::MAX_AVAILABLE){
			seqID = 0;
		}
		player->setSequenceID(seqID);
	}
	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::A)){
		HPLSequence* seq = player->getSequence();
		seq->resetCounter();
	}*/
/*
	//////
	// 仮：Gキー＝SDLK_dでプレイヤー種類変更
	if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::G)){
		if(player->getType() == PlayerType::Undelsen){
			player->setType(PlayerType::Durandana);
		}else if(player->getType() == PlayerType::Durandana){
			player->setType(PlayerType::Undelsen);
		}
	}


	////
	// メニューキー
	if(staticData->getSceneManager()->getSceneType() == UGEnum::SceneType::Game){
		if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::E)){
			staticData->getGame()->setMenu(staticData->getGame()->isMenu());
			if(staticData->getGame()->isMenu() == true){
				menuSelect = 0;
			}
		}
	}

	//////
	// イベントの実行と表示
	staticData->checkEvent();


	if(staticData->getGame()->isMenu()){
		////
		// 暗転表示
		HPLSequence* seq = staticData->getSequence(UGEnum::SpriteType::Common,
			UGEnum::Common::FadeOut);
		// TODO draw
		HPLSDLImageTool::blitRotozoomSpriteLeftTop(
			0, 0,
			0,
			seq,
			staticData->getImageManager(),
			staticData->getGraphics()->getScreen(),
			200);


		////
		// タイトル表示
		{
			float TITLE_TOP = 10;

			seq = staticData->getSequence(UGEnum::SpriteType::Common,
				UGEnum::Common::Title);
			float w = seq->getImageInformation(0)->srcRect.getWidth();
			float windowW = staticData->getSetting()->windowWidth;
			float left = windowW / 2 - w / 2;
			float top = TITLE_TOP;
		}

		////
		// メニュー表示
		std::vector<std::string> lst;
		lst.push_back(std::string("続ける"));
		lst.push_back(std::string("タイトル"));

		float TOP = 100;
		float LEFT = 200;
		float INTERVAL_Y = 20;

		for(int i = 0; i < lst.size(); i ++){
			float top = TOP + INTERVAL_Y * i;
		}

		/////
		// カーソル表示
		
		/////
		// キー判定
		if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::UP)){
			menuSelect --;
			if(menuSelect < 0){
				menuSelect = lst.size() - 1;
			}
		}
		if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::UP)){
			menuSelect ++;
			if(menuSelect >= lst.size()){
				menuSelect = 0;
			}
		}
		if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::A)){
		}
		if(staticData->getInputManager()->isPressedNow(UGEnum::KeyMap::B)){
			////
			// キャンセル
			staticData->getGame()->setMenu(false);
		}
	}*/

/*	if(staticData->getGame()->isScenario()){
	}*/


/*	//////
	// 仮：score表示
	int fontID = 0;
	int r = 255, g = 255, b = 0;
	
	sdlkanji->drawString(staticData->getGraphics()->getScreen(),
		32, 5, fontID, r,g,b,
		"SCORE:%d (テスト中：上下キーで調整)", (int)staticData->getGame()->getScore());

	////
	// 仮：キーで調整
	if(staticData->getInputManager()->isPressed(EGEnum::KeyMap::UP)){
		staticData->getGame()->addScore(0.5);
	}
	if(staticData->getInputManager()->isPressed(EGEnum::KeyMap::DOWN)){
		staticData->getGame()->addScore(-0.5);
	}

*/
}

