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

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

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

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

void M_RechangeCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/M_RechangeCFG.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_RechangeCFGData data;
		data.mID = element->IntAttribute("ID");
		data.mName = element->Attribute("Name");
		data.mDesc = element->Attribute("Desc");
		data.mActivityId = element->IntAttribute("ActivityId");
		data.mIcon = element->Attribute("Icon");
		data.mStartTime = element->Attribute("StartTime");
		data.mEndTime = element->Attribute("EndTime");
		data.mGroup = element->IntAttribute("Group");
		{
			const char* needlevel = element->Attribute("needlevel");
			std::vector<std::string> vecneedlevel;
			boost::split(vecneedlevel, needlevel, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecneedlevel.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecneedlevel[i].c_str(), &temp))
				{
					data.mneedlevel.push_back(temp);
				}
			}
		}
		data.mNeedVIP = element->IntAttribute("NeedVIP");
		data.mType = element->IntAttribute("Type");
		data.mPrice = element->IntAttribute("Price");
		data.mDiamondPrice = element->IntAttribute("DiamondPrice");
		data.mNeedLevel = element->IntAttribute("NeedLevel");
		data.mFirstGold = element->IntAttribute("FirstGold");
		data.mFirstTicket = element->IntAttribute("FirstTicket");
		data.mVIPExp = element->IntAttribute("VIPExp");
		data.mIndex = element->IntAttribute("Index");
		data.mLastGift = element->IntAttribute("LastGift");
		data.mNextGift = element->IntAttribute("NextGift");
		data.mShopType = element->IntAttribute("ShopType");
		data.mCumulativeRecharge = element->IntAttribute("CumulativeRecharge");
		{
			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* ItemCount = element->Attribute("ItemCount");
			std::vector<std::string> vecItemCount;
			boost::split(vecItemCount, ItemCount, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecItemCount.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecItemCount[i].c_str(), &temp))
				{
					data.mItemCount.push_back(temp);
				}
			}
		}
		data.mCount = element->IntAttribute("Count");
		data.mDailyGiftPackage = element->IntAttribute("DailyGiftPackage");
		data.mTimeLimit = element->IntAttribute("TimeLimit");
		data.mCategory = element->IntAttribute("Category");
		data.mBuffID = element->IntAttribute("BuffID");
		data.mRebateValue = element->IntAttribute("RebateValue");
		data.mHotIcon = element->IntAttribute("HotIcon");
		{
			const char* PackageProbability = element->Attribute("PackageProbability");
			std::vector<std::string> vecPackageProbability;
			boost::split(vecPackageProbability, PackageProbability, boost::is_any_of(","));
			float temp;
			for (unsigned int i = 0; i < vecPackageProbability.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToFloat(vecPackageProbability[i].c_str(), &temp))
				{
					data.mPackageProbability.push_back(temp);
				}
			}
		}
		data.mOriginalprice = element->IntAttribute("Originalprice");
		data.mGOLDOriginalprice = element->IntAttribute("GOLDOriginalprice");
		data.mDiamondsOriginalprice = element->IntAttribute("DiamondsOriginalprice");
		data.mGiftCeiling = element->IntAttribute("GiftCeiling");
		data.mLowerGiftLimit = element->IntAttribute("LowerGiftLimit");
		data.mHighestGain = element->IntAttribute("HighestGain");
		data.mTipsDisplay = element->IntAttribute("TipsDisplay");
		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("M_RechangeCFG Loaded. Load Data:%u", mMapData.size());
}

void M_RechangeCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/M_RechangeCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "M_RechangeCFG");
	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_RechangeCFGData data;
		LuaCfgHelper::readInt(L, "ID", data.mID);
		LuaCfgHelper::readString(L, "Name", data.mName);
		LuaCfgHelper::readString(L, "Desc", data.mDesc);
		LuaCfgHelper::readInt(L, "ActivityId", data.mActivityId);
		LuaCfgHelper::readString(L, "Icon", data.mIcon);
		LuaCfgHelper::readString(L, "StartTime", data.mStartTime);
		LuaCfgHelper::readString(L, "EndTime", data.mEndTime);
		LuaCfgHelper::readInt(L, "Group", data.mGroup);
		LuaCfgHelper::readVectorInt(L, "needlevel", data.mneedlevel);
		LuaCfgHelper::readInt(L, "NeedVIP", data.mNeedVIP);
		LuaCfgHelper::readInt(L, "Type", data.mType);
		LuaCfgHelper::readInt(L, "Price", data.mPrice);
		LuaCfgHelper::readInt(L, "DiamondPrice", data.mDiamondPrice);
		LuaCfgHelper::readInt(L, "NeedLevel", data.mNeedLevel);
		LuaCfgHelper::readInt(L, "FirstGold", data.mFirstGold);
		LuaCfgHelper::readInt(L, "FirstTicket", data.mFirstTicket);
		LuaCfgHelper::readInt(L, "VIPExp", data.mVIPExp);
		LuaCfgHelper::readInt(L, "Index", data.mIndex);
		LuaCfgHelper::readInt(L, "LastGift", data.mLastGift);
		LuaCfgHelper::readInt(L, "NextGift", data.mNextGift);
		LuaCfgHelper::readInt(L, "ShopType", data.mShopType);
		LuaCfgHelper::readInt(L, "CumulativeRecharge", data.mCumulativeRecharge);
		LuaCfgHelper::readVectorInt(L, "ItemID", data.mItemID);
		LuaCfgHelper::readVectorInt(L, "ItemCount", data.mItemCount);
		LuaCfgHelper::readInt(L, "Count", data.mCount);
		LuaCfgHelper::readInt(L, "DailyGiftPackage", data.mDailyGiftPackage);
		LuaCfgHelper::readInt(L, "TimeLimit", data.mTimeLimit);
		LuaCfgHelper::readInt(L, "Category", data.mCategory);
		LuaCfgHelper::readInt(L, "BuffID", data.mBuffID);
		LuaCfgHelper::readInt(L, "RebateValue", data.mRebateValue);
		LuaCfgHelper::readInt(L, "HotIcon", data.mHotIcon);
		LuaCfgHelper::readVectorFloat(L, "PackageProbability", data.mPackageProbability);
		LuaCfgHelper::readInt(L, "Originalprice", data.mOriginalprice);
		LuaCfgHelper::readInt(L, "GOLDOriginalprice", data.mGOLDOriginalprice);
		LuaCfgHelper::readInt(L, "DiamondsOriginalprice", data.mDiamondsOriginalprice);
		LuaCfgHelper::readInt(L, "GiftCeiling", data.mGiftCeiling);
		LuaCfgHelper::readInt(L, "LowerGiftLimit", data.mLowerGiftLimit);
		LuaCfgHelper::readInt(L, "HighestGain", data.mHighestGain);
		LuaCfgHelper::readInt(L, "TipsDisplay", data.mTipsDisplay);
		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("M_RechangeCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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