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

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

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

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

void M_VIPProfitCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_VIPProfitCFG.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)
	{
		M_VIPProfitCFGData data;
		data.mVipLv = element->IntAttribute("VipLv");
		data.mVipExp = element->IntAttribute("VipExp");
		data.mOnlineReward = element->IntAttribute("OnlineReward");
		data.mMaxGiftslimit = element->IntAttribute("MaxGiftslimit");
		{
			const char* GiftUnlock = element->Attribute("GiftUnlock");
			std::vector<std::string> vecGiftUnlock;
			boost::split(vecGiftUnlock, GiftUnlock, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecGiftUnlock.size(); i++)
			{
				data.mGiftUnlock.push_back(vecGiftUnlock[i]);
			}
		}
		{
			const char* RoomUnlock = element->Attribute("RoomUnlock");
			std::vector<std::string> vecRoomUnlock;
			boost::split(vecRoomUnlock, RoomUnlock, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecRoomUnlock.size(); i++)
			{
				data.mRoomUnlock.push_back(vecRoomUnlock[i]);
			}
		}
		{
			const char* FishItemUnlock = element->Attribute("FishItemUnlock");
			std::vector<std::string> vecFishItemUnlock;
			boost::split(vecFishItemUnlock, FishItemUnlock, boost::is_any_of(","));
			for (unsigned int i = 0; i < vecFishItemUnlock.size(); i++)
			{
				data.mFishItemUnlock.push_back(vecFishItemUnlock[i]);
			}
		}
		data.mVipName = element->IntAttribute("VipName");
		data.mGiveTicket = element->IntAttribute("GiveTicket");
		data.mAttendance = element->IntAttribute("Attendance");
		data.mDailyLottery = element->IntAttribute("DailyLottery");
		data.mBrokenGift = element->IntAttribute("BrokenGift");
		data.mBankruptcyRelief = element->IntAttribute("BankruptcyRelief");
		data.mTodayMaxGold = element->IntAttribute("TodayMaxGold");
		data.mPropGun = element->IntAttribute("PropGun");
		data.mPropGunName = element->Attribute("PropGunName");
		data.mPropGunNameImg = element->Attribute("PropGunNameImg");
		data.mPropGunIcon = element->Attribute("PropGunIcon");
		{
			const char* CanStar = element->Attribute("CanStar");
			std::vector<std::string> vecCanStar;
			boost::split(vecCanStar, CanStar, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecCanStar.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecCanStar[i].c_str(), &temp))
				{
					data.mCanStar.push_back(temp);
				}
			}
		}
		{
			const char* StarFixed = element->Attribute("StarFixed");
			std::vector<std::string> vecStarFixed;
			boost::split(vecStarFixed, StarFixed, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecStarFixed.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecStarFixed[i].c_str(), &temp))
				{
					data.mStarFixed.push_back(temp);
				}
			}
		}
		data.mBuyMaterialsGiftLimit = element->IntAttribute("BuyMaterialsGiftLimit");
		data.mBuyWeekLimit = element->IntAttribute("BuyWeekLimit");
		{
			const char* BagShopSort = element->Attribute("BagShopSort");
			std::vector<std::string> vecBagShopSort;
			boost::split(vecBagShopSort, BagShopSort, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecBagShopSort.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecBagShopSort[i].c_str(), &temp))
				{
					data.mBagShopSort.push_back(temp);
				}
			}
		}
		{
			const char* GoldShopSort = element->Attribute("GoldShopSort");
			std::vector<std::string> vecGoldShopSort;
			boost::split(vecGoldShopSort, GoldShopSort, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGoldShopSort.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGoldShopSort[i].c_str(), &temp))
				{
					data.mGoldShopSort.push_back(temp);
				}
			}
		}
		{
			const char* GemShopSort = element->Attribute("GemShopSort");
			std::vector<std::string> vecGemShopSort;
			boost::split(vecGemShopSort, GemShopSort, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecGemShopSort.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecGemShopSort[i].c_str(), &temp))
				{
					data.mGemShopSort.push_back(temp);
				}
			}
		}
		data.mTurretAddRate = element->IntAttribute("TurretAddRate");
		data.mSquamaExchange = element->IntAttribute("SquamaExchange");
		data.mAirDropsNumber = element->IntAttribute("AirDropsNumber");
		data.mOnlineRewardMultiples = element->FloatAttribute("OnlineRewardMultiples");
		data.mGoldenTurtleTime = element->IntAttribute("GoldenTurtleTime");
		data.mRechargeGoldRewardEx = element->IntAttribute("RechargeGoldRewardEx");
		{
			const char* DisplayVipReward1 = element->Attribute("DisplayVipReward1");
			std::vector<std::string> vecDisplayVipReward1;
			boost::split(vecDisplayVipReward1, DisplayVipReward1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDisplayVipReward1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDisplayVipReward1[i].c_str(), &temp))
				{
					data.mDisplayVipReward1.push_back(temp);
				}
			}
		}
		{
			const char* VipReward1 = element->Attribute("VipReward1");
			std::vector<std::string> vecVipReward1;
			boost::split(vecVipReward1, VipReward1, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecVipReward1.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecVipReward1[i].c_str(), &temp))
				{
					data.mVipReward1.push_back(temp);
				}
			}
		}
		{
			const char* DisplayVipReward2 = element->Attribute("DisplayVipReward2");
			std::vector<std::string> vecDisplayVipReward2;
			boost::split(vecDisplayVipReward2, DisplayVipReward2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecDisplayVipReward2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecDisplayVipReward2[i].c_str(), &temp))
				{
					data.mDisplayVipReward2.push_back(temp);
				}
			}
		}
		data.mArenaUp = element->IntAttribute("ArenaUp");
		data.mVipMoney = element->IntAttribute("VipMoney");
		{
			const char* VipReward2 = element->Attribute("VipReward2");
			std::vector<std::string> vecVipReward2;
			boost::split(vecVipReward2, VipReward2, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecVipReward2.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecVipReward2[i].c_str(), &temp))
				{
					data.mVipReward2.push_back(temp);
				}
			}
		}
		data.mPersonalPoolRebate = element->IntAttribute("PersonalPoolRebate");
		if (mMapData.find(data.mVipLv) != mMapData.end())std::cout <<"data refind:" << data.mVipLv << std::endl;
		CCASSERT(mMapData.find(data.mVipLv) == mMapData.end(), "data.mVipLv is exists");
		mMapData.insert(std::make_pair(data.mVipLv, data));
		element = element->NextSiblingElement();
	}
	CCLOG("M_VIPProfitCFG Loaded. Load Data:%u", mMapData.size());
}

