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

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

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

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

void Fish_IntermediateExchange::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_IntermediateExchange.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)
	{
		Fish_IntermediateExchangeData data;
		data.mID = element->IntAttribute("ID");
		data.mname = element->Attribute("name");
		{
			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);
				}
			}
		}
		{
			const char* fixedReward = element->Attribute("fixedReward");
			std::vector<std::string> vecfixedReward;
			boost::split(vecfixedReward, fixedReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecfixedReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecfixedReward[i].c_str(), &temp))
				{
					data.mfixedReward.push_back(temp);
				}
			}
		}
		{
			const char* probabilityReward = element->Attribute("probabilityReward");
			std::vector<std::string> vecprobabilityReward;
			boost::split(vecprobabilityReward, probabilityReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecprobabilityReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecprobabilityReward[i].c_str(), &temp))
				{
					data.mprobabilityReward.push_back(temp);
				}
			}
		}
		{
			const char* primaryFubao = element->Attribute("primaryFubao");
			std::vector<std::string> vecprimaryFubao;
			boost::split(vecprimaryFubao, primaryFubao, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecprimaryFubao.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecprimaryFubao[i].c_str(), &temp))
				{
					data.mprimaryFubao.push_back(temp);
				}
			}
		}
		{
			const char* NewGiftIntegral = element->Attribute("NewGiftIntegral");
			std::vector<std::string> vecNewGiftIntegral;
			boost::split(vecNewGiftIntegral, NewGiftIntegral, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNewGiftIntegral.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNewGiftIntegral[i].c_str(), &temp))
				{
					data.mNewGiftIntegral.push_back(temp);
				}
			}
		}
		{
			const char* NewIntegral = element->Attribute("NewIntegral");
			std::vector<std::string> vecNewIntegral;
			boost::split(vecNewIntegral, NewIntegral, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNewIntegral.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNewIntegral[i].c_str(), &temp))
				{
					data.mNewIntegral.push_back(temp);
				}
			}
		}
		{
			const char* GiftIntegral = element->Attribute("GiftIntegral");
			std::vector<std::string> vecGiftIntegral;
			boost::split(vecGiftIntegral, GiftIntegral, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGiftIntegral.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGiftIntegral[i].c_str(), &temp))
				{
					data.mGiftIntegral.push_back(temp);
				}
			}
		}
		{
			const char* ComIntegral = element->Attribute("ComIntegral");
			std::vector<std::string> vecComIntegral;
			boost::split(vecComIntegral, ComIntegral, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecComIntegral.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecComIntegral[i].c_str(), &temp))
				{
					data.mComIntegral.push_back(temp);
				}
			}
		}
		{
			const char* NorIntegral = element->Attribute("NorIntegral");
			std::vector<std::string> vecNorIntegral;
			boost::split(vecNorIntegral, NorIntegral, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNorIntegral.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNorIntegral[i].c_str(), &temp))
				{
					data.mNorIntegral.push_back(temp);
				}
			}
		}
		{
			const char* OnListWeight = element->Attribute("OnListWeight");
			std::vector<std::string> vecOnListWeight;
			boost::split(vecOnListWeight, OnListWeight, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOnListWeight.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOnListWeight[i].c_str(), &temp))
				{
					data.mOnListWeight.push_back(temp);
				}
			}
		}
		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("Fish_IntermediateExchange Loaded. Load Data:%u", mMapData.size());
}

void Fish_IntermediateExchange::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_IntermediateExchange");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_IntermediateExchange");
	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");
		Fish_IntermediateExchangeData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "name", data.mname);
		LuaCfgHelper::readVectorInt(L, "itemId", data.mitemId);
		LuaCfgHelper::readVectorInt(L, "itemcount", data.mitemcount);
		LuaCfgHelper::readVectorInt(L, "fixedReward", data.mfixedReward);
		LuaCfgHelper::readVectorInt(L, "probabilityReward", data.mprobabilityReward);
		LuaCfgHelper::readVectorInt(L, "primaryFubao", data.mprimaryFubao);
		LuaCfgHelper::readVectorInt(L, "NewGiftIntegral", data.mNewGiftIntegral);
		LuaCfgHelper::readVectorInt(L, "NewIntegral", data.mNewIntegral);
		LuaCfgHelper::readVectorInt(L, "GiftIntegral", data.mGiftIntegral);
		LuaCfgHelper::readVectorInt(L, "ComIntegral", data.mComIntegral);
		LuaCfgHelper::readVectorInt(L, "NorIntegral", data.mNorIntegral);
		LuaCfgHelper::readVectorInt(L, "OnListWeight", data.mOnListWeight);
		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("Fish_IntermediateExchange Loaded. Load Data:%u", mMapData.size());
}

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

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

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