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

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

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

const std::map<int64_t, M_magic_array_configData>& M_magic_array_config::GetMapData()
{
	return mMapData;
}

void M_magic_array_config::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_magic_array_config.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_magic_array_configData data;
		data.mlevel = element->IntAttribute("level");
		{
			const char* up_cost = element->Attribute("up_cost");
			std::vector<std::string> vecup_cost;
			boost::split(vecup_cost, up_cost, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecup_cost.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecup_cost[i].c_str(), &temp))
				{
					data.mup_cost.push_back(temp);
				}
			}
		}
		{
			const char* atk_cost = element->Attribute("atk_cost");
			std::vector<std::string> vecatk_cost;
			boost::split(vecatk_cost, atk_cost, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecatk_cost.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecatk_cost[i].c_str(), &temp))
				{
					data.matk_cost.push_back(temp);
				}
			}
		}
		{
			const char* un_lock_award = element->Attribute("un_lock_award");
			std::vector<std::string> vecun_lock_award;
			boost::split(vecun_lock_award, un_lock_award, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecun_lock_award.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecun_lock_award[i].c_str(), &temp))
				{
					data.mun_lock_award.push_back(temp);
				}
			}
		}
		data.matk = element->IntAttribute("atk");
		data.mhp = element->IntAttribute("hp");
		data.micon = element->Attribute("icon");
		data.mm_stone = element->Attribute("m_stone");
		data.mmgc_stone = element->FloatAttribute("mgc_stone");
		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_magic_array_config Loaded. Load Data:%u", mMapData.size());
}

void M_magic_array_config::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_magic_array_config");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_magic_array_config");
	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_magic_array_configData data;
		LuaCfgHelper::readInt(L, "level", data.mlevel);
		LuaCfgHelper::readVectorInt(L, "up_cost", data.mup_cost);
		LuaCfgHelper::readVectorInt(L, "atk_cost", data.matk_cost);
		LuaCfgHelper::readVectorInt(L, "un_lock_award", data.mun_lock_award);
		LuaCfgHelper::readInt(L, "atk", data.matk);
		LuaCfgHelper::readInt(L, "hp", data.mhp);
		LuaCfgHelper::readString(L, "icon", data.micon);
		LuaCfgHelper::readString(L, "m_stone", data.mm_stone);
		LuaCfgHelper::readFloat(L, "mgc_stone", data.mmgc_stone);
		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_magic_array_config Loaded. Load Data:%u", mMapData.size());
}

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

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

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