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

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

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

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

void Fish_OddsCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_OddsCFG.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_OddsCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mFishName = element->Attribute("FishName");
		{
			const char* HitRate = element->Attribute("HitRate");
			std::vector<std::string> vecHitRate;
			boost::split(vecHitRate, HitRate, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRate.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRate[i].c_str(), &temp))
				{
					data.mHitRate.push_back(temp);
				}
			}
		}
		{
			const char* HitRateHeaven = element->Attribute("HitRateHeaven");
			std::vector<std::string> vecHitRateHeaven;
			boost::split(vecHitRateHeaven, HitRateHeaven, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRateHeaven.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRateHeaven[i].c_str(), &temp))
				{
					data.mHitRateHeaven.push_back(temp);
				}
			}
		}
		{
			const char* HitRateHeavenBuff = element->Attribute("HitRateHeavenBuff");
			std::vector<std::string> vecHitRateHeavenBuff;
			boost::split(vecHitRateHeavenBuff, HitRateHeavenBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRateHeavenBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRateHeavenBuff[i].c_str(), &temp))
				{
					data.mHitRateHeavenBuff.push_back(temp);
				}
			}
		}
		{
			const char* HitRateBuff = element->Attribute("HitRateBuff");
			std::vector<std::string> vecHitRateBuff;
			boost::split(vecHitRateBuff, HitRateBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRateBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRateBuff[i].c_str(), &temp))
				{
					data.mHitRateBuff.push_back(temp);
				}
			}
		}
		{
			const char* HitRateDBuff = element->Attribute("HitRateDBuff");
			std::vector<std::string> vecHitRateDBuff;
			boost::split(vecHitRateDBuff, HitRateDBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRateDBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRateDBuff[i].c_str(), &temp))
				{
					data.mHitRateDBuff.push_back(temp);
				}
			}
		}
		{
			const char* HitRatePersonalBuff = element->Attribute("HitRatePersonalBuff");
			std::vector<std::string> vecHitRatePersonalBuff;
			boost::split(vecHitRatePersonalBuff, HitRatePersonalBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRatePersonalBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRatePersonalBuff[i].c_str(), &temp))
				{
					data.mHitRatePersonalBuff.push_back(temp);
				}
			}
		}
		{
			const char* HitRateHell = element->Attribute("HitRateHell");
			std::vector<std::string> vecHitRateHell;
			boost::split(vecHitRateHell, HitRateHell, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRateHell.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRateHell[i].c_str(), &temp))
				{
					data.mHitRateHell.push_back(temp);
				}
			}
		}
		{
			const char* HitRateHellBuff = element->Attribute("HitRateHellBuff");
			std::vector<std::string> vecHitRateHellBuff;
			boost::split(vecHitRateHellBuff, HitRateHellBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecHitRateHellBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecHitRateHellBuff[i].c_str(), &temp))
				{
					data.mHitRateHellBuff.push_back(temp);
				}
			}
		}
		{
			const char* FirstHitBuff = element->Attribute("FirstHitBuff");
			std::vector<std::string> vecFirstHitBuff;
			boost::split(vecFirstHitBuff, FirstHitBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFirstHitBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFirstHitBuff[i].c_str(), &temp))
				{
					data.mFirstHitBuff.push_back(temp);
				}
			}
		}
		data.mStarRate = element->IntAttribute("StarRate");
		{
			const char* DropItems = element->Attribute("DropItems");
			std::vector<std::string> vecDropItems;
			boost::split(vecDropItems, DropItems, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDropItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDropItems[i].c_str(), &temp))
				{
					data.mDropItems.push_back(temp);
				}
			}
		}
		{
			const char* ExtraDrop = element->Attribute("ExtraDrop");
			std::vector<std::string> vecExtraDrop;
			boost::split(vecExtraDrop, ExtraDrop, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecExtraDrop.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecExtraDrop[i].c_str(), &temp))
				{
					data.mExtraDrop.push_back(temp);
				}
			}
		}
		data.mBigPool = element->IntAttribute("BigPool");
		data.mPlayPool = element->IntAttribute("PlayPool");
		data.mNoviceBuff = element->IntAttribute("NoviceBuff");
		data.mRechargeBuff = element->IntAttribute("RechargeBuff");
		{
			const char* FirstBuff = element->Attribute("FirstBuff");
			std::vector<std::string> vecFirstBuff;
			boost::split(vecFirstBuff, FirstBuff, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFirstBuff.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFirstBuff[i].c_str(), &temp))
				{
					data.mFirstBuff.push_back(temp);
				}
			}
		}
		{
			const char* Group = element->Attribute("Group");
			std::vector<std::string> vecGroup;
			boost::split(vecGroup, Group, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGroup.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGroup[i].c_str(), &temp))
				{
					data.mGroup.push_back(temp);
				}
			}
		}
		data.mActivityId = element->IntAttribute("ActivityId");
		{
			const char* ActivityDropItems = element->Attribute("ActivityDropItems");
			std::vector<std::string> vecActivityDropItems;
			boost::split(vecActivityDropItems, ActivityDropItems, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecActivityDropItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecActivityDropItems[i].c_str(), &temp))
				{
					data.mActivityDropItems.push_back(temp);
				}
			}
		}
		{
			const char* rate = element->Attribute("rate");
			std::vector<std::string> vecrate;
			boost::split(vecrate, rate, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecrate.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecrate[i].c_str(), &temp))
				{
					data.mrate.push_back(temp);
				}
			}
		}
		data.mSquama = element->IntAttribute("Squama");
		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_OddsCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_OddsCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_OddsCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_OddsCFG");
	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_OddsCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "FishName", data.mFishName);
		LuaCfgHelper::readVectorInt(L, "HitRate", data.mHitRate);
		LuaCfgHelper::readVectorInt(L, "HitRateHeaven", data.mHitRateHeaven);
		LuaCfgHelper::readVectorInt(L, "HitRateHeavenBuff", data.mHitRateHeavenBuff);
		LuaCfgHelper::readVectorInt(L, "HitRateBuff", data.mHitRateBuff);
		LuaCfgHelper::readVectorInt(L, "HitRateDBuff", data.mHitRateDBuff);
		LuaCfgHelper::readVectorInt(L, "HitRatePersonalBuff", data.mHitRatePersonalBuff);
		LuaCfgHelper::readVectorInt(L, "HitRateHell", data.mHitRateHell);
		LuaCfgHelper::readVectorInt(L, "HitRateHellBuff", data.mHitRateHellBuff);
		LuaCfgHelper::readVectorInt(L, "FirstHitBuff", data.mFirstHitBuff);
		LuaCfgHelper::readInt(L, "StarRate", data.mStarRate);
		LuaCfgHelper::readVectorInt(L, "DropItems", data.mDropItems);
		LuaCfgHelper::readVectorInt(L, "ExtraDrop", data.mExtraDrop);
		LuaCfgHelper::readInt(L, "BigPool", data.mBigPool);
		LuaCfgHelper::readInt(L, "PlayPool", data.mPlayPool);
		LuaCfgHelper::readInt(L, "NoviceBuff", data.mNoviceBuff);
		LuaCfgHelper::readInt(L, "RechargeBuff", data.mRechargeBuff);
		LuaCfgHelper::readVectorInt(L, "FirstBuff", data.mFirstBuff);
		LuaCfgHelper::readVectorInt(L, "Group", data.mGroup);
		LuaCfgHelper::readInt(L, "ActivityId", data.mActivityId);
		LuaCfgHelper::readVectorInt(L, "ActivityDropItems", data.mActivityDropItems);
		LuaCfgHelper::readVectorInt(L, "rate", data.mrate);
		LuaCfgHelper::readInt(L, "Squama", data.mSquama);
		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_OddsCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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