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

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

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

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

void M_ActivityMayDayWarriorCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityMayDayWarriorCFG.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_ActivityMayDayWarriorCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mName = element->Attribute("Name");
		data.mCompleteType = element->IntAttribute("CompleteType");
		{
			const char* CompleteParam = element->Attribute("CompleteParam");
			std::vector<std::string> vecCompleteParam;
			boost::split(vecCompleteParam, CompleteParam, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecCompleteParam.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecCompleteParam[i].c_str(), &temp))
				{
					data.mCompleteParam.push_back(temp);
				}
			}
		}
		data.mCompleteCount = element->IntAttribute("CompleteCount");
		{
			const char* OrdinaryReward = element->Attribute("OrdinaryReward");
			std::vector<std::string> vecOrdinaryReward;
			boost::split(vecOrdinaryReward, OrdinaryReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOrdinaryReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOrdinaryReward[i].c_str(), &temp))
				{
					data.mOrdinaryReward.push_back(temp);
				}
			}
		}
		{
			const char* OrdinaryQuantity = element->Attribute("OrdinaryQuantity");
			std::vector<std::string> vecOrdinaryQuantity;
			boost::split(vecOrdinaryQuantity, OrdinaryQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOrdinaryQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOrdinaryQuantity[i].c_str(), &temp))
				{
					data.mOrdinaryQuantity.push_back(temp);
				}
			}
		}
		{
			const char* LuxuryReward = element->Attribute("LuxuryReward");
			std::vector<std::string> vecLuxuryReward;
			boost::split(vecLuxuryReward, LuxuryReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecLuxuryReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecLuxuryReward[i].c_str(), &temp))
				{
					data.mLuxuryReward.push_back(temp);
				}
			}
		}
		{
			const char* LuxuryQuantity = element->Attribute("LuxuryQuantity");
			std::vector<std::string> vecLuxuryQuantity;
			boost::split(vecLuxuryQuantity, LuxuryQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecLuxuryQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecLuxuryQuantity[i].c_str(), &temp))
				{
					data.mLuxuryQuantity.push_back(temp);
				}
			}
		}
		{
			const char* SupremeReward = element->Attribute("SupremeReward");
			std::vector<std::string> vecSupremeReward;
			boost::split(vecSupremeReward, SupremeReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSupremeReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSupremeReward[i].c_str(), &temp))
				{
					data.mSupremeReward.push_back(temp);
				}
			}
		}
		{
			const char* SupremeQuantity = element->Attribute("SupremeQuantity");
			std::vector<std::string> vecSupremeQuantity;
			boost::split(vecSupremeQuantity, SupremeQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSupremeQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSupremeQuantity[i].c_str(), &temp))
				{
					data.mSupremeQuantity.push_back(temp);
				}
			}
		}
		data.mGoType = element->IntAttribute("GoType");
		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_ActivityMayDayWarriorCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityMayDayWarriorCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityMayDayWarriorCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityMayDayWarriorCFG");
	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_ActivityMayDayWarriorCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "Name", data.mName);
		LuaCfgHelper::readInt(L, "CompleteType", data.mCompleteType);
		LuaCfgHelper::readVectorInt(L, "CompleteParam", data.mCompleteParam);
		LuaCfgHelper::readInt(L, "CompleteCount", data.mCompleteCount);
		LuaCfgHelper::readVectorInt(L, "OrdinaryReward", data.mOrdinaryReward);
		LuaCfgHelper::readVectorInt(L, "OrdinaryQuantity", data.mOrdinaryQuantity);
		LuaCfgHelper::readVectorInt(L, "LuxuryReward", data.mLuxuryReward);
		LuaCfgHelper::readVectorInt(L, "LuxuryQuantity", data.mLuxuryQuantity);
		LuaCfgHelper::readVectorInt(L, "SupremeReward", data.mSupremeReward);
		LuaCfgHelper::readVectorInt(L, "SupremeQuantity", data.mSupremeQuantity);
		LuaCfgHelper::readInt(L, "GoType", data.mGoType);
		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_ActivityMayDayWarriorCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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