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

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

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

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

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

void Fish_RoomCFG::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_RoomCFGData data;
		data.mRoomID = element->IntAttribute("RoomID");
		data.mRoomType = element->IntAttribute("RoomType");
		data.mRoomName = element->Attribute("RoomName");
		data.mRoomImage = element->Attribute("RoomImage");
		data.mArmatureName = element->Attribute("ArmatureName");
		data.mArmaturePath = element->Attribute("ArmaturePath");
		data.mOutFishConfig = element->Attribute("OutFishConfig");
		data.mLargetFishConfig = element->Attribute("LargetFishConfig");
		data.mBOSSID = element->IntAttribute("BOSSID");
		data.mNormalOutFishTime = element->IntAttribute("NormalOutFishTime");
		data.mSpecialBoss = element->IntAttribute("SpecialBoss");
		data.mBossGroup = element->IntAttribute("BossGroup");
		data.mGoldCondition = element->IntAttribute("GoldCondition");
		data.mVipCondition = element->IntAttribute("VipCondition");
		data.mOrder = element->IntAttribute("Order");
		data.mTicketCondition = element->IntAttribute("TicketCondition");
		data.mLockBagLv = element->IntAttribute("LockBagLv");
		data.mLevelCondition = element->IntAttribute("LevelCondition");
		data.mRobotMinGold = element->IntAttribute("RobotMinGold");
		data.mRobotMaxGold = element->IntAttribute("RobotMaxGold");
		data.mMinRate = element->IntAttribute("MinRate");
		data.mMaxRate = element->IntAttribute("MaxRate");
		data.mSkillTurret = element->IntAttribute("SkillTurret");
		{
			const char* RateList = element->Attribute("RateList");
			std::vector<std::string> vecRateList;
			boost::split(vecRateList, RateList, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecRateList.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecRateList[i].c_str(), &temp))
				{
					data.mRateList.push_back(temp);
				}
			}
		}
		data.mTableCount = element->IntAttribute("TableCount");
		data.mTimeCheck = element->Attribute("TimeCheck");
		data.mRedPackage = element->IntAttribute("RedPackage");
		{
			const char* RedQuantity = element->Attribute("RedQuantity");
			std::vector<std::string> vecRedQuantity;
			boost::split(vecRedQuantity, RedQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecRedQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecRedQuantity[i].c_str(), &temp))
				{
					data.mRedQuantity.push_back(temp);
				}
			}
		}
		data.mIsShow = element->BoolAttribute("IsShow");
		data.mIsOpen = element->BoolAttribute("IsOpen");
		data.mPowerParam = element->IntAttribute("PowerParam");
		data.mBuyPowerCost = element->IntAttribute("BuyPowerCost");
		data.mMissileRate = element->IntAttribute("MissileRate");
		data.mMissileCost = element->IntAttribute("MissileCost");
		data.mOpenProtect = element->BoolAttribute("OpenProtect");
		data.mExpGet = element->BoolAttribute("ExpGet");
		data.mChangeTable = element->BoolAttribute("ChangeTable");
		data.mWarnLeaveLv = element->IntAttribute("WarnLeaveLv");
		data.mNoEnterLv = element->IntAttribute("NoEnterLv");
		data.mWinRateAverage = element->IntAttribute("WinRateAverage");
		data.mWinRateMax = element->IntAttribute("WinRateMax");
		data.mWinRateMin = element->IntAttribute("WinRateMin");
		data.mWinRateCtrValue = element->IntAttribute("WinRateCtrValue");
		{
			const char* SceneIds = element->Attribute("SceneIds");
			std::vector<std::string> vecSceneIds;
			boost::split(vecSceneIds, SceneIds, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSceneIds.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSceneIds[i].c_str(), &temp))
				{
					data.mSceneIds.push_back(temp);
				}
			}
		}
		{
			const char* OpenTime = element->Attribute("OpenTime");
			std::vector<std::string> vecOpenTime;
			boost::split(vecOpenTime, OpenTime, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOpenTime.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOpenTime[i].c_str(), &temp))
				{
					data.mOpenTime.push_back(temp);
				}
			}
		}
		{
			const char* FreeLockId = element->Attribute("FreeLockId");
			std::vector<std::string> vecFreeLockId;
			boost::split(vecFreeLockId, FreeLockId, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFreeLockId.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFreeLockId[i].c_str(), &temp))
				{
					data.mFreeLockId.push_back(temp);
				}
			}
		}
		{
			const char* FreeViolentId = element->Attribute("FreeViolentId");
			std::vector<std::string> vecFreeViolentId;
			boost::split(vecFreeViolentId, FreeViolentId, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFreeViolentId.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFreeViolentId[i].c_str(), &temp))
				{
					data.mFreeViolentId.push_back(temp);
				}
			}
		}
		if (mMapData.find(data.mRoomID) != mMapData.end())std::cout <<"data refind:" << data.mRoomID << std::endl;
		assert(mMapData.find(data.mRoomID) == mMapData.end());
		mMapData.insert(std::make_pair(data.mRoomID, data));
		element = element->NextSiblingElement();
	}
}

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

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