import Quickshell
import Quickshell.Io
import QtQuick

import qs.config
import qs.services

ShellRoot {
    Component.onCompleted: {
        Wallpaper.outputOverride = ["HDMI-A-1", "DP-2"];
        Wallpaper.candidateOverride = ["/images/a.jpg", "/images/b.jpg", "/images/c.jpg"];
        Wallpaper.startupRestoreEnabled = false;
        Wallpaper.slideshowIntervalOverrideMs = 60000;
        Wallpaper.available = ["/images/a.jpg", "/images/b.jpg", "/images/c.jpg"];
    }

    IpcHandler {
        target: "wallpaper-service-test"

        function applyPerMonitor(): bool {
            return Wallpaper.applyPolicy({
                mode: "per-monitor",
                globalPath: "/images/a.jpg",
                collection: [],
                intervalMinutes: 30,
                shuffle: true,
                assignments: { "HDMI-A-1": "/images/b.jpg" },
                slideshowPath: ""
            }, true);
        }

        function applySingle(path: string): bool {
            return Wallpaper.set(path);
        }

        function seedSlideshow(paths: string, shuffle: bool): void {
            DesktopPreferences.set("wallpaperMode", "slideshow");
            DesktopPreferences.set("wallpaperPath", "/images/a.jpg");
            DesktopPreferences.set("wallpaperSlideshowPaths", JSON.parse(paths));
            DesktopPreferences.set("wallpaperShuffle", shuffle);
            Wallpaper.slideshowPath = "/images/a.jpg";
            Wallpaper.shuffleBag = [];
        }

        function advanceSlideshow(): bool {
            return Wallpaper.advanceSlideshow();
        }

        function rapidOutputs(): void {
            Wallpaper.outputOverride = ["DP-2"];
            Wallpaper.outputOverride = ["HDMI-A-1"];
            Wallpaper.outputOverride = ["DP-2", "HDMI-A-1"];
        }

        function status(): string {
            return JSON.stringify({
                busy: Wallpaper.busy,
                lastError: Wallpaper.lastError,
                activeByOutput: Wallpaper.activeByOutput,
                active: Wallpaper.active,
                mode: DesktopPreferences.get("wallpaperMode"),
                path: DesktopPreferences.get("wallpaperPath"),
                assignments: DesktopPreferences.get("wallpaperPerMonitor"),
                slideshowPath: Wallpaper.slideshowPath,
                shuffleBag: Wallpaper.shuffleBag,
                slideshowTimerRunning: Wallpaper.slideshowTimerRunning,
                scanning: Wallpaper.scanning,
                available: Wallpaper.available
            });
        }
    }
}
