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

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

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

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

void Fish_ShopCFG::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Fish_ShopCFG.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_ShopCFGData data;
		data.mShopID = element->IntAttribute("ShopID");
		data.mItemID = element->IntAttribute("ItemID");
		data.mItemCount = element->IntAttribute("ItemCount");
		data.mConsumeType = element->IntAttribute("ConsumeType");
		data.mKnapsackType = element->IntAttribute("KnapsackType");
		data.mConsumeCount = element->IntAttribute("ConsumeCount");
		{
			const char* ShowType = element->Attribute("ShowType");
			std::vector<std::string> vecShowType;
			boost::split(vecShowType, ShowType, boost::is_any_of(","));
			int temp;
			for (unsigned int i = 0; i < vecShowType.size(); i++)
			{
				if (tinyxml2::XMLUtil::ToInt(vecShowType[i].c_str(), &temp))
				{
					data.mShowType.push_back(temp);
				}
			}
		}
		data.mSynthesisId = element->IntAttribute("SynthesisId");
		data.mNeedVIP = element->IntAttribute("NeedVIP");
		data.mShopTab = element->IntAttribute("ShopTab");
		data.mRelease = element->IntAttribute("Release");
		data.mReleaseVIP = element->IntAttribute("ReleaseVIP");
		data.mTurrestId = element->IntAttribute("TurrestId");
		if (mMapData.find(data.mShopID) != mMapData.end())std::cout <<"data refind:" << data.mShopID << std::endl;
		CCASSERT(mMapData.find(data.mShopID) == mMapData.end(), "data.mShopID is exists");
		mMapData.insert(std::make_pair(data.mShopID, data));
		element = element->NextSiblingElement();
	}
	CCLOG("Fish_ShopCFG Loaded. Load Data:%u", mMapData.size());
}

void Fish_ShopCFG::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Fish_ShopCFG");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Fish_ShopCFG");
	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_ShopCFGData data;
		LuaCfgHelper::readInt(L, "ShopID", data.mShopID);
		LuaCfgHelper::readInt(L, "ItemID", data.mItemID);
		LuaCfgHelper::readInt(L, "ItemCount", data.mItemCount);
		LuaCfgHelper::readInt(L, "ConsumeType", data.mConsumeType);
		LuaCfgHelper::readInt(L, "KnapsackType", data.mKnapsackType);
		LuaCfgHelper::readInt(L, "ConsumeCount", data.mConsumeCount);
		LuaCfgHelper::readVectorInt(L, "ShowType", data.mShowType);
		LuaCfgHelper::readInt(L, "SynthesisId", data.mSynthesisId);
		LuaCfgHelper::readInt(L, "NeedVIP", data.mNeedVIP);
		LuaCfgHelper::readInt(L, "ShopTab", data.mShopTab);
		LuaCfgHelper::readInt(L, "Release", data.mRelease);
		LuaCfgHelper::readInt(L, "ReleaseVIP", data.mReleaseVIP);
		LuaCfgHelper::readInt(L, "TurrestId", data.mTurrestId);
		if (mMapData.find(data.mShopID) != mMapData.end())std::cout <<"data refind:" << data.mShopID << std::endl;
		CCASSERT(mMapData.find(data.mShopID) == mMapData.end(), "data.mShopID is exists");
		mMapData.insert(std::make_pair(data.mShopID, data));
		lua_pop(L, 1);
	}
	lua_settop(L, 0);
	CCLOG("Fish_ShopCFG Loaded. Load Data:%u", mMapData.size());
}

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

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

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