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

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

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

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

void Odds2_Room::Load()
{
	tinyxml2::XMLDocument xmlDoc;
	std::string content = FileUtils::getInstance()->getStringFromFile("Config/Odds2_Room.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)
	{
		Odds2_RoomData data;
		data.mRoomID = element->IntAttribute("RoomID");
		data.mLaserAdd = element->IntAttribute("LaserAdd");
		data.mLaserReduce = element->IntAttribute("LaserReduce");
		data.mFormationAdd = element->IntAttribute("FormationAdd");
		data.mFormationReduce = element->IntAttribute("FormationReduce");
		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("Odds2_Room Loaded. Load Data:%u", mMapData.size());
}

void Odds2_Room::LoadLua()
{
	LuaEngine::getInstance()->executeScriptFile("config/Odds2_Room");
	lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();
	lua_getglobal(L, "Odds2_Room");
	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");
		Odds2_RoomData data;
		LuaCfgHelper::readInt(L, "RoomID", data.mRoomID);
		LuaCfgHelper::readInt(L, "LaserAdd", data.mLaserAdd);
		LuaCfgHelper::readInt(L, "LaserReduce", data.mLaserReduce);
		LuaCfgHelper::readInt(L, "FormationAdd", data.mFormationAdd);
		LuaCfgHelper::readInt(L, "FormationReduce", data.mFormationReduce);
		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("Odds2_Room Loaded. Load Data:%u", mMapData.size());
}

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

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

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