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

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

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

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

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

void M_VIPProfitCFG::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_VIPProfitCFGData data;
		data.mVipLv = element->IntAttribute("VipLv");
		data.mVipExp = element->IntAttribute("VipExp");
		data.mOnlineReward = element->IntAttribute("OnlineReward");
		data.mMaxGiftslimit = element->IntAttribute("MaxGiftslimit");
		{
			const char* GiftUnlock = element->Attribute("GiftUnlock");
			std::vector<std::string> vecGiftUnlock;
			boost::split(vecGiftUnlock, GiftUnlock, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecGiftUnlock.size(); i++)
			{
				data.mGiftUnlock.push_back(vecGiftUnlock[i]);
			}
		}
		{
			const char* RoomUnlock = element->Attribute("RoomUnlock");
			std::vector<std::string> vecRoomUnlock;
			boost::split(vecRoomUnlock, RoomUnlock, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecRoomUnlock.size(); i++)
			{
				data.mRoomUnlock.push_back(vecRoomUnlock[i]);
			}
		}
		{
			const char* FishItemUnlock = element->Attribute("FishItemUnlock");
			std::vector<std::string> vecFishItemUnlock;
			boost::split(vecFishItemUnlock, FishItemUnlock, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecFishItemUnlock.size(); i++)
			{
				data.mFishItemUnlock.push_back(vecFishItemUnlock[i]);
			}
		}
		data.mVipName = element->IntAttribute("VipName");
		data.mGiveTicket = element->IntAttribute("GiveTicket");
		data.mAttendance = element->IntAttribute("Attendance");
		data.mDailyLottery = element->IntAttribute("DailyLottery");
		data.mBrokenGift = element->IntAttribute("BrokenGift");
		data.mBankruptcyRelief = element->IntAttribute("BankruptcyRelief");
		data.mTodayMaxGold = element->IntAttribute("TodayMaxGold");
		data.mPropGun = element->IntAttribute("PropGun");
		data.mPropGunName = element->Attribute("PropGunName");
		data.mPropGunNameImg = element->Attribute("PropGunNameImg");
		data.mPropGunIcon = element->Attribute("PropGunIcon");
		{
			const char* CanStar = element->Attribute("CanStar");
			std::vector<std::string> vecCanStar;
			boost::split(vecCanStar, CanStar, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecCanStar.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecCanStar[i].c_str(), &temp))
				{
					data.mCanStar.push_back(temp);
				}
			}
		}
		{
			const char* StarFixed = element->Attribute("StarFixed");
			std::vector<std::string> vecStarFixed;
			boost::split(vecStarFixed, StarFixed, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecStarFixed.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecStarFixed[i].c_str(), &temp))
				{
					data.mStarFixed.push_back(temp);
				}
			}
		}
		data.mBuyMaterialsGiftLimit = element->IntAttribute("BuyMaterialsGiftLimit");
		data.mBuyWeekLimit = element->IntAttribute("BuyWeekLimit");
		{
			const char* BagShopSort = element->Attribute("BagShopSort");
			std::vector<std::string> vecBagShopSort;
			boost::split(vecBagShopSort, BagShopSort, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecBagShopSort.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecBagShopSort[i].c_str(), &temp))
				{
					data.mBagShopSort.push_back(temp);
				}
			}
		}
		{
			const char* GoldShopSort = element->Attribute("GoldShopSort");
			std::vector<std::string> vecGoldShopSort;
			boost::split(vecGoldShopSort, GoldShopSort, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGoldShopSort.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGoldShopSort[i].c_str(), &temp))
				{
					data.mGoldShopSort.push_back(temp);
				}
			}
		}
		{
			const char* GemShopSort = element->Attribute("GemShopSort");
			std::vector<std::string> vecGemShopSort;
			boost::split(vecGemShopSort, GemShopSort, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGemShopSort.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGemShopSort[i].c_str(), &temp))
				{
					data.mGemShopSort.push_back(temp);
				}
			}
		}
		data.mTurretAddRate = element->IntAttribute("TurretAddRate");
		data.mSquamaExchange = element->IntAttribute("SquamaExchange");
		data.mAirDropsNumber = element->IntAttribute("AirDropsNumber");
		data.mOnlineRewardMultiples = element->FloatAttribute("OnlineRewardMultiples");
		data.mGoldenTurtleTime = element->IntAttribute("GoldenTurtleTime");
		data.mRechargeGoldRewardEx = element->IntAttribute("RechargeGoldRewardEx");
		{
			const char* DisplayVipReward1 = element->Attribute("DisplayVipReward1");
			std::vector<std::string> vecDisplayVipReward1;
			boost::split(vecDisplayVipReward1, DisplayVipReward1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDisplayVipReward1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDisplayVipReward1[i].c_str(), &temp))
				{
					data.mDisplayVipReward1.push_back(temp);
				}
			}
		}
		{
			const char* VipReward1 = element->Attribute("VipReward1");
			std::vector<std::string> vecVipReward1;
			boost::split(vecVipReward1, VipReward1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecVipReward1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecVipReward1[i].c_str(), &temp))
				{
					data.mVipReward1.push_back(temp);
				}
			}
		}
		{
			const char* DisplayVipReward2 = element->Attribute("DisplayVipReward2");
			std::vector<std::string> vecDisplayVipReward2;
			boost::split(vecDisplayVipReward2, DisplayVipReward2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDisplayVipReward2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDisplayVipReward2[i].c_str(), &temp))
				{
					data.mDisplayVipReward2.push_back(temp);
				}
			}
		}
		data.mArenaUp = element->IntAttribute("ArenaUp");
		data.mVipMoney = element->IntAttribute("VipMoney");
		{
			const char* VipReward2 = element->Attribute("VipReward2");
			std::vector<std::string> vecVipReward2;
			boost::split(vecVipReward2, VipReward2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecVipReward2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecVipReward2[i].c_str(), &temp))
				{
					data.mVipReward2.push_back(temp);
				}
			}
		}
		data.mPersonalPoolRebate = element->IntAttribute("PersonalPoolRebate");
		if (mMapData.find(data.mVipLv) != mMapData.end())std::cout <<"data refind:" << data.mVipLv << std::endl;
		assert(mMapData.find(data.mVipLv) == mMapData.end());
		mMapData.insert(std::make_pair(data.mVipLv, data));
		element = element->NextSiblingElement();
	}
}

void M_VIPProfitCFG::Load()
{
	Load("../Config/M_VIPProfitCFG.xml");
}

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