#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_TurretSetCFG.h"
#include "FileEncrypt.h"
std::auto_ptr<Fish_TurretSetCFG> Fish_TurretSetCFG::msSingleton(nullptr);

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

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

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

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

void Fish_TurretSetCFG::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_TurretSetCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mSetID = element->IntAttribute("SetID");
		data.mName = element->Attribute("Name");
		data.mMinRate = element->IntAttribute("MinRate");
		data.mMaxRate = element->IntAttribute("MaxRate");
		data.mGrade = element->IntAttribute("Grade");
		data.mVipClear = element->IntAttribute("VipClear");
		data.mVipGet = element->IntAttribute("VipGet");
		{
			const char* ItemID = element->Attribute("ItemID");
			std::vector<std::string> vecItemID;
			boost::split(vecItemID, ItemID, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemID.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemID[i].c_str(), &temp))
				{
					data.mItemID.push_back(temp);
				}
			}
		}
		{
			const char* EffectType = element->Attribute("EffectType");
			std::vector<std::string> vecEffectType;
			boost::split(vecEffectType, EffectType, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecEffectType.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecEffectType[i].c_str(), &temp))
				{
					data.mEffectType.push_back(temp);
				}
			}
		}
		{
			const char* RandomEffect = element->Attribute("RandomEffect");
			std::vector<std::string> vecRandomEffect;
			boost::split(vecRandomEffect, RandomEffect, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecRandomEffect.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecRandomEffect[i].c_str(), &temp))
				{
					data.mRandomEffect.push_back(temp);
				}
			}
		}
		data.mTurretID = element->IntAttribute("TurretID");
		data.mVelocity = element->IntAttribute("Velocity");
		data.mLockingPower = element->IntAttribute("LockingPower");
		data.mRagePower = element->IntAttribute("RagePower");
		data.mSelfBulletID = element->IntAttribute("SelfBulletID");
		data.mOtherBulletID = element->IntAttribute("OtherBulletID");
		data.mPowerBulletID = element->IntAttribute("PowerBulletID");
		data.mAddRate = element->IntAttribute("AddRate");
		data.mMaxPower = element->IntAttribute("MaxPower");
		data.mPowerBuff = element->IntAttribute("PowerBuff");
		data.mFireSpeed = element->IntAttribute("FireSpeed");
		{
			const char* FireItemSpeed = element->Attribute("FireItemSpeed");
			std::vector<std::string> vecFireItemSpeed;
			boost::split(vecFireItemSpeed, FireItemSpeed, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFireItemSpeed.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFireItemSpeed[i].c_str(), &temp))
				{
					data.mFireItemSpeed.push_back(temp);
				}
			}
		}
		data.mRoute = element->IntAttribute("Route");
		data.mTopLevel = element->IntAttribute("TopLevel");
		data.mLvUpItem = element->IntAttribute("LvUpItem");
		data.mLvUpCount = element->IntAttribute("LvUpCount");
		{
			const char* ResetReturnCount = element->Attribute("ResetReturnCount");
			std::vector<std::string> vecResetReturnCount;
			boost::split(vecResetReturnCount, ResetReturnCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecResetReturnCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecResetReturnCount[i].c_str(), &temp))
				{
					data.mResetReturnCount.push_back(temp);
				}
			}
		}
		{
			const char* BatteryLv = element->Attribute("BatteryLv");
			std::vector<std::string> vecBatteryLv;
			boost::split(vecBatteryLv, BatteryLv, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecBatteryLv.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecBatteryLv[i].c_str(), &temp))
				{
					data.mBatteryLv.push_back(temp);
				}
			}
		}
		{
			const char* MultipleUpper = element->Attribute("MultipleUpper");
			std::vector<std::string> vecMultipleUpper;
			boost::split(vecMultipleUpper, MultipleUpper, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecMultipleUpper.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecMultipleUpper[i].c_str(), &temp))
				{
					data.mMultipleUpper.push_back(temp);
				}
			}
		}
		{
			const char* AddValue = element->Attribute("AddValue");
			std::vector<std::string> vecAddValue;
			boost::split(vecAddValue, AddValue, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecAddValue.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecAddValue[i].c_str(), &temp))
				{
					data.mAddValue.push_back(temp);
				}
			}
		}
		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_TurretSetCFG::Load()
{
	Load("../Config/Fish_TurretSetCFG.xml");
}

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