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

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

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

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

void M_CDKEY_GiftCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_CDKEY_GiftCFG.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_CDKEY_GiftCFGData data;
		data.mGiftId = element->IntAttribute("GiftId");
		data.mGiftName = element->Attribute("GiftName");
		data.mGold = element->IntAttribute("Gold");
		data.mGem = element->IntAttribute("Gem");
		data.mDragbonBall = element->IntAttribute("DragbonBall");
		{
			const char* Item = element->Attribute("Item");
			std::vector<std::string> vecItem;
			boost::split(vecItem, Item, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItem.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItem[i].c_str(), &temp))
				{
					data.mItem.push_back(temp);
				}
			}
		}
		if (mMapData.find(data.mGiftId) != mMapData.end())std::cout <<"data refind:" << data.mGiftId << std::endl;
		CCASSERT(mMapData.find(data.mGiftId) == mMapData.end(), "data.mGiftId is exists");
		mMapData.insert(std::make_pair(data.mGiftId, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_CDKEY_GiftCFG Loaded. Load Data:%u", mMapData.size());
}

void M_CDKEY_GiftCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_CDKEY_GiftCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_CDKEY_GiftCFG");
	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_CDKEY_GiftCFGData data;
		LuaCfgHelper::readInt(L, "GiftId", data.mGiftId);
		LuaCfgHelper::readString(L, "GiftName", data.mGiftName);
		LuaCfgHelper::readInt(L, "Gold", data.mGold);
		LuaCfgHelper::readInt(L, "Gem", data.mGem);
		LuaCfgHelper::readInt(L, "DragbonBall", data.mDragbonBall);
		LuaCfgHelper::readVectorInt(L, "Item", data.mItem);
		if (mMapData.find(data.mGiftId) != mMapData.end())std::cout <<"data refind:" << data.mGiftId << std::endl;
		CCASSERT(mMapData.find(data.mGiftId) == mMapData.end(), "data.mGiftId is exists");
		mMapData.insert(std::make_pair(data.mGiftId, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_CDKEY_GiftCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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