#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <string>
#include <shlobj.h>

class Config {
public:
    static std::wstring GetConfigPath() {
        wchar_t appdata[MAX_PATH];
        if (FAILED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, appdata))) {
            return L"";
        }

        std::wstring dir = appdata;
        dir += L"\\SacredStep";

        CreateDirectoryW(dir.c_str(), NULL);

        return dir + L"\\config.ini";
    }

    static void WriteInt(const wchar_t* section, const wchar_t* key, int value) {
        std::wstring path = GetConfigPath();
        if (path.empty()) return;

        wchar_t buf[32];
        swprintf_s(buf, L"%d", value);
        WritePrivateProfileStringW(section, key, buf, path.c_str());
    }

    static void WriteString(const wchar_t* section, const wchar_t* key, const wchar_t* value) {
        std::wstring path = GetConfigPath();
        if (path.empty()) return;
        WritePrivateProfileStringW(section, key, value, path.c_str());
    }

    static int ReadInt(const wchar_t* section, const wchar_t* key, int defVal = 0) {
        std::wstring path = GetConfigPath();
        if (path.empty()) return defVal;

        wchar_t buf[32];
        GetPrivateProfileStringW(section, key, L"", buf, _countof(buf), path.c_str());
        if (buf[0] == 0) return defVal;

        return _wtoi(buf);
    }

    static std::wstring ReadString(const wchar_t* section, const wchar_t* key, const wchar_t* defVal = L"") {
        std::wstring path = GetConfigPath();
        if (path.empty()) return defVal;

        wchar_t buf[256];
        GetPrivateProfileStringW(section, key, L"", buf, _countof(buf), path.c_str());
        if (buf[0] == 0) return defVal;

        return buf;
    }
};
