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

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

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

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

void M_SplashScreenCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_SplashScreenCFG.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_SplashScreenCFGData data;
		data.mSplashScreenId = element->IntAttribute("SplashScreenId");
		data.mText1 = element->Attribute("Text1");
		data.mText2 = element->Attribute("Text2");
		data.mText3 = element->Attribute("Text3");
		data.mText4 = element->Attribute("Text4");
		data.mText5 = element->Attribute("Text5");
		data.mRemark = element->Attribute("Remark");
		if (mMapData.find(data.mSplashScreenId) != mMapData.end())std::cout <<"data refind:" << data.mSplashScreenId << std::endl;
		CCASSERT(mMapData.find(data.mSplashScreenId) == mMapData.end(), "data.mSplashScreenId is exists");
		mMapData.insert(std::make_pair(data.mSplashScreenId, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_SplashScreenCFG Loaded. Load Data:%u", mMapData.size());
}

void M_SplashScreenCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_SplashScreenCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_SplashScreenCFG");
	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_SplashScreenCFGData data;
		LuaCfgHelper::readInt(L, "SplashScreenId", data.mSplashScreenId);
		LuaCfgHelper::readString(L, "Text1", data.mText1);
		LuaCfgHelper::readString(L, "Text2", data.mText2);
		LuaCfgHelper::readString(L, "Text3", data.mText3);
		LuaCfgHelper::readString(L, "Text4", data.mText4);
		LuaCfgHelper::readString(L, "Text5", data.mText5);
		LuaCfgHelper::readString(L, "Remark", data.mRemark);
		if (mMapData.find(data.mSplashScreenId) != mMapData.end())std::cout <<"data refind:" << data.mSplashScreenId << std::endl;
		CCASSERT(mMapData.find(data.mSplashScreenId) == mMapData.end(), "data.mSplashScreenId is exists");
		mMapData.insert(std::make_pair(data.mSplashScreenId, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_SplashScreenCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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