#include "stdafx.h"
#include "tinyxml2/tinyxml2.h"
#include "LuaCfgHelper.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "M_ItemCFG.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;
}

const std::map<int, M_ItemCFGData>& M_ItemCFG::GetMapData()
{
	return mMapData;
}

void M_ItemCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_ItemCFG.xml");
	auto result = xmlDoc.Parse(content.c_str(), content.length());
	if (result != tinyxml2::XML_SUCCESS)
	{
		CCLOGERROR("Result:%d", result);
		CCASSERT(false, "result != tinyxml2::XML_SUCCESS");
		return;
	}
	auto root = xmlDoc.RootElement();
	if (root == NULL)
	{
		CCASSERT(false, "root == NULL");
		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;
		CCASSERT(mMapData.find(data.mItemId) == mMapData.end(), "data.mItemId is exists");
		mMapData.insert(std::make_pair(data.mItemId, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_ItemCFG Loaded. Load Data:%u", mMapData.size());
}

void M_ItemCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_ItemCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_ItemCFG");
	CCASSERT(lua_istable(L, -1) == 1, "is not table");
	lua_pushstring(L, "datas");
	lua_gettable(L, -2);
	CCASSERT(lua_istable(L, -1) == 1, "is not table");
	lua_pushnil(L);
	while(lua_next(L, 2))
	{
		CCASSERT(lua_istable(L, -1) == 1, "is not table");
		M_ItemCFGData data;
		LuaCfgHelper::readInt(L, "ItemId", data.mItemId);
		LuaCfgHelper::readString(L, "ItemName", data.mItemName);
		LuaCfgHelper::readString(L, "ItemDesc", data.mItemDesc);
		LuaCfgHelper::readString(L, "Icon", data.mIcon);
		LuaCfgHelper::readInt(L, "ItemCategory", data.mItemCategory);
		LuaCfgHelper::readVectorInt(L, "Parameter", data.mParameter);
		LuaCfgHelper::readInt(L, "DiamondValue", data.mDiamondValue);
		LuaCfgHelper::readVectorInt(L, "ItemValue", data.mItemValue);
		LuaCfgHelper::readInt(L, "GoldValue", data.mGoldValue);
		LuaCfgHelper::readInt(L, "ConvertedGold", data.mConvertedGold);
		LuaCfgHelper::readInt(L, "ItemGame", data.mItemGame);
		LuaCfgHelper::readInt(L, "GameItem", data.mGameItem);
		LuaCfgHelper::readString(L, "BigIcon", data.mBigIcon);
		LuaCfgHelper::readInt(L, "ItemFun", data.mItemFun);
		LuaCfgHelper::readInt(L, "TurretParam", data.mTurretParam);
		LuaCfgHelper::readInt(L, "WingParam", data.mWingParam);
		LuaCfgHelper::readInt(L, "Display", data.mDisplay);
		LuaCfgHelper::readInt(L, "Group", data.mGroup);
		LuaCfgHelper::readInt(L, "Debris", data.mDebris);
		LuaCfgHelper::readInt(L, "TimeLimit", data.mTimeLimit);
		LuaCfgHelper::readInt(L, "TimeLimitDisplay", data.mTimeLimitDisplay);
		LuaCfgHelper::readInt(L, "TimeCD", data.mTimeCD);
		LuaCfgHelper::readInt(L, "NeedRemove", data.mNeedRemove);
		LuaCfgHelper::readInt(L, "ImmediateUse", data.mImmediateUse);
		LuaCfgHelper::readInt(L, "PlayerLimit", data.mPlayerLimit);
		LuaCfgHelper::readInt(L, "DropRate", data.mDropRate);
		LuaCfgHelper::readInt(L, "DropLimit", data.mDropLimit);
		LuaCfgHelper::readInt(L, "NeedTurret", data.mNeedTurret);
		LuaCfgHelper::readInt(L, "NeedLevel", data.mNeedLevel);
		LuaCfgHelper::readInt(L, "ResetSettings", data.mResetSettings);
		LuaCfgHelper::readInt(L, "Record", data.mRecord);
		if (mMapData.find(data.mItemId) != mMapData.end())std::cout <<"data refind:" << data.mItemId << std::endl;
		CCASSERT(mMapData.find(data.mItemId) == mMapData.end(), "data.mItemId is exists");
		mMapData.insert(std::make_pair(data.mItemId, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_ItemCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

void M_ItemCFG::Release()
{
	msSingleton.reset(nullptr);
}
