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

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

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

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

void Fish_TurretWingCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_TurretWingCFG.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)
	{
		Fish_TurretWingCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mName = element->Attribute("Name");
		{
			const char* EffectType = element->Attribute("EffectType");
			std::vector<std::string> vecEffectType;
			boost::split(vecEffectType, EffectType, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecEffectType.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecEffectType[i].c_str(), &temp))
				{
					data.mEffectType.push_back(temp);
				}
			}
		}
		data.mWingRES = element->Attribute("WingRES");
		{
			const char* ItemID = element->Attribute("ItemID");
			std::vector<std::string> vecItemID;
			boost::split(vecItemID, ItemID, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemID.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemID[i].c_str(), &temp))
				{
					data.mItemID.push_back(temp);
				}
			}
		}
		data.mRoute = element->IntAttribute("Route");
		data.mTopLevel = element->IntAttribute("TopLevel");
		data.mLvUpItem = element->IntAttribute("LvUpItem");
		data.mLvUpCount = element->IntAttribute("LvUpCount");
		data.mRandItem = element->IntAttribute("RandItem");
		data.mRandItemCount = element->IntAttribute("RandItemCount");
		{
			const char* ResetReturnCount = element->Attribute("ResetReturnCount");
			std::vector<std::string> vecResetReturnCount;
			boost::split(vecResetReturnCount, ResetReturnCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecResetReturnCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecResetReturnCount[i].c_str(), &temp))
				{
					data.mResetReturnCount.push_back(temp);
				}
			}
		}
		data.mResetValue = element->IntAttribute("ResetValue");
		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("Fish_TurretWingCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_TurretWingCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_TurretWingCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_TurretWingCFG");
	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");
		Fish_TurretWingCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "Name", data.mName);
		LuaCfgHelper::readVectorInt(L, "EffectType", data.mEffectType);
		LuaCfgHelper::readString(L, "WingRES", data.mWingRES);
		LuaCfgHelper::readVectorInt(L, "ItemID", data.mItemID);
		LuaCfgHelper::readInt(L, "Route", data.mRoute);
		LuaCfgHelper::readInt(L, "TopLevel", data.mTopLevel);
		LuaCfgHelper::readInt(L, "LvUpItem", data.mLvUpItem);
		LuaCfgHelper::readInt(L, "LvUpCount", data.mLvUpCount);
		LuaCfgHelper::readInt(L, "RandItem", data.mRandItem);
		LuaCfgHelper::readInt(L, "RandItemCount", data.mRandItemCount);
		LuaCfgHelper::readVectorInt(L, "ResetReturnCount", data.mResetReturnCount);
		LuaCfgHelper::readInt(L, "ResetValue", data.mResetValue);
		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("Fish_TurretWingCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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