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

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

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

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

void Fish_RoomCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_RoomCFG.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_RoomCFGData data;
		data.mRoomID = element->IntAttribute("RoomID");
		data.mRoomType = element->IntAttribute("RoomType");
		data.mRoomName = element->Attribute("RoomName");
		data.mRoomImage = element->Attribute("RoomImage");
		data.mArmatureName = element->Attribute("ArmatureName");
		data.mArmaturePath = element->Attribute("ArmaturePath");
		data.mOutFishConfig = element->Attribute("OutFishConfig");
		data.mLargetFishConfig = element->Attribute("LargetFishConfig");
		data.mBOSSID = element->IntAttribute("BOSSID");
		data.mNormalOutFishTime = element->IntAttribute("NormalOutFishTime");
		data.mSpecialBoss = element->IntAttribute("SpecialBoss");
		data.mBossGroup = element->IntAttribute("BossGroup");
		data.mGoldCondition = element->IntAttribute("GoldCondition");
		data.mVipCondition = element->IntAttribute("VipCondition");
		data.mOrder = element->IntAttribute("Order");
		data.mTicketCondition = element->IntAttribute("TicketCondition");
		data.mLockBagLv = element->IntAttribute("LockBagLv");
		data.mLevelCondition = element->IntAttribute("LevelCondition");
		data.mRobotMinGold = element->IntAttribute("RobotMinGold");
		data.mRobotMaxGold = element->IntAttribute("RobotMaxGold");
		data.mMinRate = element->IntAttribute("MinRate");
		data.mMaxRate = element->IntAttribute("MaxRate");
		data.mSkillTurret = element->IntAttribute("SkillTurret");
		{
			const char* RateList = element->Attribute("RateList");
			std::vector<std::string> vecRateList;
			boost::split(vecRateList, RateList, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecRateList.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecRateList[i].c_str(), &temp))
				{
					data.mRateList.push_back(temp);
				}
			}
		}
		data.mTableCount = element->IntAttribute("TableCount");
		data.mTimeCheck = element->Attribute("TimeCheck");
		data.mRedPackage = element->IntAttribute("RedPackage");
		{
			const char* RedQuantity = element->Attribute("RedQuantity");
			std::vector<std::string> vecRedQuantity;
			boost::split(vecRedQuantity, RedQuantity, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecRedQuantity.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecRedQuantity[i].c_str(), &temp))
				{
					data.mRedQuantity.push_back(temp);
				}
			}
		}
		data.mIsShow = element->BoolAttribute("IsShow");
		data.mIsOpen = element->BoolAttribute("IsOpen");
		data.mPowerParam = element->IntAttribute("PowerParam");
		data.mBuyPowerCost = element->IntAttribute("BuyPowerCost");
		data.mMissileRate = element->IntAttribute("MissileRate");
		data.mMissileCost = element->IntAttribute("MissileCost");
		data.mOpenProtect = element->BoolAttribute("OpenProtect");
		data.mExpGet = element->BoolAttribute("ExpGet");
		data.mChangeTable = element->BoolAttribute("ChangeTable");
		data.mWarnLeaveLv = element->IntAttribute("WarnLeaveLv");
		data.mNoEnterLv = element->IntAttribute("NoEnterLv");
		data.mWinRateAverage = element->IntAttribute("WinRateAverage");
		data.mWinRateMax = element->IntAttribute("WinRateMax");
		data.mWinRateMin = element->IntAttribute("WinRateMin");
		data.mWinRateCtrValue = element->IntAttribute("WinRateCtrValue");
		{
			const char* SceneIds = element->Attribute("SceneIds");
			std::vector<std::string> vecSceneIds;
			boost::split(vecSceneIds, SceneIds, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecSceneIds.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecSceneIds[i].c_str(), &temp))
				{
					data.mSceneIds.push_back(temp);
				}
			}
		}
		{
			const char* OpenTime = element->Attribute("OpenTime");
			std::vector<std::string> vecOpenTime;
			boost::split(vecOpenTime, OpenTime, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecOpenTime.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecOpenTime[i].c_str(), &temp))
				{
					data.mOpenTime.push_back(temp);
				}
			}
		}
		{
			const char* FreeLockId = element->Attribute("FreeLockId");
			std::vector<std::string> vecFreeLockId;
			boost::split(vecFreeLockId, FreeLockId, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFreeLockId.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFreeLockId[i].c_str(), &temp))
				{
					data.mFreeLockId.push_back(temp);
				}
			}
		}
		{
			const char* FreeViolentId = element->Attribute("FreeViolentId");
			std::vector<std::string> vecFreeViolentId;
			boost::split(vecFreeViolentId, FreeViolentId, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecFreeViolentId.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecFreeViolentId[i].c_str(), &temp))
				{
					data.mFreeViolentId.push_back(temp);
				}
			}
		}
		if (mMapData.find(data.mRoomID) != mMapData.end())std::cout <<"data refind:" << data.mRoomID << std::endl;
		CCASSERT(mMapData.find(data.mRoomID) == mMapData.end(), "data.mRoomID is exists");
		mMapData.insert(std::make_pair(data.mRoomID, data));
		element = element->NextSiblingElement();
	}
	CCLOG("Fish_RoomCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_RoomCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_RoomCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_RoomCFG");
	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_RoomCFGData data;
		LuaCfgHelper::readInt(L, "RoomID", data.mRoomID);
		LuaCfgHelper::readInt(L, "RoomType", data.mRoomType);
		LuaCfgHelper::readString(L, "RoomName", data.mRoomName);
		LuaCfgHelper::readString(L, "RoomImage", data.mRoomImage);
		LuaCfgHelper::readString(L, "ArmatureName", data.mArmatureName);
		LuaCfgHelper::readString(L, "ArmaturePath", data.mArmaturePath);
		LuaCfgHelper::readString(L, "OutFishConfig", data.mOutFishConfig);
		LuaCfgHelper::readString(L, "LargetFishConfig", data.mLargetFishConfig);
		LuaCfgHelper::readInt(L, "BOSSID", data.mBOSSID);
		LuaCfgHelper::readInt(L, "NormalOutFishTime", data.mNormalOutFishTime);
		LuaCfgHelper::readInt(L, "SpecialBoss", data.mSpecialBoss);
		LuaCfgHelper::readInt(L, "BossGroup", data.mBossGroup);
		LuaCfgHelper::readInt(L, "GoldCondition", data.mGoldCondition);
		LuaCfgHelper::readInt(L, "VipCondition", data.mVipCondition);
		LuaCfgHelper::readInt(L, "Order", data.mOrder);
		LuaCfgHelper::readInt(L, "TicketCondition", data.mTicketCondition);
		LuaCfgHelper::readInt(L, "LockBagLv", data.mLockBagLv);
		LuaCfgHelper::readInt(L, "LevelCondition", data.mLevelCondition);
		LuaCfgHelper::readInt(L, "RobotMinGold", data.mRobotMinGold);
		LuaCfgHelper::readInt(L, "RobotMaxGold", data.mRobotMaxGold);
		LuaCfgHelper::readInt(L, "MinRate", data.mMinRate);
		LuaCfgHelper::readInt(L, "MaxRate", data.mMaxRate);
		LuaCfgHelper::readInt(L, "SkillTurret", data.mSkillTurret);
		LuaCfgHelper::readVectorInt(L, "RateList", data.mRateList);
		LuaCfgHelper::readInt(L, "TableCount", data.mTableCount);
		LuaCfgHelper::readString(L, "TimeCheck", data.mTimeCheck);
		LuaCfgHelper::readInt(L, "RedPackage", data.mRedPackage);
		LuaCfgHelper::readVectorInt(L, "RedQuantity", data.mRedQuantity);
		LuaCfgHelper::readBool(L, "IsShow", data.mIsShow);
		LuaCfgHelper::readBool(L, "IsOpen", data.mIsOpen);
		LuaCfgHelper::readInt(L, "PowerParam", data.mPowerParam);
		LuaCfgHelper::readInt(L, "BuyPowerCost", data.mBuyPowerCost);
		LuaCfgHelper::readInt(L, "MissileRate", data.mMissileRate);
		LuaCfgHelper::readInt(L, "MissileCost", data.mMissileCost);
		LuaCfgHelper::readBool(L, "OpenProtect", data.mOpenProtect);
		LuaCfgHelper::readBool(L, "ExpGet", data.mExpGet);
		LuaCfgHelper::readBool(L, "ChangeTable", data.mChangeTable);
		LuaCfgHelper::readInt(L, "WarnLeaveLv", data.mWarnLeaveLv);
		LuaCfgHelper::readInt(L, "NoEnterLv", data.mNoEnterLv);
		LuaCfgHelper::readInt(L, "WinRateAverage", data.mWinRateAverage);
		LuaCfgHelper::readInt(L, "WinRateMax", data.mWinRateMax);
		LuaCfgHelper::readInt(L, "WinRateMin", data.mWinRateMin);
		LuaCfgHelper::readInt(L, "WinRateCtrValue", data.mWinRateCtrValue);
		LuaCfgHelper::readVectorInt(L, "SceneIds", data.mSceneIds);
		LuaCfgHelper::readVectorInt(L, "OpenTime", data.mOpenTime);
		LuaCfgHelper::readVectorInt(L, "FreeLockId", data.mFreeLockId);
		LuaCfgHelper::readVectorInt(L, "FreeViolentId", data.mFreeViolentId);
		if (mMapData.find(data.mRoomID) != mMapData.end())std::cout <<"data refind:" << data.mRoomID << std::endl;
		CCASSERT(mMapData.find(data.mRoomID) == mMapData.end(), "data.mRoomID is exists");
		mMapData.insert(std::make_pair(data.mRoomID, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("Fish_RoomCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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