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

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

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

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

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

void M_ItemCFG::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_ItemCFGData data;
		data.mItemId = element->IntAttribute("ItemId");
		data.mItemName = element->Attribute("ItemName");
		data.mItemDesc = element->Attribute("ItemDesc");
		data.mIcon = element->Attribute("Icon");
		data.mItemCategory = element->IntAttribute("ItemCategory");
		{
			const char* Parameter = element->Attribute("Parameter");
			std::vector<std::string> vecParameter;
			boost::split(vecParameter, Parameter, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecParameter.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecParameter[i].c_str(), &temp))
				{
					data.mParameter.push_back(temp);
				}
			}
		}
		data.mDiamondValue = element->IntAttribute("DiamondValue");
		{
			const char* ItemValue = element->Attribute("ItemValue");
			std::vector<std::string> vecItemValue;
			boost::split(vecItemValue, ItemValue, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemValue.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemValue[i].c_str(), &temp))
				{
					data.mItemValue.push_back(temp);
				}
			}
		}
		data.mGoldValue = element->IntAttribute("GoldValue");
		data.mConvertedGold = element->IntAttribute("ConvertedGold");
		data.mItemGame = element->IntAttribute("ItemGame");
		data.mGameItem = element->IntAttribute("GameItem");
		data.mBigIcon = element->Attribute("BigIcon");
		data.mItemFun = element->IntAttribute("ItemFun");
		data.mTurretParam = element->IntAttribute("TurretParam");
		data.mWingParam = element->IntAttribute("WingParam");
		data.mDisplay = element->IntAttribute("Display");
		data.mGroup = element->IntAttribute("Group");
		data.mDebris = element->IntAttribute("Debris");
		data.mTimeLimit = element->IntAttribute("TimeLimit");
		data.mTimeLimitDisplay = element->IntAttribute("TimeLimitDisplay");
		data.mTimeCD = element->IntAttribute("TimeCD");
		data.mNeedRemove = element->IntAttribute("NeedRemove");
		data.mImmediateUse = element->IntAttribute("ImmediateUse");
		data.mPlayerLimit = element->IntAttribute("PlayerLimit");
		data.mDropRate = element->IntAttribute("DropRate");
		data.mDropLimit = element->IntAttribute("DropLimit");
		data.mNeedTurret = element->IntAttribute("NeedTurret");
		data.mNeedLevel = element->IntAttribute("NeedLevel");
		data.mResetSettings = element->IntAttribute("ResetSettings");
		data.mRecord = element->IntAttribute("Record");
		if (mMapData.find(data.mItemId) != mMapData.end())std::cout <<"data refind:" << data.mItemId << std::endl;
		assert(mMapData.find(data.mItemId) == mMapData.end());
		mMapData.insert(std::make_pair(data.mItemId, data));
		element = element->NextSiblingElement();
	}
}

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

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