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

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

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

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

void M_ActivityHappySummerBlindBoxCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityHappySummerBlindBoxCFG.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_ActivityHappySummerBlindBoxCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mItemID = element->IntAttribute("ItemID");
		data.mCount = element->IntAttribute("Count");
		data.mRemainder = element->IntAttribute("Remainder");
		data.mBIgPrice = element->IntAttribute("BIgPrice");
		data.mWeight1 = element->IntAttribute("Weight1");
		data.mWeight2 = element->IntAttribute("Weight2");
		data.mWeight3 = element->IntAttribute("Weight3");
		data.mWeight4 = element->IntAttribute("Weight4");
		data.mWeight5 = element->IntAttribute("Weight5");
		data.mWeight6 = element->IntAttribute("Weight6");
		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_ActivityHappySummerBlindBoxCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityHappySummerBlindBoxCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityHappySummerBlindBoxCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityHappySummerBlindBoxCFG");
	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_ActivityHappySummerBlindBoxCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readInt(L, "ItemID", data.mItemID);
		LuaCfgHelper::readInt(L, "Count", data.mCount);
		LuaCfgHelper::readInt(L, "Remainder", data.mRemainder);
		LuaCfgHelper::readInt(L, "BIgPrice", data.mBIgPrice);
		LuaCfgHelper::readInt(L, "Weight1", data.mWeight1);
		LuaCfgHelper::readInt(L, "Weight2", data.mWeight2);
		LuaCfgHelper::readInt(L, "Weight3", data.mWeight3);
		LuaCfgHelper::readInt(L, "Weight4", data.mWeight4);
		LuaCfgHelper::readInt(L, "Weight5", data.mWeight5);
		LuaCfgHelper::readInt(L, "Weight6", data.mWeight6);
		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_ActivityHappySummerBlindBoxCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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