pragma Singleton

// One presentation model for state that remains active until it is explicitly
// stopped or naturally completes. Transient confirmations stay in Signal Glass.

import Quickshell
import QtQuick

Singleton {
    id: root

    readonly property var activities: {
        const result = [];

        if (FocusSession.active) {
            result.push({
                kind: "focus",
                glyph: "\u{F051F}",
                label: "Focus session",
                detail: (FocusSession.workspaceLabel || "Focused workspace") + " · " + FocusSession.statusText,
                state: FocusSession.remainingText,
                tone: FocusSession.paused ? "warn" : "accent",
                action: FocusSession.paused ? "Resume" : "Pause"
            });
        }

        if (Caffeine.enabled) {
            result.push({
                kind: "caffeine",
                glyph: "\u{F0F2E}",
                label: "Caffeine",
                detail: "Automatic sleep is blocked",
                state: "On",
                tone: "ok",
                action: "Turn off"
            });
        }

        if (PrivacyState.recordingActive) {
            result.push({
                kind: "recording",
                glyph: "\u{F044A}",
                label: "Screen recording",
                detail: "Panama · " + root.recordingElapsed,
                state: "Live",
                tone: "danger",
                action: "Stop"
            });
        }
        if (PrivacyState.screenSharingActive)
            result.push(root.privacyActivity("screen", "\u{F0379}", "Screen sharing", PrivacyState.screenSharingApp));
        if (PrivacyState.cameraActive)
            result.push(root.privacyActivity("camera", "\u{F0100}", "Camera", PrivacyState.cameraApp));
        if (PrivacyState.microphoneActive)
            result.push(root.privacyActivity("microphone", "\u{F036C}", "Microphone", PrivacyState.microphoneApp));

        if (KdeConnect.transferActive) {
            result.push({
                kind: "phone-transfer",
                glyph: "\u{F03F2}",
                label: "Sending to " + (KdeConnect.transferDeviceName || "Phone"),
                detail: KdeConnect.transferFileName || "Preparing file",
                state: "Sending",
                tone: "accent",
                action: "Cancel"
            });
        }

        return result;
    }

    readonly property int count: activities.length
    readonly property string recordingElapsed: {
        const total = Capture.recordingSeconds;
        const seconds = String(total % 60).padStart(2, "0");
        const minutes = Math.floor(total / 60) % 60;
        const hours = Math.floor(total / 3600);
        return hours > 0
            ? `${hours}:${String(minutes).padStart(2, "0")}:${seconds}`
            : `${minutes}:${seconds}`;
    }

    function privacyActivity(kind: string, glyph: string, label: string, app: string): var {
        return {
            kind,
            glyph,
            label,
            detail: app || "Managed by the application",
            state: "In use",
            tone: "warn",
            action: ""
        };
    }

    function activate(kind: string): void {
        if (kind === "focus")
            FocusSession.activateWorkspace();
        else if (kind === "phone-transfer")
            ShellState.open("quicksettings");
        else if (["recording", "screen", "camera", "microphone"].indexOf(kind) >= 0)
            ShellState.open("activity");
    }

    function invoke(kind: string): void {
        if (kind === "focus")
            FocusSession.pauseOrResume();
        else if (kind === "caffeine")
            Caffeine.toggle();
        else if (kind === "recording")
            Capture.stopRecording();
        else if (kind === "phone-transfer")
            KdeConnect.cancelTransfer();
    }
}
