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

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

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

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

void Fish_GiantSharkBomber::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_GiantSharkBomber.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_GiantSharkBomberData data;
		data.mID = element->IntAttribute("ID");
		data.mName = element->Attribute("Name");
		data.mConsumeEnergy = element->IntAttribute("ConsumeEnergy");
		data.mGoldBombardment = element->IntAttribute("GoldBombardment");
		{
			const char* GoldWeight = element->Attribute("GoldWeight");
			std::vector<std::string> vecGoldWeight;
			boost::split(vecGoldWeight, GoldWeight, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGoldWeight.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGoldWeight[i].c_str(), &temp))
				{
					data.mGoldWeight.push_back(temp);
				}
			}
		}
		{
			const char* GoldFloat = element->Attribute("GoldFloat");
			std::vector<std::string> vecGoldFloat;
			boost::split(vecGoldFloat, GoldFloat, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGoldFloat.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGoldFloat[i].c_str(), &temp))
				{
					data.mGoldFloat.push_back(temp);
				}
			}
		}
		{
			const char* PointWeight = element->Attribute("PointWeight");
			std::vector<std::string> vecPointWeight;
			boost::split(vecPointWeight, PointWeight, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPointWeight.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPointWeight[i].c_str(), &temp))
				{
					data.mPointWeight.push_back(temp);
				}
			}
		}
		{
			const char* PointFloat = element->Attribute("PointFloat");
			std::vector<std::string> vecPointFloat;
			boost::split(vecPointFloat, PointFloat, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecPointFloat.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecPointFloat[i].c_str(), &temp))
				{
					data.mPointFloat.push_back(temp);
				}
			}
		}
		{
			const char* GiftPointWeight = element->Attribute("GiftPointWeight");
			std::vector<std::string> vecGiftPointWeight;
			boost::split(vecGiftPointWeight, GiftPointWeight, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGiftPointWeight.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGiftPointWeight[i].c_str(), &temp))
				{
					data.mGiftPointWeight.push_back(temp);
				}
			}
		}
		{
			const char* GiftPointFloat = element->Attribute("GiftPointFloat");
			std::vector<std::string> vecGiftPointFloat;
			boost::split(vecGiftPointFloat, GiftPointFloat, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGiftPointFloat.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGiftPointFloat[i].c_str(), &temp))
				{
					data.mGiftPointFloat.push_back(temp);
				}
			}
		}
		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_GiantSharkBomber Loaded. Load Data:%u", mMapData.size());
}

void Fish_GiantSharkBomber::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_GiantSharkBomber");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_GiantSharkBomber");
	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_GiantSharkBomberData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "Name", data.mName);
		LuaCfgHelper::readInt(L, "ConsumeEnergy", data.mConsumeEnergy);
		LuaCfgHelper::readInt(L, "GoldBombardment", data.mGoldBombardment);
		LuaCfgHelper::readVectorInt(L, "GoldWeight", data.mGoldWeight);
		LuaCfgHelper::readVectorInt(L, "GoldFloat", data.mGoldFloat);
		LuaCfgHelper::readVectorInt(L, "PointWeight", data.mPointWeight);
		LuaCfgHelper::readVectorInt(L, "PointFloat", data.mPointFloat);
		LuaCfgHelper::readVectorInt(L, "GiftPointWeight", data.mGiftPointWeight);
		LuaCfgHelper::readVectorInt(L, "GiftPointFloat", data.mGiftPointFloat);
		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_GiantSharkBomber Loaded. Load Data:%u", mMapData.size());
}

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

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

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