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

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

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

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

void Fish_FreeLotteryCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_FreeLotteryCFG.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_FreeLotteryCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mInfo = element->Attribute("Info");
		data.mBaseGold = element->IntAttribute("BaseGold");
		data.mLotteryValue = element->IntAttribute("LotteryValue");
		{
			const char* AwardItem1 = element->Attribute("AwardItem1");
			std::vector<std::string> vecAwardItem1;
			boost::split(vecAwardItem1, AwardItem1, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem1.size(); i++)
			{
				data.mAwardItem1.push_back(vecAwardItem1[i]);
			}
		}
		{
			const char* AwardItem2 = element->Attribute("AwardItem2");
			std::vector<std::string> vecAwardItem2;
			boost::split(vecAwardItem2, AwardItem2, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem2.size(); i++)
			{
				data.mAwardItem2.push_back(vecAwardItem2[i]);
			}
		}
		{
			const char* AwardItem3 = element->Attribute("AwardItem3");
			std::vector<std::string> vecAwardItem3;
			boost::split(vecAwardItem3, AwardItem3, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem3.size(); i++)
			{
				data.mAwardItem3.push_back(vecAwardItem3[i]);
			}
		}
		{
			const char* AwardItem4 = element->Attribute("AwardItem4");
			std::vector<std::string> vecAwardItem4;
			boost::split(vecAwardItem4, AwardItem4, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem4.size(); i++)
			{
				data.mAwardItem4.push_back(vecAwardItem4[i]);
			}
		}
		{
			const char* AwardItem5 = element->Attribute("AwardItem5");
			std::vector<std::string> vecAwardItem5;
			boost::split(vecAwardItem5, AwardItem5, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem5.size(); i++)
			{
				data.mAwardItem5.push_back(vecAwardItem5[i]);
			}
		}
		{
			const char* AwardItem6 = element->Attribute("AwardItem6");
			std::vector<std::string> vecAwardItem6;
			boost::split(vecAwardItem6, AwardItem6, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem6.size(); i++)
			{
				data.mAwardItem6.push_back(vecAwardItem6[i]);
			}
		}
		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_FreeLotteryCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_FreeLotteryCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_FreeLotteryCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_FreeLotteryCFG");
	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_FreeLotteryCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "Info", data.mInfo);
		LuaCfgHelper::readInt(L, "BaseGold", data.mBaseGold);
		LuaCfgHelper::readInt(L, "LotteryValue", data.mLotteryValue);
		LuaCfgHelper::readVectorString(L, "AwardItem1", data.mAwardItem1);
		LuaCfgHelper::readVectorString(L, "AwardItem2", data.mAwardItem2);
		LuaCfgHelper::readVectorString(L, "AwardItem3", data.mAwardItem3);
		LuaCfgHelper::readVectorString(L, "AwardItem4", data.mAwardItem4);
		LuaCfgHelper::readVectorString(L, "AwardItem5", data.mAwardItem5);
		LuaCfgHelper::readVectorString(L, "AwardItem6", data.mAwardItem6);
		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_FreeLotteryCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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