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

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

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

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

void M_ActivityFeedBackPrizeCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityFeedBackPrizeCFG.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_ActivityFeedBackPrizeCFGData data;
		data.mID = element->IntAttribute("ID");
		{
			const char* PrizeItem1 = element->Attribute("PrizeItem1");
			std::vector<std::string> vecPrizeItem1;
			boost::split(vecPrizeItem1, PrizeItem1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeItem1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeItem1[i].c_str(), &temp))
				{
					data.mPrizeItem1.push_back(temp);
				}
			}
		}
		{
			const char* PrizeCount1 = element->Attribute("PrizeCount1");
			std::vector<std::string> vecPrizeCount1;
			boost::split(vecPrizeCount1, PrizeCount1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeCount1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeCount1[i].c_str(), &temp))
				{
					data.mPrizeCount1.push_back(temp);
				}
			}
		}
		data.mPrizeQuota1 = element->IntAttribute("PrizeQuota1");
		{
			const char* PrizeItem2 = element->Attribute("PrizeItem2");
			std::vector<std::string> vecPrizeItem2;
			boost::split(vecPrizeItem2, PrizeItem2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeItem2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeItem2[i].c_str(), &temp))
				{
					data.mPrizeItem2.push_back(temp);
				}
			}
		}
		{
			const char* PrizeCount2 = element->Attribute("PrizeCount2");
			std::vector<std::string> vecPrizeCount2;
			boost::split(vecPrizeCount2, PrizeCount2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeCount2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeCount2[i].c_str(), &temp))
				{
					data.mPrizeCount2.push_back(temp);
				}
			}
		}
		data.mPrizeQuota2 = element->IntAttribute("PrizeQuota2");
		{
			const char* PrizeItem3 = element->Attribute("PrizeItem3");
			std::vector<std::string> vecPrizeItem3;
			boost::split(vecPrizeItem3, PrizeItem3, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeItem3.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeItem3[i].c_str(), &temp))
				{
					data.mPrizeItem3.push_back(temp);
				}
			}
		}
		{
			const char* PrizeCount3 = element->Attribute("PrizeCount3");
			std::vector<std::string> vecPrizeCount3;
			boost::split(vecPrizeCount3, PrizeCount3, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeCount3.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeCount3[i].c_str(), &temp))
				{
					data.mPrizeCount3.push_back(temp);
				}
			}
		}
		data.mPrizeQuota3 = element->IntAttribute("PrizeQuota3");
		{
			const char* PrizeItem4 = element->Attribute("PrizeItem4");
			std::vector<std::string> vecPrizeItem4;
			boost::split(vecPrizeItem4, PrizeItem4, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeItem4.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeItem4[i].c_str(), &temp))
				{
					data.mPrizeItem4.push_back(temp);
				}
			}
		}
		{
			const char* PrizeCount4 = element->Attribute("PrizeCount4");
			std::vector<std::string> vecPrizeCount4;
			boost::split(vecPrizeCount4, PrizeCount4, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeCount4.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeCount4[i].c_str(), &temp))
				{
					data.mPrizeCount4.push_back(temp);
				}
			}
		}
		data.mPrizeQuota4 = element->IntAttribute("PrizeQuota4");
		{
			const char* PrizeItem5 = element->Attribute("PrizeItem5");
			std::vector<std::string> vecPrizeItem5;
			boost::split(vecPrizeItem5, PrizeItem5, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeItem5.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeItem5[i].c_str(), &temp))
				{
					data.mPrizeItem5.push_back(temp);
				}
			}
		}
		{
			const char* PrizeCount5 = element->Attribute("PrizeCount5");
			std::vector<std::string> vecPrizeCount5;
			boost::split(vecPrizeCount5, PrizeCount5, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPrizeCount5.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPrizeCount5[i].c_str(), &temp))
				{
					data.mPrizeCount5.push_back(temp);
				}
			}
		}
		data.mPrizeQuota5 = element->IntAttribute("PrizeQuota5");
		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_ActivityFeedBackPrizeCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityFeedBackPrizeCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityFeedBackPrizeCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityFeedBackPrizeCFG");
	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_ActivityFeedBackPrizeCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readVectorInt(L, "PrizeItem1", data.mPrizeItem1);
		LuaCfgHelper::readVectorInt(L, "PrizeCount1", data.mPrizeCount1);
		LuaCfgHelper::readInt(L, "PrizeQuota1", data.mPrizeQuota1);
		LuaCfgHelper::readVectorInt(L, "PrizeItem2", data.mPrizeItem2);
		LuaCfgHelper::readVectorInt(L, "PrizeCount2", data.mPrizeCount2);
		LuaCfgHelper::readInt(L, "PrizeQuota2", data.mPrizeQuota2);
		LuaCfgHelper::readVectorInt(L, "PrizeItem3", data.mPrizeItem3);
		LuaCfgHelper::readVectorInt(L, "PrizeCount3", data.mPrizeCount3);
		LuaCfgHelper::readInt(L, "PrizeQuota3", data.mPrizeQuota3);
		LuaCfgHelper::readVectorInt(L, "PrizeItem4", data.mPrizeItem4);
		LuaCfgHelper::readVectorInt(L, "PrizeCount4", data.mPrizeCount4);
		LuaCfgHelper::readInt(L, "PrizeQuota4", data.mPrizeQuota4);
		LuaCfgHelper::readVectorInt(L, "PrizeItem5", data.mPrizeItem5);
		LuaCfgHelper::readVectorInt(L, "PrizeCount5", data.mPrizeCount5);
		LuaCfgHelper::readInt(L, "PrizeQuota5", data.mPrizeQuota5);
		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_ActivityFeedBackPrizeCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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