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

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

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

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

void M_un_lock_battle::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_un_lock_battle.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_un_lock_battleData data;
		data.mbattle_type = element->IntAttribute("battle_type");
		data.mun_lock_magic = element->IntAttribute("un_lock_magic");
		data.mdemand = element->IntAttribute("demand");
		data.mprohibit = element->IntAttribute("prohibit");
		{
			const char* use_magic = element->Attribute("use_magic");
			std::vector<std::string> vecuse_magic;
			boost::split(vecuse_magic, use_magic, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecuse_magic.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecuse_magic[i].c_str(), &temp))
				{
					data.muse_magic.push_back(temp);
				}
			}
		}
		{
			const char* fish_wave = element->Attribute("fish_wave");
			std::vector<std::string> vecfish_wave;
			boost::split(vecfish_wave, fish_wave, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecfish_wave.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecfish_wave[i].c_str(), &temp))
				{
					data.mfish_wave.push_back(temp);
				}
			}
		}
		data.mfish_scheme = element->Attribute("fish_scheme");
		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_un_lock_battle Loaded. Load Data:%u", mMapData.size());
}

void M_un_lock_battle::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_un_lock_battle");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_un_lock_battle");
	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_un_lock_battleData data;
		LuaCfgHelper::readInt(L, "battle_type", data.mbattle_type);
		LuaCfgHelper::readInt(L, "un_lock_magic", data.mun_lock_magic);
		LuaCfgHelper::readInt(L, "demand", data.mdemand);
		LuaCfgHelper::readInt(L, "prohibit", data.mprohibit);
		LuaCfgHelper::readVectorInt(L, "use_magic", data.muse_magic);
		LuaCfgHelper::readVectorInt(L, "fish_wave", data.mfish_wave);
		LuaCfgHelper::readString(L, "fish_scheme", data.mfish_scheme);
		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_un_lock_battle Loaded. Load Data:%u", mMapData.size());
}

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

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

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