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

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

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

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

void M_ActivityTaskCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ActivityTaskCFG.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_ActivityTaskCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mDesc1 = element->Attribute("Desc1");
		data.mDesc2 = element->Attribute("Desc2");
		data.mCompleteType = element->IntAttribute("CompleteType");
		{
			const char* CompleteCount = element->Attribute("CompleteCount");
			std::vector<std::string> vecCompleteCount;
			boost::split(vecCompleteCount, CompleteCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecCompleteCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecCompleteCount[i].c_str(), &temp))
				{
					data.mCompleteCount.push_back(temp);
				}
			}
		}
		{
			const char* AwardItem = element->Attribute("AwardItem");
			std::vector<std::string> vecAwardItem;
			boost::split(vecAwardItem, AwardItem, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecAwardItem.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecAwardItem[i].c_str(), &temp))
				{
					data.mAwardItem.push_back(temp);
				}
			}
		}
		data.mCompleteParam = element->IntAttribute("CompleteParam");
		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_ActivityTaskCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ActivityTaskCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ActivityTaskCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ActivityTaskCFG");
	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_ActivityTaskCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "Desc1", data.mDesc1);
		LuaCfgHelper::readString(L, "Desc2", data.mDesc2);
		LuaCfgHelper::readInt(L, "CompleteType", data.mCompleteType);
		LuaCfgHelper::readVectorInt(L, "CompleteCount", data.mCompleteCount);
		LuaCfgHelper::readVectorInt(L, "AwardItem", data.mAwardItem);
		LuaCfgHelper::readInt(L, "CompleteParam", data.mCompleteParam);
		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_ActivityTaskCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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