#ifndef LOADCONFIG_H
#define LOADCONFIG_H

#include <iostream>
#include <fstream>
#include <string>
#include <map>

#include "convert.h"

class GlobalConfiguration
{
public:
    std::map<std::string,std::string> data;

    bool loadFile(std::string const & fileName)
    {
        std::cout << "Loading configuration file \"" << fileName << "\" ..." << std::endl;
        std::ifstream ifs(fileName.c_str());
        bool fileLoaded = false;

        fileLoaded = true;
        if (!ifs)
        {
            std::cout << "Configuration file \"" << fileName << "\" wasn't load!" << std::endl;
            fileLoaded = false;
        }

        std::string line;
        while (getline(ifs, line))
        {
            if(line[0] != '%' && line.size() != 0)
            {
                int index = 0;
                for(index = 0; line[index] != ' '; index++);
                std::string id;
                id.append(line.begin(), line.begin()+index);

                for(index++; line[index] != ' '; index++);
                std::string value;
                value.append(line.begin()+index+1, line.end());

                data[id] = value;
            }
        }

        ifs.close();
        return fileLoaded;
    }

    GlobalConfiguration()
    {
    }

    GlobalConfiguration(std::string const & fileName)
    {
        loadFile(fileName);
    }

    std::string getValue(std::string id)
    {
        return data[id];
    }

    int getIntValue(std::string id)
    {
        return StrToInt(data[id]);
    }

    float getFloatValue(std::string id)
    {
        return (float)StrToFloat(data[id]);
    }
};

GlobalConfiguration config;

#endif // LOADCONFIG_H

