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

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

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

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

void Fish_LuckyDrawCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_LuckyDrawCFG.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_LuckyDrawCFGData data;
		data.mIndex = element->IntAttribute("Index");
		{
			const char* PinValue = element->Attribute("PinValue");
			std::vector<std::string> vecPinValue;
			boost::split(vecPinValue, PinValue, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPinValue.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPinValue[i].c_str(), &temp))
				{
					data.mPinValue.push_back(temp);
				}
			}
		}
		data.mStarLvl = element->IntAttribute("StarLvl");
		data.mItemType = element->IntAttribute("ItemType");
		data.mItemCount = element->IntAttribute("ItemCount");
		data.mItemValue = element->IntAttribute("ItemValue");
		{
			const char* NormalProb = element->Attribute("NormalProb");
			std::vector<std::string> vecNormalProb;
			boost::split(vecNormalProb, NormalProb, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNormalProb.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNormalProb[i].c_str(), &temp))
				{
					data.mNormalProb.push_back(temp);
				}
			}
		}
		{
			const char* NoChipProb = element->Attribute("NoChipProb");
			std::vector<std::string> vecNoChipProb;
			boost::split(vecNoChipProb, NoChipProb, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNoChipProb.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNoChipProb[i].c_str(), &temp))
				{
					data.mNoChipProb.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("Fish_LuckyDrawCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_LuckyDrawCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_LuckyDrawCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_LuckyDrawCFG");
	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_LuckyDrawCFGData data;
		LuaCfgHelper::readInt(L, "Index", data.mIndex);
		LuaCfgHelper::readVectorInt(L, "PinValue", data.mPinValue);
		LuaCfgHelper::readInt(L, "StarLvl", data.mStarLvl);
		LuaCfgHelper::readInt(L, "ItemType", data.mItemType);
		LuaCfgHelper::readInt(L, "ItemCount", data.mItemCount);
		LuaCfgHelper::readInt(L, "ItemValue", data.mItemValue);
		LuaCfgHelper::readVectorInt(L, "NormalProb", data.mNormalProb);
		LuaCfgHelper::readVectorInt(L, "NoChipProb", data.mNoChipProb);
		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("Fish_LuckyDrawCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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