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

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

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

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

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

void Fish_FreeLotteryCFG::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_FreeLotteryCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mInfo = element->Attribute("Info");
		data.mBaseGold = element->IntAttribute("BaseGold");
		data.mLotteryValue = element->IntAttribute("LotteryValue");
		{
			const char* AwardItem1 = element->Attribute("AwardItem1");
			std::vector<std::string> vecAwardItem1;
			boost::split(vecAwardItem1, AwardItem1, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem1.size(); i++)
			{
				data.mAwardItem1.push_back(vecAwardItem1[i]);
			}
		}
		{
			const char* AwardItem2 = element->Attribute("AwardItem2");
			std::vector<std::string> vecAwardItem2;
			boost::split(vecAwardItem2, AwardItem2, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem2.size(); i++)
			{
				data.mAwardItem2.push_back(vecAwardItem2[i]);
			}
		}
		{
			const char* AwardItem3 = element->Attribute("AwardItem3");
			std::vector<std::string> vecAwardItem3;
			boost::split(vecAwardItem3, AwardItem3, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem3.size(); i++)
			{
				data.mAwardItem3.push_back(vecAwardItem3[i]);
			}
		}
		{
			const char* AwardItem4 = element->Attribute("AwardItem4");
			std::vector<std::string> vecAwardItem4;
			boost::split(vecAwardItem4, AwardItem4, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem4.size(); i++)
			{
				data.mAwardItem4.push_back(vecAwardItem4[i]);
			}
		}
		{
			const char* AwardItem5 = element->Attribute("AwardItem5");
			std::vector<std::string> vecAwardItem5;
			boost::split(vecAwardItem5, AwardItem5, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem5.size(); i++)
			{
				data.mAwardItem5.push_back(vecAwardItem5[i]);
			}
		}
		{
			const char* AwardItem6 = element->Attribute("AwardItem6");
			std::vector<std::string> vecAwardItem6;
			boost::split(vecAwardItem6, AwardItem6, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecAwardItem6.size(); i++)
			{
				data.mAwardItem6.push_back(vecAwardItem6[i]);
			}
		}
		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_FreeLotteryCFG::Load()
{
	Load("../Config/Fish_FreeLotteryCFG.xml");
}

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