#include "stdafx.h"
#include "tinyxml2/tinyxml2.h"
#include "LuaCfgHelper.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "M_ActivityMonsterCFG.h"
std::auto_ptr<M_ActivityMonsterCFG> M_ActivityMonsterCFG::msSingleton(nullptr);

int M_ActivityMonsterCFG::GetCount()
{
	return (int)mMapData.size();
}

const M_ActivityMonsterCFGData* M_ActivityMonsterCFG::GetData(int Id)
{
	auto it = mMapData.find(Id);
	if (it != mMapData.end())
	{
		return &it->second;
	}
	return NULL;
}

const std::map<int, M_ActivityMonsterCFGData>& M_ActivityMonsterCFG::GetMapData()
{
	return mMapData;
}

void M_ActivityMonsterCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityMonsterCFG.xml");
	auto result = xmlDoc.Parse(content.c_str(), content.length());
	if (result != tinyxml2::XML_SUCCESS)
	{
		CCLOGERROR("Result:%d", result);
		CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
		return;
	}
	auto root = xmlDoc.RootElement();
	if (root == NULL)
	{
		CCASSERT(false, "root == NULL");
		return;
	}
	auto element = root->FirstChildElement("Data");
	while (element != NULL)
	{
		M_ActivityMonsterCFGData data;
		data.mId = element->IntAttribute("Id");
		data.mMonsterName = element->Attribute("MonsterName");
		data.mMonsterBlood = element->IntAttribute("MonsterBlood");
		data.mStar1 = element->IntAttribute("Star1");
		data.mStar2 = element->IntAttribute("Star2");
		data.mStar3 = element->IntAttribute("Star3");
		data.mFirstKillItem = element->IntAttribute("FirstKillItem");
		data.mFirstKillICount = element->IntAttribute("FirstKillICount");
		data.mKillItem = element->IntAttribute("KillItem");
		data.mKillICount = element->IntAttribute("KillICount");
		data.mResources = element->Attribute("Resources");
		if (mMapData.find(data.mId) != mMapData.end())std::cout <<"data refind:" << data.mId << std::endl;
		CCASSERT(mMapData.find(data.mId) == mMapData.end(), "data.mId is exists");
		mMapData.insert(std::make_pair(data.mId, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_ActivityMonsterCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityMonsterCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityMonsterCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityMonsterCFG");
	CCASSERT(lua_istable(L, -1) == 1, "is not table");
	lua_pushstring(L, "datas");
	lua_gettable(L, -2);
	CCASSERT(lua_istable(L, -1) == 1, "is not table");
	lua_pushnil(L);
	while(lua_next(L, 2))
	{
		CCASSERT(lua_istable(L, -1) == 1, "is not table");
		M_ActivityMonsterCFGData data;
		LuaCfgHelper::readInt(L, "Id", data.mId);
		LuaCfgHelper::readString(L, "MonsterName", data.mMonsterName);
		LuaCfgHelper::readInt(L, "MonsterBlood", data.mMonsterBlood);
		LuaCfgHelper::readInt(L, "Star1", data.mStar1);
		LuaCfgHelper::readInt(L, "Star2", data.mStar2);
		LuaCfgHelper::readInt(L, "Star3", data.mStar3);
		LuaCfgHelper::readInt(L, "FirstKillItem", data.mFirstKillItem);
		LuaCfgHelper::readInt(L, "FirstKillICount", data.mFirstKillICount);
		LuaCfgHelper::readInt(L, "KillItem", data.mKillItem);
		LuaCfgHelper::readInt(L, "KillICount", data.mKillICount);
		LuaCfgHelper::readString(L, "Resources", data.mResources);
		if (mMapData.find(data.mId) != mMapData.end())std::cout <<"data refind:" << data.mId << std::endl;
		CCASSERT(mMapData.find(data.mId) == mMapData.end(), "data.mId is exists");
		mMapData.insert(std::make_pair(data.mId, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_ActivityMonsterCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityMonsterCFG::Reload()
{
	mMapData.clear();
	Load();
}

M_ActivityMonsterCFG* M_ActivityMonsterCFG::GetSingleton()
{
	if (msSingleton.get() == nullptr)
	{
		msSingleton.reset(new M_ActivityMonsterCFG());
	}
	return msSingleton.get();
}

void M_ActivityMonsterCFG::Release()
{
	msSingleton.reset(nullptr);
}
