#include <stdexcept>
#include <tinyxml2.h>
#include "config.h"

using namespace tinyxml2;

namespace config{

namespace window{
    vec2i size;
    int monitor, samples;
}

void init()
{
    XMLDocument doc;
    if(doc.LoadFile("config.xml"))
    {
        window::size = vec2i(1024,600);
        window::monitor = -1;
        window::samples = 0;
        save();
        return;
    }

    XMLElement *root = doc.RootElement();

    if(XMLElement *el = root->FirstChildElement("window"))
    {
        window::size.x = el->IntAttribute("width");
        window::size.y = el->IntAttribute("height");

        window::monitor = el->IntAttribute("monitor");
        window::samples = el->IntAttribute("samples");
    }
}

void save()
{
    XMLDocument doc;
    doc.InsertFirstChild(doc.NewElement("config"));

    XMLElement *el = doc.NewElement("window");

    el->SetAttribute("width", window::size.x);
    el->SetAttribute("height", window::size.y);
    el->SetAttribute("monitor", window::monitor);
    el->SetAttribute("samples", window::samples);

    doc.RootElement()->InsertEndChild(el);

    doc.SaveFile("config.xml");
}

}
