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

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

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

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

void Fish_TurretLevelCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_TurretLevelCFG.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_TurretLevelCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mOpenRate = element->IntAttribute("OpenRate");
		data.mAwardGold = element->IntAttribute("AwardGold");
		data.mOneTickUp = element->IntAttribute("OneTickUp");
		data.mShotsBuff = element->IntAttribute("ShotsBuff");
		data.mLevelUpIcon = element->Attribute("LevelUpIcon");
		data.mUpType = element->IntAttribute("UpType");
		data.mUpRate = element->IntAttribute("UpRate");
		data.mNeedLucky = element->IntAttribute("NeedLucky");
		{
			const char* NeedItems = element->Attribute("NeedItems");
			std::vector<std::string> vecNeedItems;
			boost::split(vecNeedItems, NeedItems, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNeedItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNeedItems[i].c_str(), &temp))
				{
					data.mNeedItems.push_back(temp);
				}
			}
		}
		{
			const char* NeedCount = element->Attribute("NeedCount");
			std::vector<std::string> vecNeedCount;
			boost::split(vecNeedCount, NeedCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecNeedCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecNeedCount[i].c_str(), &temp))
				{
					data.mNeedCount.push_back(temp);
				}
			}
		}
		{
			const char* ExtraUpgrade = element->Attribute("ExtraUpgrade");
			std::vector<std::string> vecExtraUpgrade;
			boost::split(vecExtraUpgrade, ExtraUpgrade, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecExtraUpgrade.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecExtraUpgrade[i].c_str(), &temp))
				{
					data.mExtraUpgrade.push_back(temp);
				}
			}
		}
		data.mDropAttenuates = element->IntAttribute("DropAttenuates");
		{
			const char* DailyQuestGroup = element->Attribute("DailyQuestGroup");
			std::vector<std::string> vecDailyQuestGroup;
			boost::split(vecDailyQuestGroup, DailyQuestGroup, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDailyQuestGroup.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDailyQuestGroup[i].c_str(), &temp))
				{
					data.mDailyQuestGroup.push_back(temp);
				}
			}
		}
		data.mDropLimit = element->IntAttribute("DropLimit");
		{
			const char* TaskItems = element->Attribute("TaskItems");
			std::vector<std::string> vecTaskItems;
			boost::split(vecTaskItems, TaskItems, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecTaskItems.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecTaskItems[i].c_str(), &temp))
				{
					data.mTaskItems.push_back(temp);
				}
			}
		}
		{
			const char* QuantityOfArticles = element->Attribute("QuantityOfArticles");
			std::vector<std::string> vecQuantityOfArticles;
			boost::split(vecQuantityOfArticles, QuantityOfArticles, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecQuantityOfArticles.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecQuantityOfArticles[i].c_str(), &temp))
				{
					data.mQuantityOfArticles.push_back(temp);
				}
			}
		}
		data.mTaskDisplay = element->IntAttribute("TaskDisplay");
		{
			const char* UnlockDisplayID = element->Attribute("UnlockDisplayID");
			std::vector<std::string> vecUnlockDisplayID;
			boost::split(vecUnlockDisplayID, UnlockDisplayID, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecUnlockDisplayID.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecUnlockDisplayID[i].c_str(), &temp))
				{
					data.mUnlockDisplayID.push_back(temp);
				}
			}
		}
		{
			const char* UnlockDisplayMasks = element->Attribute("UnlockDisplayMasks");
			std::vector<std::string> vecUnlockDisplayMasks;
			boost::split(vecUnlockDisplayMasks, UnlockDisplayMasks, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecUnlockDisplayMasks.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecUnlockDisplayMasks[i].c_str(), &temp))
				{
					data.mUnlockDisplayMasks.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_TurretLevelCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_TurretLevelCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_TurretLevelCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_TurretLevelCFG");
	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_TurretLevelCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readInt(L, "OpenRate", data.mOpenRate);
		LuaCfgHelper::readInt(L, "AwardGold", data.mAwardGold);
		LuaCfgHelper::readInt(L, "OneTickUp", data.mOneTickUp);
		LuaCfgHelper::readInt(L, "ShotsBuff", data.mShotsBuff);
		LuaCfgHelper::readString(L, "LevelUpIcon", data.mLevelUpIcon);
		LuaCfgHelper::readInt(L, "UpType", data.mUpType);
		LuaCfgHelper::readInt(L, "UpRate", data.mUpRate);
		LuaCfgHelper::readInt(L, "NeedLucky", data.mNeedLucky);
		LuaCfgHelper::readVectorInt(L, "NeedItems", data.mNeedItems);
		LuaCfgHelper::readVectorInt(L, "NeedCount", data.mNeedCount);
		LuaCfgHelper::readVectorInt(L, "ExtraUpgrade", data.mExtraUpgrade);
		LuaCfgHelper::readInt(L, "DropAttenuates", data.mDropAttenuates);
		LuaCfgHelper::readVectorInt(L, "DailyQuestGroup", data.mDailyQuestGroup);
		LuaCfgHelper::readInt(L, "DropLimit", data.mDropLimit);
		LuaCfgHelper::readVectorInt(L, "TaskItems", data.mTaskItems);
		LuaCfgHelper::readVectorInt(L, "QuantityOfArticles", data.mQuantityOfArticles);
		LuaCfgHelper::readInt(L, "TaskDisplay", data.mTaskDisplay);
		LuaCfgHelper::readVectorInt(L, "UnlockDisplayID", data.mUnlockDisplayID);
		LuaCfgHelper::readVectorInt(L, "UnlockDisplayMasks", data.mUnlockDisplayMasks);
		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_TurretLevelCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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