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

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

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

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

void Fish_FishCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_FishCFG.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_FishCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mFishName = element->Attribute("FishName");
		data.mModelID = element->IntAttribute("ModelID");
		data.mScore = element->IntAttribute("Score");
		data.mFishPoints = element->IntAttribute("FishPoints");
		{
			const char* Dimensity = element->Attribute("Dimensity");
			std::vector<std::string> vecDimensity;
			boost::split(vecDimensity, Dimensity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDimensity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDimensity[i].c_str(), &temp))
				{
					data.mDimensity.push_back(temp);
				}
			}
		}
		data.mType = element->IntAttribute("Type");
		data.mDropGem = element->IntAttribute("DropGem");
		data.mShowRadius = element->IntAttribute("ShowRadius");
		data.mLayer = element->IntAttribute("Layer");
		data.mCanNetHit = element->BoolAttribute("CanNetHit");
		{
			const char* ServerDieParam = element->Attribute("ServerDieParam");
			std::vector<std::string> vecServerDieParam;
			boost::split(vecServerDieParam, ServerDieParam, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecServerDieParam.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecServerDieParam[i].c_str(), &temp))
				{
					data.mServerDieParam.push_back(temp);
				}
			}
		}
		{
			const char* DieEffectParam = element->Attribute("DieEffectParam");
			std::vector<std::string> vecDieEffectParam;
			boost::split(vecDieEffectParam, DieEffectParam, boost::is_any_of(","));
			float temp;
			for (unsigned int i = 0; i < vecDieEffectParam.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToFloat(vecDieEffectParam[i].c_str(), &temp))
				{
					data.mDieEffectParam.push_back(temp);
				}
			}
		}
		data.mDieEffect = element->IntAttribute("DieEffect");
		data.mShockScreen = element->BoolAttribute("ShockScreen");
		data.mDropGold = element->IntAttribute("DropGold");
		data.mEffectType = element->IntAttribute("EffectType");
		data.mGoldPlate = element->IntAttribute("GoldPlate");
		data.mScreenPlate = element->IntAttribute("ScreenPlate");
		{
			const char* DieSoundID = element->Attribute("DieSoundID");
			std::vector<std::string> vecDieSoundID;
			boost::split(vecDieSoundID, DieSoundID, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDieSoundID.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDieSoundID[i].c_str(), &temp))
				{
					data.mDieSoundID.push_back(temp);
				}
			}
		}
		data.mShowBook = element->BoolAttribute("ShowBook");
		data.mBookIcon = element->Attribute("BookIcon");
		data.mBookScale = element->FloatAttribute("BookScale");
		data.mBookName = element->Attribute("BookName");
		data.mBookInfo = element->Attribute("BookInfo");
		data.mIsBOSS = element->BoolAttribute("IsBOSS");
		data.mIsFrozen = element->BoolAttribute("IsFrozen");
		data.mIsLottery = element->BoolAttribute("IsLottery");
		data.mShowStar = element->BoolAttribute("ShowStar");
		data.mRadius = element->FloatAttribute("Radius");
		data.mDisplayItem = element->IntAttribute("DisplayItem");
		{
			const char* ItemBubble = element->Attribute("ItemBubble");
			std::vector<std::string> vecItemBubble;
			boost::split(vecItemBubble, ItemBubble, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemBubble.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemBubble[i].c_str(), &temp))
				{
					data.mItemBubble.push_back(temp);
				}
			}
		}
		{
			const char* ChatBubble = element->Attribute("ChatBubble");
			std::vector<std::string> vecChatBubble;
			boost::split(vecChatBubble, ChatBubble, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecChatBubble.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecChatBubble[i].c_str(), &temp))
				{
					data.mChatBubble.push_back(temp);
				}
			}
		}
		{
			const char* ChatOffset = element->Attribute("ChatOffset");
			std::vector<std::string> vecChatOffset;
			boost::split(vecChatOffset, ChatOffset, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecChatOffset.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecChatOffset[i].c_str(), &temp))
				{
					data.mChatOffset.push_back(temp);
				}
			}
		}
		data.mItemBubbleScale = element->FloatAttribute("ItemBubbleScale");
		data.mCanBomb = element->BoolAttribute("CanBomb");
		data.mShowIndex = element->IntAttribute("ShowIndex");
		data.mShowType = element->IntAttribute("ShowType");
		data.mShowScore = element->Attribute("ShowScore");
		data.mBombRange = element->IntAttribute("BombRange");
		data.mCanHitByBulletHead = element->Attribute("CanHitByBulletHead");
		data.mScoreType = element->IntAttribute("ScoreType");
		{
			const char* TimesRange = element->Attribute("TimesRange");
			std::vector<std::string> vecTimesRange;
			boost::split(vecTimesRange, TimesRange, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTimesRange.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTimesRange[i].c_str(), &temp))
				{
					data.mTimesRange.push_back(temp);
				}
			}
		}
		data.mKillAndSendScore = element->IntAttribute("KillAndSendScore");
		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_FishCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_FishCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_FishCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_FishCFG");
	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_FishCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "FishName", data.mFishName);
		LuaCfgHelper::readInt(L, "ModelID", data.mModelID);
		LuaCfgHelper::readInt(L, "Score", data.mScore);
		LuaCfgHelper::readInt(L, "FishPoints", data.mFishPoints);
		LuaCfgHelper::readVectorInt(L, "Dimensity", data.mDimensity);
		LuaCfgHelper::readInt(L, "Type", data.mType);
		LuaCfgHelper::readInt(L, "DropGem", data.mDropGem);
		LuaCfgHelper::readInt(L, "ShowRadius", data.mShowRadius);
		LuaCfgHelper::readInt(L, "Layer", data.mLayer);
		LuaCfgHelper::readBool(L, "CanNetHit", data.mCanNetHit);
		LuaCfgHelper::readVectorInt(L, "ServerDieParam", data.mServerDieParam);
		LuaCfgHelper::readVectorFloat(L, "DieEffectParam", data.mDieEffectParam);
		LuaCfgHelper::readInt(L, "DieEffect", data.mDieEffect);
		LuaCfgHelper::readBool(L, "ShockScreen", data.mShockScreen);
		LuaCfgHelper::readInt(L, "DropGold", data.mDropGold);
		LuaCfgHelper::readInt(L, "EffectType", data.mEffectType);
		LuaCfgHelper::readInt(L, "GoldPlate", data.mGoldPlate);
		LuaCfgHelper::readInt(L, "ScreenPlate", data.mScreenPlate);
		LuaCfgHelper::readVectorInt(L, "DieSoundID", data.mDieSoundID);
		LuaCfgHelper::readBool(L, "ShowBook", data.mShowBook);
		LuaCfgHelper::readString(L, "BookIcon", data.mBookIcon);
		LuaCfgHelper::readFloat(L, "BookScale", data.mBookScale);
		LuaCfgHelper::readString(L, "BookName", data.mBookName);
		LuaCfgHelper::readString(L, "BookInfo", data.mBookInfo);
		LuaCfgHelper::readBool(L, "IsBOSS", data.mIsBOSS);
		LuaCfgHelper::readBool(L, "IsFrozen", data.mIsFrozen);
		LuaCfgHelper::readBool(L, "IsLottery", data.mIsLottery);
		LuaCfgHelper::readBool(L, "ShowStar", data.mShowStar);
		LuaCfgHelper::readFloat(L, "Radius", data.mRadius);
		LuaCfgHelper::readInt(L, "DisplayItem", data.mDisplayItem);
		LuaCfgHelper::readVectorInt(L, "ItemBubble", data.mItemBubble);
		LuaCfgHelper::readVectorInt(L, "ChatBubble", data.mChatBubble);
		LuaCfgHelper::readVectorInt(L, "ChatOffset", data.mChatOffset);
		LuaCfgHelper::readFloat(L, "ItemBubbleScale", data.mItemBubbleScale);
		LuaCfgHelper::readBool(L, "CanBomb", data.mCanBomb);
		LuaCfgHelper::readInt(L, "ShowIndex", data.mShowIndex);
		LuaCfgHelper::readInt(L, "ShowType", data.mShowType);
		LuaCfgHelper::readString(L, "ShowScore", data.mShowScore);
		LuaCfgHelper::readInt(L, "BombRange", data.mBombRange);
		LuaCfgHelper::readString(L, "CanHitByBulletHead", data.mCanHitByBulletHead);
		LuaCfgHelper::readInt(L, "ScoreType", data.mScoreType);
		LuaCfgHelper::readVectorInt(L, "TimesRange", data.mTimesRange);
		LuaCfgHelper::readInt(L, "KillAndSendScore", data.mKillAndSendScore);
		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_FishCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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