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

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

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

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

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

void M_ActivityMayDayWarriorCFG::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)
	{
		M_ActivityMayDayWarriorCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mName = element->Attribute("Name");
		data.mCompleteType = element->IntAttribute("CompleteType");
		{
			const char* CompleteParam = element->Attribute("CompleteParam");
			std::vector<std::string> vecCompleteParam;
			boost::split(vecCompleteParam, CompleteParam, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecCompleteParam.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecCompleteParam[i].c_str(), &temp))
				{
					data.mCompleteParam.push_back(temp);
				}
			}
		}
		data.mCompleteCount = element->IntAttribute("CompleteCount");
		{
			const char* OrdinaryReward = element->Attribute("OrdinaryReward");
			std::vector<std::string> vecOrdinaryReward;
			boost::split(vecOrdinaryReward, OrdinaryReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOrdinaryReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOrdinaryReward[i].c_str(), &temp))
				{
					data.mOrdinaryReward.push_back(temp);
				}
			}
		}
		{
			const char* OrdinaryQuantity = element->Attribute("OrdinaryQuantity");
			std::vector<std::string> vecOrdinaryQuantity;
			boost::split(vecOrdinaryQuantity, OrdinaryQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOrdinaryQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOrdinaryQuantity[i].c_str(), &temp))
				{
					data.mOrdinaryQuantity.push_back(temp);
				}
			}
		}
		{
			const char* LuxuryReward = element->Attribute("LuxuryReward");
			std::vector<std::string> vecLuxuryReward;
			boost::split(vecLuxuryReward, LuxuryReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecLuxuryReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecLuxuryReward[i].c_str(), &temp))
				{
					data.mLuxuryReward.push_back(temp);
				}
			}
		}
		{
			const char* LuxuryQuantity = element->Attribute("LuxuryQuantity");
			std::vector<std::string> vecLuxuryQuantity;
			boost::split(vecLuxuryQuantity, LuxuryQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecLuxuryQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecLuxuryQuantity[i].c_str(), &temp))
				{
					data.mLuxuryQuantity.push_back(temp);
				}
			}
		}
		{
			const char* SupremeReward = element->Attribute("SupremeReward");
			std::vector<std::string> vecSupremeReward;
			boost::split(vecSupremeReward, SupremeReward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSupremeReward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSupremeReward[i].c_str(), &temp))
				{
					data.mSupremeReward.push_back(temp);
				}
			}
		}
		{
			const char* SupremeQuantity = element->Attribute("SupremeQuantity");
			std::vector<std::string> vecSupremeQuantity;
			boost::split(vecSupremeQuantity, SupremeQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSupremeQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSupremeQuantity[i].c_str(), &temp))
				{
					data.mSupremeQuantity.push_back(temp);
				}
			}
		}
		data.mGoType = element->IntAttribute("GoType");
		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 M_ActivityMayDayWarriorCFG::Load()
{
	Load("../Config/M_ActivityMayDayWarriorCFG.xml");
}

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