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

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

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

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

void M_GuideCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_GuideCFG.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_GuideCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mType = element->IntAttribute("Type");
		{
			const char* SelPos = element->Attribute("SelPos");
			std::vector<std::string> vecSelPos;
			boost::split(vecSelPos, SelPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSelPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSelPos[i].c_str(), &temp))
				{
					data.mSelPos.push_back(temp);
				}
			}
		}
		{
			const char* SelSize = element->Attribute("SelSize");
			std::vector<std::string> vecSelSize;
			boost::split(vecSelSize, SelSize, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSelSize.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSelSize[i].c_str(), &temp))
				{
					data.mSelSize.push_back(temp);
				}
			}
		}
		data.mSelPic = element->Attribute("SelPic");
		data.mTipsDescPic = element->Attribute("TipsDescPic");
		{
			const char* TipsPos = element->Attribute("TipsPos");
			std::vector<std::string> vecTipsPos;
			boost::split(vecTipsPos, TipsPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTipsPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTipsPos[i].c_str(), &temp))
				{
					data.mTipsPos.push_back(temp);
				}
			}
		}
		{
			const char* TipsSize = element->Attribute("TipsSize");
			std::vector<std::string> vecTipsSize;
			boost::split(vecTipsSize, TipsSize, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTipsSize.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTipsSize[i].c_str(), &temp))
				{
					data.mTipsSize.push_back(temp);
				}
			}
		}
		data.mTipsPic = element->Attribute("TipsPic");
		{
			const char* IconPos = element->Attribute("IconPos");
			std::vector<std::string> vecIconPos;
			boost::split(vecIconPos, IconPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecIconPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecIconPos[i].c_str(), &temp))
				{
					data.mIconPos.push_back(temp);
				}
			}
		}
		{
			const char* FingerPos = element->Attribute("FingerPos");
			std::vector<std::string> vecFingerPos;
			boost::split(vecFingerPos, FingerPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFingerPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFingerPos[i].c_str(), &temp))
				{
					data.mFingerPos.push_back(temp);
				}
			}
		}
		data.mName = element->Attribute("Name");
		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_GuideCFG Loaded. Load Data:%u", mMapData.size());
}

void M_GuideCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_GuideCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_GuideCFG");
	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_GuideCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readInt(L, "Type", data.mType);
		LuaCfgHelper::readVectorInt(L, "SelPos", data.mSelPos);
		LuaCfgHelper::readVectorInt(L, "SelSize", data.mSelSize);
		LuaCfgHelper::readString(L, "SelPic", data.mSelPic);
		LuaCfgHelper::readString(L, "TipsDescPic", data.mTipsDescPic);
		LuaCfgHelper::readVectorInt(L, "TipsPos", data.mTipsPos);
		LuaCfgHelper::readVectorInt(L, "TipsSize", data.mTipsSize);
		LuaCfgHelper::readString(L, "TipsPic", data.mTipsPic);
		LuaCfgHelper::readVectorInt(L, "IconPos", data.mIconPos);
		LuaCfgHelper::readVectorInt(L, "FingerPos", data.mFingerPos);
		LuaCfgHelper::readString(L, "Name", data.mName);
		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_GuideCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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