void M_VIPProfitCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_VIPProfitCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_VIPProfitCFG");
	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");
		M_VIPProfitCFGData data;
		LuaCfgHelper::readInt(L, "VipLv", data.mVipLv);
		LuaCfgHelper::readInt(L, "VipExp", data.mVipExp);
		LuaCfgHelper::readInt(L, "OnlineReward", data.mOnlineReward);
		LuaCfgHelper::readInt(L, "MaxGiftslimit", data.mMaxGiftslimit);
		LuaCfgHelper::readVectorString(L, "GiftUnlock", data.mGiftUnlock);
		LuaCfgHelper::readVectorString(L, "RoomUnlock", data.mRoomUnlock);
		LuaCfgHelper::readVectorString(L, "FishItemUnlock", data.mFishItemUnlock);
		LuaCfgHelper::readInt(L, "VipName", data.mVipName);
		LuaCfgHelper::readInt(L, "GiveTicket", data.mGiveTicket);
		LuaCfgHelper::readInt(L, "Attendance", data.mAttendance);
		LuaCfgHelper::readInt(L, "DailyLottery", data.mDailyLottery);
		LuaCfgHelper::readInt(L, "BrokenGift", data.mBrokenGift);
		LuaCfgHelper::readInt(L, "BankruptcyRelief", data.mBankruptcyRelief);
		LuaCfgHelper::readInt(L, "TodayMaxGold", data.mTodayMaxGold);
		LuaCfgHelper::readInt(L, "PropGun", data.mPropGun);
		LuaCfgHelper::readString(L, "PropGunName", data.mPropGunName);
		LuaCfgHelper::readString(L, "PropGunNameImg", data.mPropGunNameImg);
		LuaCfgHelper::readString(L, "PropGunIcon", data.mPropGunIcon);
		LuaCfgHelper::readVectorInt(L, "CanStar", data.mCanStar);
		LuaCfgHelper::readVectorInt(L, "StarFixed", data.mStarFixed);
		LuaCfgHelper::readInt(L, "BuyMaterialsGiftLimit", data.mBuyMaterialsGiftLimit);
		LuaCfgHelper::readInt(L, "BuyWeekLimit", data.mBuyWeekLimit);
		LuaCfgHelper::readVectorInt(L, "BagShopSort", data.mBagShopSort);
		LuaCfgHelper::readVectorInt(L, "GoldShopSort", data.mGoldShopSort);
		LuaCfgHelper::readVectorInt(L, "GemShopSort", data.mGemShopSort);
		LuaCfgHelper::readInt(L, "TurretAddRate", data.mTurretAddRate);
		LuaCfgHelper::readInt(L, "SquamaExchange", data.mSquamaExchange);
		LuaCfgHelper::readInt(L, "AirDropsNumber", data.mAirDropsNumber);
		LuaCfgHelper::readFloat(L, "OnlineRewardMultiples", data.mOnlineRewardMultiples);
		LuaCfgHelper::readInt(L, "GoldenTurtleTime", data.mGoldenTurtleTime);
		LuaCfgHelper::readInt(L, "RechargeGoldRewardEx", data.mRechargeGoldRewardEx);
		LuaCfgHelper::readVectorInt(L, "DisplayVipReward1", data.mDisplayVipReward1);
		LuaCfgHelper::readVectorInt(L, "VipReward1", data.mVipReward1);
		LuaCfgHelper::readVectorInt(L, "DisplayVipReward2", data.mDisplayVipReward2);
		LuaCfgHelper::readInt(L, "ArenaUp", data.mArenaUp);
		LuaCfgHelper::readInt(L, "VipMoney", data.mVipMoney);
		LuaCfgHelper::readVectorInt(L, "VipReward2", data.mVipReward2);
		LuaCfgHelper::readInt(L, "PersonalPoolRebate", data.mPersonalPoolRebate);
		if (mMapData.find(data.mVipLv) != mMapData.end())std::cout <<"data refind:" << data.mVipLv << std::endl;
		CCASSERT(mMapData.find(data.mVipLv) == mMapData.end(), "data.mVipLv is exists");
		mMapData.insert(std::make_pair(data.mVipLv, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("M_VIPProfitCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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