#include "GPlayerSelectScene.h"

#ifdef _DXLIB
#include "GStaticDataDX.h"
#else
#include "GStaticData.h"
#endif

using namespace GEnum;
using namespace std;

static int ANIMATION_INTERVAL_X = 640 / PLAYER_MAX_NUM;				// 横の間隔

// 上のANIMATION_INTERVAL_Xと同じ式があるが、意味合いが異なるので、そのまま記す事
static int ANIMATION_LEFT = 640 / PLAYER_MAX_NUM / 2 - 96 / 2;
static int ANIMATION_BOTTOM = 400;
GPlayerSelectScene::GPlayerSelectScene() {
	// セットアップ

	for( int i = 0; i < PLAYER_MAX_NUM; i ++ ) {
		m_aryPlayerInfos[i].m_AnimationCounter.getImageInformation(0)->waitFrame = PLAYER_SELECT_ANIMATION_INTERVAL ;
	}
}

GPlayerSelectScene::~GPlayerSelectScene() {
}

void GPlayerSelectScene::init() {
	// ランダム設定
	// 操作している人の分は変更しない
	getStaticData()->m_CommonWork.randomPlayers( true );

	for( int i = 0; i < PLAYER_MAX_NUM; i ++ ) {
		this->m_aryPlayerInfos[i].m_Player.changeType( getStaticData()->m_ObjectManager.m_aryPlayerList[ i ].getType() );
		this->m_aryPlayerInfos[i].m_Player.setFaceDirection( FaceDirection::LEFT );
	}

	// 画面サイズに合わせて調整
	int nCharacterWidth = static_cast<int>(
		m_aryPlayerInfos[0].m_Player.getSprite()->getPair(0)->left->getImageInformation(0)->srcRect.getWidth() );

	// 横の間隔
	ANIMATION_INTERVAL_X = getStaticData()->getSetting()->windowWidth / PLAYER_MAX_NUM;

	// 左端
	ANIMATION_LEFT = getStaticData()->getSetting()->windowWidth / PLAYER_MAX_NUM / 2 - nCharacterWidth / 2;

	// スクロールしない
	HPLScroller* lpScroller = getStaticData()->m_ScrollManager.getScroller(0);
	HPLRect rVirtualRect = lpScroller->getVirtualRect();
	rVirtualRect.setX(0);
	rVirtualRect.setY(0);
	lpScroller->setVirtualRect( rVirtualRect );
}
void GPlayerSelectScene::process() {
#ifdef _DXLIB
#else
	getStaticData()->clearScreen(0,100,150);
#endif

	// 操作している人のプレイヤー
	GPlayer* lpPlayer = &this->m_aryPlayerInfos[ getStaticData()->m_ObjectManager.m_nPlayingIndex ].m_Player;

	// タイトル
	{
		HPLSequence* lpSeq = getStaticData()->getSequence( SpriteType::COMMON, Common::PLAYER_SELECT_TITLE );
#ifdef _DXLIB
		getStaticData()->m_Basic.m_Graphics.draw(
			0,0, lpSeq, 0 );
#else
		HPLSDLImageTool::blitRotozoomSpriteLeftTop( 0, 0, 
			0, lpSeq, getStaticData()->getImageManager(), 
			getStaticData()->getGraphics()->getScreen());
#endif
	}

#ifdef _DXLIB
#else
	HPLSDLKanjiManager* sdlkanji = getStaticData()->getSDLKanjiManager();
#endif

	int DELTA_X = -3;
	int DELTA_Y = -3;
	// プレイヤー表示
	for( int i = 0; i < PlayerType::MAX_NUM; i ++ ) {
		int nLeft = PLAYER_SELECT_LEFT + PLAYER_SELECT_INTERVAL_X * (i% PLAYER_SELECT_COLUMN_PER_ROW);
		int nTop = PLAYER_SELECT_TOP + PLAYER_SELECT_INTERVAL_Y * (i / PLAYER_SELECT_COLUMN_PER_ROW);
		//if( i == PlayerType::EVIGEN ) {

		// 顔も表示
		HPLSequence* lpSeq = getStaticData()->getSequence( SpriteType::COMMON, Common::PLAYER_SELECT_EVIGEN + i );
#ifdef _DXLIB
		getStaticData()->m_Basic.m_Graphics.draw(
			nLeft, nTop, lpSeq, 0 );
#else
		HPLSDLImageTool::blitRotozoomSpriteLeftTop( nLeft, nTop, 
			0, lpSeq, getStaticData()->getImageManager(), getStaticData()->getGraphics()->getScreen() );
#endif
		//}

		if( i == lpPlayer->getType() ) {
			// 選択
			// 枠表示
			HPLSequence* lpSeq = getStaticData()->getSequence( SpriteType::COMMON, Common::PLAYER_SELECT_SELECT );
#ifdef _DXLIB
			getStaticData()->m_Basic.m_Graphics.draw(
				nLeft + DELTA_X, nTop + DELTA_Y, lpSeq, 0 );
#else
			HPLSDLImageTool::blitRotozoomSpriteLeftTop( 
				nLeft + DELTA_X, nTop + DELTA_Y, 
				0, lpSeq, getStaticData()->getImageManager(), getStaticData()->getGraphics()->getScreen() );

			// 名前表示
			sdlkanji->drawString( getStaticData()->getGraphics()->getScreen(),
				PLAYER_SELECT_NAME_LEFT,
				PLAYER_SELECT_NAME_TOP,
				0, 255,255,255, "%s", getStaticData()->m_ObjectManager.m_vecPlayerSettings[i].m_strName.c_str());
#endif
			lpSeq->step();
		}

	}

	// テストアニメーション表示
	for( int i = 0; i < PLAYER_MAX_NUM; i ++ ) {
		HPLSprite* lpSprite = this->m_aryPlayerInfos[i].m_Player.getSprite();

		HPLSequence* lpSequence = lpSprite->getPair( this->m_aryPlayerInfos[i].m_nAnimationSequenceID )->left;

		int nSpriteHeight = lpSequence->getImageInformation(0)->srcRect.getHeight();

		// 表示
		int nLeft = ANIMATION_LEFT + i * ANIMATION_INTERVAL_X;
		int nTop = ANIMATION_BOTTOM - nSpriteHeight;
#ifdef _DXLIB
		getStaticData()->m_Basic.m_Graphics.draw(
			nLeft, nTop, lpSequence );
#else
		HPLSDLImageTool::blitRotozoomSpriteLeftTop( 
			nLeft, nTop, 
			0, lpSequence, getStaticData()->getImageManager(), getStaticData()->getGraphics()->getScreen() );
#endif
		GPlayer* lpTargetPlayer = &this->m_aryPlayerInfos[i].m_Player;
		HPLRect area = lpTargetPlayer->getArea();
		area.setX(nLeft );
		area.setY(nTop );
		lpTargetPlayer->setArea(area);
		// 武器表示
		getStaticData()->m_CommonWork.drawPlayerWeapon( *lpTargetPlayer, nLeft, nTop );
		/*lpTargetPlayer->getType(),
			*lpTargetPlayer->getState(), lpTargetPlayer->m_lpEvigenWeaponSprite,
			lpTargetPlayer->m_lpDurandalSprite, FaceDirection::LEFT,
			nLeft, nTop,
			this->m_aryPlayerInfos[i].m_nAnimationSequenceID);*/

		if( lpSequence->step().isFinishAnimation ) {
			// 強制ループ
			lpSequence->resetCounter();
		}
		if( this->m_aryPlayerInfos[i].m_AnimationCounter.step().isFinishAnimation ) {
			// 一定時間経過
			// 別のアニメーションに変更
			this->m_aryPlayerInfos[i].m_nAnimationSequenceID = 
				HPLMath::nextInt(0, Motion::TAUNT );

			this->m_aryPlayerInfos[i].m_AnimationCounter.reset();
		}
		
	}
	if( getStaticData()->getInputManager()->isPressedNow( GEnum::KeyMap::A ) ) {
		// プレイヤータイプ設定
		getStaticData()->m_ObjectManager.m_aryPlayerList[ getStaticData()->m_ObjectManager.m_nPlayingIndex ].changeType(
			lpPlayer->getType() );
		// ステージ選択へ
		getStaticData()->getSceneManager()->setScene( GEnum::SceneType::STAGE_SELECT );

	}

	///////////////////////////////
	// プレイヤー変更
	int nOldPlayerType = this->m_aryPlayerInfos[ getStaticData()->m_ObjectManager.m_nPlayingIndex ].m_Player.getType();
	int nPlayerType = nOldPlayerType;
	if( getStaticData()->getInputManager()->isPressedNow( KeyMap::RIGHT ) ) {
		// 次へ
		nPlayerType ++;
	}
	if( getStaticData()->getInputManager()->isPressedNow( KeyMap::LEFT ) ) {
		// 前へ
		nPlayerType --;
	}
/*	if( getStaticData()->getInputManager()->isPressedNow( KeyMap::UP ) ) {
		// 次へ
		this->m_nPlayerType ++;
	}
	if( getStaticData()->getInputManager()->isPressedNow( KeyMap::DOWN ) ) {
		// 前へ
		this->m_nPlayerType --;
	}*/

	// 矯正
	// TODO 飛び越えた量だけループさせるべき
	if( nPlayerType >= PlayerType::MAX_NUM ) {
		nPlayerType = 0;
	}

	if( nPlayerType < 0 ) {
		nPlayerType = PlayerType::MAX_NUM - 1;
	}

	if( nOldPlayerType != nPlayerType ) {
		lpPlayer->changeType( nPlayerType );
		this->m_aryPlayerInfos[ getStaticData()->m_ObjectManager.m_nPlayingIndex ].m_AnimationCounter.reset();
	}

	//////////////////// プレイヤー変更えんど
}
void GPlayerSelectScene::quit() {
}
