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

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

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

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

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

void Fish_TurretLevelCFG::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_TurretLevelCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mOpenRate = element->IntAttribute("OpenRate");
		data.mAwardGold = element->IntAttribute("AwardGold");
		data.mOneTickUp = element->IntAttribute("OneTickUp");
		data.mShotsBuff = element->IntAttribute("ShotsBuff");
		data.mLevelUpIcon = element->Attribute("LevelUpIcon");
		data.mUpType = element->IntAttribute("UpType");
		data.mUpRate = element->IntAttribute("UpRate");
		data.mNeedLucky = element->IntAttribute("NeedLucky");
		{
			const char* NeedItems = element->Attribute("NeedItems");
			std::vector<std::string> vecNeedItems;
			boost::split(vecNeedItems, NeedItems, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNeedItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNeedItems[i].c_str(), &temp))
				{
					data.mNeedItems.push_back(temp);
				}
			}
		}
		{
			const char* NeedCount = element->Attribute("NeedCount");
			std::vector<std::string> vecNeedCount;
			boost::split(vecNeedCount, NeedCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNeedCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNeedCount[i].c_str(), &temp))
				{
					data.mNeedCount.push_back(temp);
				}
			}
		}
		{
			const char* ExtraUpgrade = element->Attribute("ExtraUpgrade");
			std::vector<std::string> vecExtraUpgrade;
			boost::split(vecExtraUpgrade, ExtraUpgrade, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecExtraUpgrade.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecExtraUpgrade[i].c_str(), &temp))
				{
					data.mExtraUpgrade.push_back(temp);
				}
			}
		}
		data.mDropAttenuates = element->IntAttribute("DropAttenuates");
		{
			const char* DailyQuestGroup = element->Attribute("DailyQuestGroup");
			std::vector<std::string> vecDailyQuestGroup;
			boost::split(vecDailyQuestGroup, DailyQuestGroup, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDailyQuestGroup.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDailyQuestGroup[i].c_str(), &temp))
				{
					data.mDailyQuestGroup.push_back(temp);
				}
			}
		}
		data.mDropLimit = element->IntAttribute("DropLimit");
		{
			const char* TaskItems = element->Attribute("TaskItems");
			std::vector<std::string> vecTaskItems;
			boost::split(vecTaskItems, TaskItems, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTaskItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTaskItems[i].c_str(), &temp))
				{
					data.mTaskItems.push_back(temp);
				}
			}
		}
		{
			const char* QuantityOfArticles = element->Attribute("QuantityOfArticles");
			std::vector<std::string> vecQuantityOfArticles;
			boost::split(vecQuantityOfArticles, QuantityOfArticles, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecQuantityOfArticles.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecQuantityOfArticles[i].c_str(), &temp))
				{
					data.mQuantityOfArticles.push_back(temp);
				}
			}
		}
		data.mTaskDisplay = element->IntAttribute("TaskDisplay");
		{
			const char* UnlockDisplayID = element->Attribute("UnlockDisplayID");
			std::vector<std::string> vecUnlockDisplayID;
			boost::split(vecUnlockDisplayID, UnlockDisplayID, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecUnlockDisplayID.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecUnlockDisplayID[i].c_str(), &temp))
				{
					data.mUnlockDisplayID.push_back(temp);
				}
			}
		}
		{
			const char* UnlockDisplayMasks = element->Attribute("UnlockDisplayMasks");
			std::vector<std::string> vecUnlockDisplayMasks;
			boost::split(vecUnlockDisplayMasks, UnlockDisplayMasks, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecUnlockDisplayMasks.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecUnlockDisplayMasks[i].c_str(), &temp))
				{
					data.mUnlockDisplayMasks.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_TurretLevelCFG::Load()
{
	Load("../Config/Fish_TurretLevelCFG.xml");
}

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