pragma Singleton

// ─────────────────────────────────────────────────────────────────────────────
// Saved window layouts: what is on disk, and getting rid of the ones that are
// not worth keeping.
//
// Saving deliberately does not live here. A layout is worth recording at the
// moment you have it arranged, and that moment is not one to interrupt by going
// to find a settings page -- so it is a launcher command, and this is where
// they are reviewed and pruned afterwards.
//
// Opening is offered too, but as a convenience rather than the main route: from
// the launcher it is two keystrokes and never leaves the keyboard.
// ─────────────────────────────────────────────────────────────────────────────

import Quickshell
import Quickshell.Io
import QtQuick
import qs.config

Singleton {
    id: root

    readonly property string helper: Quickshell.shellDir + "/scripts/panama-project"

    property var projects: []
    property bool busy: false
    property string lastError: ""

    Process {
        id: listRun
        command: [root.helper, "list"]
        stdout: StdioCollector {
            onStreamFinished: {
                try {
                    const answer = JSON.parse(this.text);
                    root.projects = Array.isArray(answer.projects) ? answer.projects : [];
                } catch (error) {
                    root.lastError = "Could not read the saved projects.";
                }
            }
        }
        onExited: root.busy = false
    }

    Process {
        id: actionRun
        stdout: StdioCollector {
            onStreamFinished: {
                try {
                    const answer = JSON.parse(this.text);
                    if (answer.ok !== true) {
                        // The one worth saying out loud: a project needs as many
                        // free workspaces as it was saved with, and being told
                        // "nothing happened" would be useless.
                        root.lastError = answer.error === "not-enough-free-workspaces"
                            ? `Needs ${answer.needed} free workspaces; ${answer.available} are free.`
                            : "That did not work.";
                    }
                } catch (error) {
                    root.lastError = "That did not work.";
                }
            }
        }
        onExited: {
            root.busy = false;
            root.refresh();
        }
    }

    function refresh(): void {
        if (!listRun.running)
            listRun.running = true;
    }

    function open(name: string): void {
        if (actionRun.running)
            return;
        root.lastError = "";
        root.busy = true;
        actionRun.command = [root.helper, "open", name];
        actionRun.running = true;
    }

    function remove(name: string): void {
        if (actionRun.running)
            return;
        root.lastError = "";
        root.busy = true;
        actionRun.command = [root.helper, "delete", name];
        actionRun.running = true;
    }

    Component.onCompleted: root.refresh()
}
