#include "NormalAI.h"
#include "AttackAction.h"
#include <vector>
#include "BattlerGroup.h"
#include "DataBasedBattler.h"
#include "DataBasedAction.h"

et::NormalAI::NormalAI(mof::DataResourceManager* pDataResourceManager)
: BattleAI(pDataResourceManager)
{
}

et::NormalAI::~NormalAI(){

}


et::Action* et::NormalAI::createAction(DataBasedBattler* pOwn , BattlerGroup* pGroup){
	mof::DataRow targetRow = pOwn->getDataRow();
	//選択可能なアクションの中で最も期待値の高いものを選ぶ
	mof::DataResource monsterAction = m_pDataResourceManager->getResource(_T("data/monster.action.csv"));
	const TCHAR* const table[] = {_T("DirectAttack1") , _T("DirectAttack2") , _T("SpecialAttack1") , _T("SpecialAttack1")};
	int ap = pOwn->getParameter().actionPoint;
	
	int coefficient = -1;
	int selectedIndex = -1;
	mof::DataRow selectedAction;
	mof::DataRow targetAction;

	for(int i = 0 ; i < sizeof(table)/sizeof(table[0]) ; i++){
		mof::tstring action = targetRow.getData(table[i]);
		if(action == _T(""))continue;
		targetAction = mof::DataRow(monsterAction , action.c_str());
		
		int cost = targetAction.getIntegerData( _T("Cost"));
		if(ap >= cost){
			int tmpCoefficient = targetAction.getIntegerData( _T("Coefficient"));
			if(coefficient < tmpCoefficient){
				//選択アクションを更新
				coefficient = tmpCoefficient;
				selectedIndex = i;
				selectedAction = targetAction;
			}
			
		}
	}
	if(selectedIndex == -1)return NULL;

	return new DataBasedAction(pOwn , pGroup->getHeroBattler() ,  selectedAction , selectedIndex);
}

/*
et::Action* et::NormalAI::createAction(DataBasedBattler* pOwn , BattlerGroup* pGroup){
	mof::DataRow targetRow = pOwn->getDataRow();

	//選択可能なアクションの中からランダムで選ぶ
	mof::DataResource monsterAction = m_pDataResourceManager->getResource(_T("data/monster.action.csv"));
	const TCHAR* const table[] = {_T("DirectAttack1") , _T("DirectAttack2") , _T("SpecialAttack1") , _T("SpecialAttack1")};
	int ap = pOwn->getParameter().actionPoint;
	
	mof::DataRow targetAction;

	std::vector<int> selectableList;

	for(int i = 0 ; i < sizeof(table)/sizeof(table[0]) ; i++){
		mof::tstring action = targetRow.getData(table[i]);
		if(action == _T(""))continue;
		targetAction = mof::DataRow(monsterAction , action.c_str());
		
		int cost = targetAction.getIntegerData( _T("Cost"));
		DEBUG_PRINT(cost);
		if(ap >= cost)selectableList.push_back(i);
	}
	if(selectableList.size() == 0)return NULL;

	int selectedIndex = rand() % selectableList.size();
	return new DataBasedAction(pOwn , pGroup->getHeroBattler() ,  
		mof::DataRow(monsterAction , targetRow.getData(table[selectedIndex]).c_str()) , selectedIndex);
}*/