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

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

const M_Global_ConfigData* M_Global_Config::GetData(std::string Key)
{
	auto it = mMapData.find(Key);
	if (it != mMapData.end())
	{
		return &it->second;
	}
	return NULL;
}

const std::map<std::string, M_Global_ConfigData>& M_Global_Config::GetMapData()
{
	return mMapData;
}

void M_Global_Config::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_Global_Config.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_Global_ConfigData data;
		data.mKey = element->Attribute("Key");
		data.mDesc = element->Attribute("Desc");
		data.mIntValue = element->IntAttribute("IntValue");
		{
			const char* IntListValue = element->Attribute("IntListValue");
			std::vector<std::string> vecIntListValue;
			boost::split(vecIntListValue, IntListValue, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecIntListValue.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecIntListValue[i].c_str(), &temp))
				{
					data.mIntListValue.push_back(temp);
				}
			}
		}
		data.mStrValue = element->Attribute("StrValue");
		{
			const char* StrListValue = element->Attribute("StrListValue");
			std::vector<std::string> vecStrListValue;
			boost::split(vecStrListValue, StrListValue, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecStrListValue.size(); i++)
			{
				data.mStrListValue.push_back(vecStrListValue[i]);
			}
		}
		data.mBoolValue = element->BoolAttribute("BoolValue");
		if (mMapData.find(data.mKey) != mMapData.end())std::cout <<"data refind:" << data.mKey << std::endl;
		CCASSERT(mMapData.find(data.mKey) == mMapData.end(), "data.mKey is exists");
		mMapData.insert(std::make_pair(data.mKey, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_Global_Config Loaded. Load Data:%u", mMapData.size());
}

void M_Global_Config::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_Global_Config");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_Global_Config");
	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_Global_ConfigData data;
		LuaCfgHelper::readString(L, "Key", data.mKey);
		LuaCfgHelper::readString(L, "Desc", data.mDesc);
		LuaCfgHelper::readInt(L, "IntValue", data.mIntValue);
		LuaCfgHelper::readVectorInt(L, "IntListValue", data.mIntListValue);
		LuaCfgHelper::readString(L, "StrValue", data.mStrValue);
		LuaCfgHelper::readVectorString(L, "StrListValue", data.mStrListValue);
		LuaCfgHelper::readBool(L, "BoolValue", data.mBoolValue);
		if (mMapData.find(data.mKey) != mMapData.end())std::cout <<"data refind:" << data.mKey << std::endl;
		CCASSERT(mMapData.find(data.mKey) == mMapData.end(), "data.mKey is exists");
		mMapData.insert(std::make_pair(data.mKey, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_Global_Config Loaded. Load Data:%u", mMapData.size());
}

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

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

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