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

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

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

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

void Fish_TurretSetCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_TurretSetCFG.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_TurretSetCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mSetID = element->IntAttribute("SetID");
		data.mName = element->Attribute("Name");
		data.mMinRate = element->IntAttribute("MinRate");
		data.mMaxRate = element->IntAttribute("MaxRate");
		data.mGrade = element->IntAttribute("Grade");
		data.mVipClear = element->IntAttribute("VipClear");
		data.mVipGet = element->IntAttribute("VipGet");
		{
			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);
				}
			}
		}
		{
			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);
				}
			}
		}
		{
			const char* RandomEffect = element->Attribute("RandomEffect");
			std::vector<std::string> vecRandomEffect;
			boost::split(vecRandomEffect, RandomEffect, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecRandomEffect.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecRandomEffect[i].c_str(), &temp))
				{
					data.mRandomEffect.push_back(temp);
				}
			}
		}
		data.mTurretID = element->IntAttribute("TurretID");
		data.mVelocity = element->IntAttribute("Velocity");
		data.mLockingPower = element->IntAttribute("LockingPower");
		data.mRagePower = element->IntAttribute("RagePower");
		data.mSelfBulletID = element->IntAttribute("SelfBulletID");
		data.mOtherBulletID = element->IntAttribute("OtherBulletID");
		data.mPowerBulletID = element->IntAttribute("PowerBulletID");
		data.mAddRate = element->IntAttribute("AddRate");
		data.mMaxPower = element->IntAttribute("MaxPower");
		data.mPowerBuff = element->IntAttribute("PowerBuff");
		data.mFireSpeed = element->IntAttribute("FireSpeed");
		{
			const char* FireItemSpeed = element->Attribute("FireItemSpeed");
			std::vector<std::string> vecFireItemSpeed;
			boost::split(vecFireItemSpeed, FireItemSpeed, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFireItemSpeed.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFireItemSpeed[i].c_str(), &temp))
				{
					data.mFireItemSpeed.push_back(temp);
				}
			}
		}
		data.mRoute = element->IntAttribute("Route");
		data.mTopLevel = element->IntAttribute("TopLevel");
		data.mLvUpItem = element->IntAttribute("LvUpItem");
		data.mLvUpCount = element->IntAttribute("LvUpCount");
		{
			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);
				}
			}
		}
		{
			const char* BatteryLv = element->Attribute("BatteryLv");
			std::vector<std::string> vecBatteryLv;
			boost::split(vecBatteryLv, BatteryLv, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecBatteryLv.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecBatteryLv[i].c_str(), &temp))
				{
					data.mBatteryLv.push_back(temp);
				}
			}
		}
		{
			const char* MultipleUpper = element->Attribute("MultipleUpper");
			std::vector<std::string> vecMultipleUpper;
			boost::split(vecMultipleUpper, MultipleUpper, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecMultipleUpper.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecMultipleUpper[i].c_str(), &temp))
				{
					data.mMultipleUpper.push_back(temp);
				}
			}
		}
		{
			const char* AddValue = element->Attribute("AddValue");
			std::vector<std::string> vecAddValue;
			boost::split(vecAddValue, AddValue, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecAddValue.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecAddValue[i].c_str(), &temp))
				{
					data.mAddValue.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_TurretSetCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_TurretSetCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_TurretSetCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_TurretSetCFG");
	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_TurretSetCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readInt(L, "SetID", data.mSetID);
		LuaCfgHelper::readString(L, "Name", data.mName);
		LuaCfgHelper::readInt(L, "MinRate", data.mMinRate);
		LuaCfgHelper::readInt(L, "MaxRate", data.mMaxRate);
		LuaCfgHelper::readInt(L, "Grade", data.mGrade);
		LuaCfgHelper::readInt(L, "VipClear", data.mVipClear);
		LuaCfgHelper::readInt(L, "VipGet", data.mVipGet);
		LuaCfgHelper::readVectorInt(L, "ItemID", data.mItemID);
		LuaCfgHelper::readVectorInt(L, "EffectType", data.mEffectType);
		LuaCfgHelper::readVectorInt(L, "RandomEffect", data.mRandomEffect);
		LuaCfgHelper::readInt(L, "TurretID", data.mTurretID);
		LuaCfgHelper::readInt(L, "Velocity", data.mVelocity);
		LuaCfgHelper::readInt(L, "LockingPower", data.mLockingPower);
		LuaCfgHelper::readInt(L, "RagePower", data.mRagePower);
		LuaCfgHelper::readInt(L, "SelfBulletID", data.mSelfBulletID);
		LuaCfgHelper::readInt(L, "OtherBulletID", data.mOtherBulletID);
		LuaCfgHelper::readInt(L, "PowerBulletID", data.mPowerBulletID);
		LuaCfgHelper::readInt(L, "AddRate", data.mAddRate);
		LuaCfgHelper::readInt(L, "MaxPower", data.mMaxPower);
		LuaCfgHelper::readInt(L, "PowerBuff", data.mPowerBuff);
		LuaCfgHelper::readInt(L, "FireSpeed", data.mFireSpeed);
		LuaCfgHelper::readVectorInt(L, "FireItemSpeed", data.mFireItemSpeed);
		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::readVectorInt(L, "ResetReturnCount", data.mResetReturnCount);
		LuaCfgHelper::readVectorInt(L, "BatteryLv", data.mBatteryLv);
		LuaCfgHelper::readVectorInt(L, "MultipleUpper", data.mMultipleUpper);
		LuaCfgHelper::readVectorInt(L, "AddValue", data.mAddValue);
		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_TurretSetCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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