#include "CSaveManager.h"

CSaveManager::CSaveManager(const core::stringw& settings_file,IrrlichtDevice& device)
{
    *xml_file=settings_file;
    *MenuDevice=device;
}

//Exemple
/*<?xml version="1.0"?>
<mygame>
	<video>
		<setting name="driver" value="Direct3D9">
		<setting name="fullscreen" value="0">
		<setting name="resolution" value="1024x768">
	</video>
</mygame>*/
bool CSaveManager::Load()
{
	core::stringw currentSection; //keep track of our current section

	io::IXMLReader* xml = MenuDevice->getFileSystem()->createXMLReader(*xml_file);  //create xml reader
    if (!xml)
        return false;

	//while there is more to read
	while (xml->read())
	{
		//check the node type
		switch (xml->getNodeType())
		{
			//we found a new element
			case irr::io::EXN_ELEMENT:
			{
				 //we currently are in the empty or mygame section and find the video tag so we set our current section to video
                    /*if (currentSection.empty() && videoTag.equals_ignore_case(xml->getNodeName()))
                    {
                        currentSection = videoTag;
                    }
                    //we are in the video section and we find a setting to parse
                    else if (currentSection.equals_ignore_case(videoTag) && settingTag.equals_ignore_case(xml->getNodeName() ))
                    {
                        //read in the key
                        stringw key = xml->getAttributeValueSafe(L"name");
                        //if there actually is a key to set
                        if (!key.empty())
                        {
                            //set the setting in the map to the value,
                            //the [] operator overrides values if they already exist or inserts a new key value
                            //pair into the settings map if it was not defined yet
                            SettingMap[key] = xml->getAttributeValueSafe(L"value");
                        }
                    }*/

                    //..
                    // You can add your own sections and tags to read in here
                    //..
            }
			break;

			//we found the end of an element
			case irr::io::EXN_ELEMENT_END:
				//we were at the end of the video section so we reset our tag
				currentSection=L"";
			break;

		}

		// don't forget to delete the xml reader
        xml->drop();

        return true;
	}
}

bool CSaveManager::Save()
{
	//create xml writer
	io::IXMLWriter* xwriter = MenuDevice->getFileSystem()->createXMLWriter(*xml_file);
	if (!xwriter)
		return false;

	//write out the obligatory xml header. Each xml-file needs to have exactly one of those.
	xwriter->writeXMLHeader();

	//start element save
	xwriter->writeElement(L"save");
	xwriter->writeLineBreak();                  //new line

    //write element as <map name="nomdelacarte" />
    //the second parameter indicates this is an empty element with no children, just attributes
    xwriter->writeElement(L"map",true, L"name", L"ecrins.map");
    xwriter->writeLineBreak();

	//start section with stations settings
	xwriter->writeElement(L"stations");
	xwriter->writeLineBreak();                  //new line

	//for(unsigned i=1;i<stations.size();i++) {
        xwriter->writeElement(L"station",false, L"name", L"Pelvoux", L"level",L"1");
        xwriter->writeLineBreak();
        xwriter->writeElement(L"riders");
        xwriter->writeLineBreak();

        xwriter->writeClosingTag(L"riders");
        xwriter->writeLineBreak();
	//}

	//close stations section
	xwriter->writeClosingTag(L"stations");
	xwriter->writeLineBreak();

	//start section with rms
	xwriter->writeElement(L"rms");
	xwriter->writeLineBreak();

	xwriter->writeClosingTag(L"rms");
	xwriter->writeLineBreak();

	//start section with slopes
	xwriter->writeElement(L"slopes");
	xwriter->writeLineBreak();

	xwriter->writeClosingTag(L"slopes");
	xwriter->writeLineBreak();

	//close save section
	xwriter->writeClosingTag(L"save");

	//delete xml writer
	xwriter->drop();

	return true;
}

// Set setting in our manager
void CSaveManager::setSetting(const core::stringw& name, const core::stringw& value)
{
    SettingMap[name]=value;
}

// set setting overload to quickly assign integers to our setting map
void CSaveManager::setSetting(const core::stringw& name, s32 value)
{
    SettingMap[name]=core::stringw(value);
}

// Get setting as string
core::stringw CSaveManager::getSetting(const core::stringw& key) const
{
    //the find function or irrmap returns a pointer to a map Node
    //if the key can be found, otherwise it returns null
    //the map node has the function getValue and getKey, as we already know the key, we return node->getValue()
    core::map<core::stringw, core::stringw>::Node* n = SettingMap.find(key);
    if (n)
        return n->getValue();
    else
        return L"";
}

//
bool CSaveManager::getSettingAsBoolean(const core::stringw& key ) const
{
    core::stringw s = getSetting(key);
    if (s.empty())
        return false;
    return s.equals_ignore_case(L"1");
}

//
s32 CSaveManager::getSettingAsInteger(const core::stringw& key) const
{
    //we implicitly cast to string instead of stringw because strtol10 does not accept wide strings
    const core::stringc s = getSetting(key);
    if (s.empty())
        return 0;

    return core::strtol10(s.c_str());
}

CSaveManager::~CSaveManager()
{

}
