#include <string>
#include "Utils/json/Manager.hpp"

namespace json
{
    CManager* CManager::m_instance = nullptr;
    
    CManager::CManager()
		:m_CurrentOpenedFile("")
    {
        m_pcDocument = nullptr;
    }
    
    CManager::~CManager()
    {
    }
    
    CManager* CManager::getInstance()
    {
        if(!m_instance)
        {
            m_instance = new CManager();
        }
        return m_instance;
    }
    
    bool CManager::openFile(const string& kstrFileFullName)
    {
        string clearContent = cocos2d::FileUtils::getInstance()->getStringFromFile(kstrFileFullName);
        
        m_pcDocument.Parse<0>(clearContent.c_str());
        
        if(m_pcDocument.HasParseError())
        {
			//std::string s = "CManager::openFile parse failed with error nr " + std::to_string((int)m_pcDocument.GetParseError());
			//CCASSERT(false, s.c_str());
            cocos2d::log("CManager::openFile parse failed with error nr. %d", (int)m_pcDocument.GetParseError());
            return false;
        }
		m_CurrentOpenedFile = kstrFileFullName;
		return true;
    }
    
    string CManager::getStringForKey(const string& kstrKey)
    {
        return m_pcDocument[kstrKey.c_str()].GetString();
    }
    
    int CManager::getIntForKey(const string& kstrKey)
    {
        return m_pcDocument[kstrKey.c_str()].GetInt();
    }

	Document* CManager::getOpenedDocument()
	{
		if (m_CurrentOpenedFile.length() > 0)
		{
			return &m_pcDocument;
		}
		else
		{
			return nullptr;
		}
	}

}
