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

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

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

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

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

void M_GuideCFG::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_GuideCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mType = element->IntAttribute("Type");
		{
			const char* SelPos = element->Attribute("SelPos");
			std::vector<std::string> vecSelPos;
			boost::split(vecSelPos, SelPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSelPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSelPos[i].c_str(), &temp))
				{
					data.mSelPos.push_back(temp);
				}
			}
		}
		{
			const char* SelSize = element->Attribute("SelSize");
			std::vector<std::string> vecSelSize;
			boost::split(vecSelSize, SelSize, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSelSize.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSelSize[i].c_str(), &temp))
				{
					data.mSelSize.push_back(temp);
				}
			}
		}
		data.mSelPic = element->Attribute("SelPic");
		data.mTipsDescPic = element->Attribute("TipsDescPic");
		{
			const char* TipsPos = element->Attribute("TipsPos");
			std::vector<std::string> vecTipsPos;
			boost::split(vecTipsPos, TipsPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTipsPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTipsPos[i].c_str(), &temp))
				{
					data.mTipsPos.push_back(temp);
				}
			}
		}
		{
			const char* TipsSize = element->Attribute("TipsSize");
			std::vector<std::string> vecTipsSize;
			boost::split(vecTipsSize, TipsSize, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTipsSize.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTipsSize[i].c_str(), &temp))
				{
					data.mTipsSize.push_back(temp);
				}
			}
		}
		data.mTipsPic = element->Attribute("TipsPic");
		{
			const char* IconPos = element->Attribute("IconPos");
			std::vector<std::string> vecIconPos;
			boost::split(vecIconPos, IconPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecIconPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecIconPos[i].c_str(), &temp))
				{
					data.mIconPos.push_back(temp);
				}
			}
		}
		{
			const char* FingerPos = element->Attribute("FingerPos");
			std::vector<std::string> vecFingerPos;
			boost::split(vecFingerPos, FingerPos, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFingerPos.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFingerPos[i].c_str(), &temp))
				{
					data.mFingerPos.push_back(temp);
				}
			}
		}
		data.mName = element->Attribute("Name");
		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_GuideCFG::Load()
{
	Load("../Config/M_GuideCFG.xml");
}

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