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

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

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

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

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

void M_RechangeCFG::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_RechangeCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mName = element->Attribute("Name");
		data.mDesc = element->Attribute("Desc");
		data.mActivityId = element->IntAttribute("ActivityId");
		data.mIcon = element->Attribute("Icon");
		data.mStartTime = element->Attribute("StartTime");
		data.mEndTime = element->Attribute("EndTime");
		data.mGroup = element->IntAttribute("Group");
		{
			const char* needlevel = element->Attribute("needlevel");
			std::vector<std::string> vecneedlevel;
			boost::split(vecneedlevel, needlevel, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecneedlevel.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecneedlevel[i].c_str(), &temp))
				{
					data.mneedlevel.push_back(temp);
				}
			}
		}
		data.mNeedVIP = element->IntAttribute("NeedVIP");
		data.mType = element->IntAttribute("Type");
		data.mPrice = element->IntAttribute("Price");
		data.mDiamondPrice = element->IntAttribute("DiamondPrice");
		data.mNeedLevel = element->IntAttribute("NeedLevel");
		data.mFirstGold = element->IntAttribute("FirstGold");
		data.mFirstTicket = element->IntAttribute("FirstTicket");
		data.mVIPExp = element->IntAttribute("VIPExp");
		data.mIndex = element->IntAttribute("Index");
		data.mLastGift = element->IntAttribute("LastGift");
		data.mNextGift = element->IntAttribute("NextGift");
		data.mShopType = element->IntAttribute("ShopType");
		data.mCumulativeRecharge = element->IntAttribute("CumulativeRecharge");
		{
			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* ItemCount = element->Attribute("ItemCount");
			std::vector<std::string> vecItemCount;
			boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
				{
					data.mItemCount.push_back(temp);
				}
			}
		}
		data.mCount = element->IntAttribute("Count");
		data.mDailyGiftPackage = element->IntAttribute("DailyGiftPackage");
		data.mTimeLimit = element->IntAttribute("TimeLimit");
		data.mCategory = element->IntAttribute("Category");
		data.mBuffID = element->IntAttribute("BuffID");
		data.mRebateValue = element->IntAttribute("RebateValue");
		data.mHotIcon = element->IntAttribute("HotIcon");
		{
			const char* PackageProbability = element->Attribute("PackageProbability");
			std::vector<std::string> vecPackageProbability;
			boost::split(vecPackageProbability, PackageProbability, boost::is_any_of(","));
			float temp;
			for (unsigned int i = 0; i < vecPackageProbability.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToFloat(vecPackageProbability[i].c_str(), &temp))
				{
					data.mPackageProbability.push_back(temp);
				}
			}
		}
		data.mOriginalprice = element->IntAttribute("Originalprice");
		data.mGOLDOriginalprice = element->IntAttribute("GOLDOriginalprice");
		data.mDiamondsOriginalprice = element->IntAttribute("DiamondsOriginalprice");
		data.mGiftCeiling = element->IntAttribute("GiftCeiling");
		data.mLowerGiftLimit = element->IntAttribute("LowerGiftLimit");
		data.mHighestGain = element->IntAttribute("HighestGain");
		data.mTipsDisplay = element->IntAttribute("TipsDisplay");
		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_RechangeCFG::Load()
{
	Load("../Config/M_RechangeCFG.xml");
}

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