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

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

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

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

void M_RobotNameCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_RobotNameCFG.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_RobotNameCFGData data;
		data.mindex = element->IntAttribute("index");
		data.mNickName = element->Attribute("NickName");
		data.mPlayerId = element->IntAttribute("PlayerId");
		data.mVip = element->IntAttribute("Vip");
		data.mHead = element->IntAttribute("Head");
		data.mHeadFrame = element->IntAttribute("HeadFrame");
		data.mGold = element->IntAttribute("Gold");
		data.mTurretID = element->IntAttribute("TurretID");
		data.mWingID = element->IntAttribute("WingID");
		{
			const char* Items = element->Attribute("Items");
			std::vector<std::string> vecItems;
			boost::split(vecItems, Items, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItems[i].c_str(), &temp))
				{
					data.mItems.push_back(temp);
				}
			}
		}
		if (mMapData.find(data.mindex) != mMapData.end())std::cout <<"data refind:" << data.mindex << std::endl;
		CCASSERT(mMapData.find(data.mindex) == mMapData.end(), "data.mindex is exists");
		mMapData.insert(std::make_pair(data.mindex, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_RobotNameCFG Loaded. Load Data:%u", mMapData.size());
}

void M_RobotNameCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_RobotNameCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_RobotNameCFG");
	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_RobotNameCFGData data;
		LuaCfgHelper::readInt(L, "index", data.mindex);
		LuaCfgHelper::readString(L, "NickName", data.mNickName);
		LuaCfgHelper::readInt(L, "PlayerId", data.mPlayerId);
		LuaCfgHelper::readInt(L, "Vip", data.mVip);
		LuaCfgHelper::readInt(L, "Head", data.mHead);
		LuaCfgHelper::readInt(L, "HeadFrame", data.mHeadFrame);
		LuaCfgHelper::readInt(L, "Gold", data.mGold);
		LuaCfgHelper::readInt(L, "TurretID", data.mTurretID);
		LuaCfgHelper::readInt(L, "WingID", data.mWingID);
		LuaCfgHelper::readVectorInt(L, "Items", data.mItems);
		if (mMapData.find(data.mindex) != mMapData.end())std::cout <<"data refind:" << data.mindex << std::endl;
		CCASSERT(mMapData.find(data.mindex) == mMapData.end(), "data.mindex is exists");
		mMapData.insert(std::make_pair(data.mindex, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_RobotNameCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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