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

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

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

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

void M_ActivityFeedBackGiftCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityFeedBackGiftCFG.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_ActivityFeedBackGiftCFGData data;
		data.mVipLv = element->IntAttribute("VipLv");
		{
			const char* Gear1 = element->Attribute("Gear1");
			std::vector<std::string> vecGear1;
			boost::split(vecGear1, Gear1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGear1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGear1[i].c_str(), &temp))
				{
					data.mGear1.push_back(temp);
				}
			}
		}
		{
			const char* Gear2 = element->Attribute("Gear2");
			std::vector<std::string> vecGear2;
			boost::split(vecGear2, Gear2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGear2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGear2[i].c_str(), &temp))
				{
					data.mGear2.push_back(temp);
				}
			}
		}
		{
			const char* Gear3 = element->Attribute("Gear3");
			std::vector<std::string> vecGear3;
			boost::split(vecGear3, Gear3, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGear3.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGear3[i].c_str(), &temp))
				{
					data.mGear3.push_back(temp);
				}
			}
		}
		{
			const char* Gear4 = element->Attribute("Gear4");
			std::vector<std::string> vecGear4;
			boost::split(vecGear4, Gear4, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGear4.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGear4[i].c_str(), &temp))
				{
					data.mGear4.push_back(temp);
				}
			}
		}
		{
			const char* Gear5 = element->Attribute("Gear5");
			std::vector<std::string> vecGear5;
			boost::split(vecGear5, Gear5, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGear5.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGear5[i].c_str(), &temp))
				{
					data.mGear5.push_back(temp);
				}
			}
		}
		if (mMapData.find(data.mVipLv) != mMapData.end())std::cout <<"data refind:" << data.mVipLv << std::endl;
		CCASSERT(mMapData.find(data.mVipLv) == mMapData.end(), "data.mVipLv is exists");
		mMapData.insert(std::make_pair(data.mVipLv, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_ActivityFeedBackGiftCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityFeedBackGiftCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityFeedBackGiftCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityFeedBackGiftCFG");
	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_ActivityFeedBackGiftCFGData data;
		LuaCfgHelper::readInt(L, "VipLv", data.mVipLv);
		LuaCfgHelper::readVectorInt(L, "Gear1", data.mGear1);
		LuaCfgHelper::readVectorInt(L, "Gear2", data.mGear2);
		LuaCfgHelper::readVectorInt(L, "Gear3", data.mGear3);
		LuaCfgHelper::readVectorInt(L, "Gear4", data.mGear4);
		LuaCfgHelper::readVectorInt(L, "Gear5", data.mGear5);
		if (mMapData.find(data.mVipLv) != mMapData.end())std::cout <<"data refind:" << data.mVipLv << std::endl;
		CCASSERT(mMapData.find(data.mVipLv) == mMapData.end(), "data.mVipLv is exists");
		mMapData.insert(std::make_pair(data.mVipLv, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_ActivityFeedBackGiftCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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