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

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

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

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

void M_ActivityDayHuntCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityDayHuntCFG.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_ActivityDayHuntCFGData data;
		data.mID = element->IntAttribute("ID");
		{
			const char* Reward = element->Attribute("Reward");
			std::vector<std::string> vecReward;
			boost::split(vecReward, Reward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecReward[i].c_str(), &temp))
				{
					data.mReward.push_back(temp);
				}
			}
		}
		{
			const char* ExpectHighest = element->Attribute("ExpectHighest");
			std::vector<std::string> vecExpectHighest;
			boost::split(vecExpectHighest, ExpectHighest, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecExpectHighest.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecExpectHighest[i].c_str(), &temp))
				{
					data.mExpectHighest.push_back(temp);
				}
			}
		}
		{
			const char* DisplayItem = element->Attribute("DisplayItem");
			std::vector<std::string> vecDisplayItem;
			boost::split(vecDisplayItem, DisplayItem, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDisplayItem.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDisplayItem[i].c_str(), &temp))
				{
					data.mDisplayItem.push_back(temp);
				}
			}
		}
		{
			const char* DisplayMumber = element->Attribute("DisplayMumber");
			std::vector<std::string> vecDisplayMumber;
			boost::split(vecDisplayMumber, DisplayMumber, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecDisplayMumber.size(); i++)
			{
				data.mDisplayMumber.push_back(vecDisplayMumber[i]);
			}
		}
		data.mDisplayResetMumber = element->IntAttribute("DisplayResetMumber");
		data.mbagId = element->IntAttribute("bagId");
		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_ActivityDayHuntCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityDayHuntCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityDayHuntCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityDayHuntCFG");
	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_ActivityDayHuntCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readVectorInt(L, "Reward", data.mReward);
		LuaCfgHelper::readVectorInt(L, "ExpectHighest", data.mExpectHighest);
		LuaCfgHelper::readVectorInt(L, "DisplayItem", data.mDisplayItem);
		LuaCfgHelper::readVectorString(L, "DisplayMumber", data.mDisplayMumber);
		LuaCfgHelper::readInt(L, "DisplayResetMumber", data.mDisplayResetMumber);
		LuaCfgHelper::readInt(L, "bagId", data.mbagId);
		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_ActivityDayHuntCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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