#include "stdafx.h"
#include "tinyxml2/tinyxml2.h"
#include "LuaCfgHelper.h"
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "M_equipment.h"
std::auto_ptr<M_equipment> M_equipment::msSingleton(nullptr);

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

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

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

void M_equipment::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_equipment.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_equipmentData data;
		data.mID = element->IntAttribute("ID");
		data.mitemType = element->IntAttribute("itemType");
		data.mleve = element->IntAttribute("leve");
		data.mquality = element->Attribute("quality");
		{
			const char* cost1 = element->Attribute("cost1");
			std::vector<std::string> veccost1;
			boost::split(veccost1, cost1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < veccost1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(veccost1[i].c_str(), &temp))
				{
					data.mcost1.push_back(temp);
				}
			}
		}
		{
			const char* cost2 = element->Attribute("cost2");
			std::vector<std::string> veccost2;
			boost::split(veccost2, cost2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < veccost2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(veccost2[i].c_str(), &temp))
				{
					data.mcost2.push_back(temp);
				}
			}
		}
		{
			const char* reward = element->Attribute("reward");
			std::vector<std::string> vecreward;
			boost::split(vecreward, reward, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecreward.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecreward[i].c_str(), &temp))
				{
					data.mreward.push_back(temp);
				}
			}
		}
		data.matk = element->IntAttribute("atk");
		data.mhp = element->IntAttribute("hp");
		data.matkpct = element->IntAttribute("atkpct");
		data.mhppct = element->IntAttribute("hppct");
		data.mcrtpct = element->IntAttribute("crtpct");
		data.mmovepct = element->IntAttribute("movepct");
		if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
		CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
		mMapData.insert(std::make_pair(data.mID, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_equipment Loaded. Load Data:%u", mMapData.size());
}

void M_equipment::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_equipment");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_equipment");
	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_equipmentData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readInt(L, "itemType", data.mitemType);
		LuaCfgHelper::readInt(L, "leve", data.mleve);
		LuaCfgHelper::readString(L, "quality", data.mquality);
		LuaCfgHelper::readVectorInt(L, "cost1", data.mcost1);
		LuaCfgHelper::readVectorInt(L, "cost2", data.mcost2);
		LuaCfgHelper::readVectorInt(L, "reward", data.mreward);
		LuaCfgHelper::readInt(L, "atk", data.matk);
		LuaCfgHelper::readInt(L, "hp", data.mhp);
		LuaCfgHelper::readInt(L, "atkpct", data.matkpct);
		LuaCfgHelper::readInt(L, "hppct", data.mhppct);
		LuaCfgHelper::readInt(L, "crtpct", data.mcrtpct);
		LuaCfgHelper::readInt(L, "movepct", data.mmovepct);
		if (mMapData.find(data.mID) != mMapData.end())std::cout <<"data refind:" << data.mID << std::endl;
		CCASSERT(mMapData.find(data.mID) == mMapData.end(), "data.mID is exists");
		mMapData.insert(std::make_pair(data.mID, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_equipment Loaded. Load Data:%u", mMapData.size());
}

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

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

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