#include "stdafx.h"
#include <cassert>
#include <fstream>
#include <iostream>
#include <iostream>
#include <boost/smart_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include "tinyxml2.h"
#include "Fish_FishCFG.h"
#include "FileEncrypt.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;
}

boost::unordered_map<int, Fish_FishCFGData>& Fish_FishCFG::GetMapData()
{
	return mMapData;
}

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

void Fish_FishCFG::Load(const std::string& path)
{
	std::ifstream readStream(path, std::ios::binary);
	if (!readStream.is_open())
	{
		assert(false);
		return;
	}
	readStream.seekg(0, std::ios::end);
	int fileSize = readStream.tellg();
	boost::shared_array<char> buffer(new char[fileSize+1]);
	buffer.get()[fileSize] = '\0';
	readStream.seekg(0, std::ios::beg);
	readStream.read(buffer.get(), fileSize);
	readStream.close();
	FileEncrypt::decryptBuffer( buffer, fileSize );
	tinyxml2::XMLDocument xmlDoc;
	auto result = xmlDoc.Parse(buffer.get(), fileSize);
	if (result != tinyxml2::XML_SUCCESS)
	{
		assert(false);
		return;
	}
	auto root = xmlDoc.RootElement();
	if (root == NULL)
	{
		assert(false);
		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;
		assert(mMapData.find(data.mID) == mMapData.end());
		mMapData.insert(std::make_pair(data.mID, data));
		element = element->NextSiblingElement();
	}
}

void Fish_FishCFG::Load()
{
	Load("../Config/Fish_FishCFG.xml");
}

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