#pragma once
#include <wx/stdpaths.h>
#include <wx/filename.h>
#include <fstream>
#include <string>

// ── Prefs ─────────────────────────────────────────────────────────────────────
// Tiny file-based preferences store. Saved as a plain key=value text file
// next to the executable so it works identically on Windows and macOS.
//
// File: <exe dir>/prefs.cfg
// Keys:
//   playful=1   Show sassy images (splash cashier, help image, buy tooltip).
//   playful=0   Down-to-business mode: no images.
//   asked=1     One-time experience dialog has already been shown.
// ─────────────────────────────────────────────────────────────────────────────
class Prefs {
public:
    static Prefs& Get() {
        static Prefs instance;
        return instance;
    }

    // Returns true if the one-time dialog has already been shown.
    bool WasAsked() const { return m_asked; }

    // Returns true when playful mode is active (show images).
    bool IsPlayful() const { return m_playful; }

    // Set after the dialog is answered; call Save() afterwards.
    void SetPlayful(bool v) { m_playful = v; m_asked = true; }

    // Load from disk. Call once at startup before any UI is shown.
    void Load() {
        std::ifstream f(Path().ToStdString());
        if (!f.is_open()) return;   // file doesn't exist -> defaults
        std::string line;
        while (std::getline(f, line)) {
            if (line.substr(0, 8) == "playful=")
                m_playful = (line[8] == '1');
            else if (line.substr(0, 6) == "asked=")
                m_asked = (line[6] == '1');
        }
    }

    // Persist to disk.
    void Save() {
        std::ofstream f(Path().ToStdString());
        if (!f.is_open()) return;
        f << "playful=" << (m_playful ? "1" : "0") << "\n";
        f << "asked="   << (m_asked   ? "1" : "0") << "\n";
    }

private:
    Prefs() = default;
    bool m_playful = true;   // default: playful
    bool m_asked   = false;  // default: not yet asked

    static wxString Path() {
        wxFileName exe(wxStandardPaths::Get().GetExecutablePath());
        return exe.GetPath(wxPATH_GET_SEPARATOR) + "prefs.cfg";
    }
};
