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

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

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

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

void M_ActivityVIPCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityVIPCFG.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_ActivityVIPCFGData data;
		data.mVipLv = element->IntAttribute("VipLv");
		{
			const char* ActiviyScratchTicket = element->Attribute("ActiviyScratchTicket");
			std::vector<std::string> vecActiviyScratchTicket;
			boost::split(vecActiviyScratchTicket, ActiviyScratchTicket, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecActiviyScratchTicket.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecActiviyScratchTicket[i].c_str(), &temp))
				{
					data.mActiviyScratchTicket.push_back(temp);
				}
			}
		}
		{
			const char* ActiviyAdventure = element->Attribute("ActiviyAdventure");
			std::vector<std::string> vecActiviyAdventure;
			boost::split(vecActiviyAdventure, ActiviyAdventure, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecActiviyAdventure.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecActiviyAdventure[i].c_str(), &temp))
				{
					data.mActiviyAdventure.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_ActivityVIPCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityVIPCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityVIPCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityVIPCFG");
	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_ActivityVIPCFGData data;
		LuaCfgHelper::readInt(L, "VipLv", data.mVipLv);
		LuaCfgHelper::readVectorInt(L, "ActiviyScratchTicket", data.mActiviyScratchTicket);
		LuaCfgHelper::readVectorInt(L, "ActiviyAdventure", data.mActiviyAdventure);
		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_ActivityVIPCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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