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

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

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

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

void M_ExchangeCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ExchangeCFG.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_ExchangeCFGData data;
		data.mChangeId = element->IntAttribute("ChangeId");
		data.mFunction = element->IntAttribute("Function");
		data.moder = element->IntAttribute("oder");
		data.mItemName = element->Attribute("ItemName");
		data.mItemDesc = element->Attribute("ItemDesc");
		data.mVipCondition = element->IntAttribute("VipCondition");
		data.mExchangeType = element->IntAttribute("ExchangeType");
		{
			const char* NeedItemId = element->Attribute("NeedItemId");
			std::vector<std::string> vecNeedItemId;
			boost::split(vecNeedItemId, NeedItemId, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNeedItemId.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNeedItemId[i].c_str(), &temp))
				{
					data.mNeedItemId.push_back(temp);
				}
			}
		}
		{
			const char* NeedItemCount = element->Attribute("NeedItemCount");
			std::vector<std::string> vecNeedItemCount;
			boost::split(vecNeedItemCount, NeedItemCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNeedItemCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNeedItemCount[i].c_str(), &temp))
				{
					data.mNeedItemCount.push_back(temp);
				}
			}
		}
		{
			const char* ItemId = element->Attribute("ItemId");
			std::vector<std::string> vecItemId;
			boost::split(vecItemId, ItemId, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemId.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemId[i].c_str(), &temp))
				{
					data.mItemId.push_back(temp);
				}
			}
		}
		{
			const char* ItemCount = element->Attribute("ItemCount");
			std::vector<std::string> vecItemCount;
			boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
				{
					data.mItemCount.push_back(temp);
				}
			}
		}
		data.mIcon = element->Attribute("Icon");
		data.mTimeType = element->IntAttribute("TimeType");
		data.mExchangeMaxLimit = element->IntAttribute("ExchangeMaxLimit");
		data.mType = element->IntAttribute("Type");
		data.mManageMent = element->IntAttribute("ManageMent");
		data.mManageMentName = element->Attribute("ManageMentName");
		data.mDiscount = element->IntAttribute("Discount");
		data.mReturnContribution = element->IntAttribute("ReturnContribution");
		if (mMapData.find(data.mChangeId) != mMapData.end())std::cout <<"data refind:" << data.mChangeId << std::endl;
		CCASSERT(mMapData.find(data.mChangeId) == mMapData.end(), "data.mChangeId is exists");
		mMapData.insert(std::make_pair(data.mChangeId, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_ExchangeCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ExchangeCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ExchangeCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ExchangeCFG");
	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_ExchangeCFGData data;
		LuaCfgHelper::readInt(L, "ChangeId", data.mChangeId);
		LuaCfgHelper::readInt(L, "Function", data.mFunction);
		LuaCfgHelper::readInt(L, "oder", data.moder);
		LuaCfgHelper::readString(L, "ItemName", data.mItemName);
		LuaCfgHelper::readString(L, "ItemDesc", data.mItemDesc);
		LuaCfgHelper::readInt(L, "VipCondition", data.mVipCondition);
		LuaCfgHelper::readInt(L, "ExchangeType", data.mExchangeType);
		LuaCfgHelper::readVectorInt(L, "NeedItemId", data.mNeedItemId);
		LuaCfgHelper::readVectorInt(L, "NeedItemCount", data.mNeedItemCount);
		LuaCfgHelper::readVectorInt(L, "ItemId", data.mItemId);
		LuaCfgHelper::readVectorInt(L, "ItemCount", data.mItemCount);
		LuaCfgHelper::readString(L, "Icon", data.mIcon);
		LuaCfgHelper::readInt(L, "TimeType", data.mTimeType);
		LuaCfgHelper::readInt(L, "ExchangeMaxLimit", data.mExchangeMaxLimit);
		LuaCfgHelper::readInt(L, "Type", data.mType);
		LuaCfgHelper::readInt(L, "ManageMent", data.mManageMent);
		LuaCfgHelper::readString(L, "ManageMentName", data.mManageMentName);
		LuaCfgHelper::readInt(L, "Discount", data.mDiscount);
		LuaCfgHelper::readInt(L, "ReturnContribution", data.mReturnContribution);
		if (mMapData.find(data.mChangeId) != mMapData.end())std::cout <<"data refind:" << data.mChangeId << std::endl;
		CCASSERT(mMapData.find(data.mChangeId) == mMapData.end(), "data.mChangeId is exists");
		mMapData.insert(std::make_pair(data.mChangeId, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_ExchangeCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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