import Quickshell
import Quickshell.Io
import QtQuick

import "services/WallpaperPolicy.js" as WallpaperPolicy

ShellRoot {
    readonly property var candidates: ["/images/a.jpg", "/images/b.jpg", "/images/c.jpg"]
    readonly property var outputs: ["DP-2", "HDMI-A-1"]

    IpcHandler {
        target: "wallpaper-policy-test"

        function status(): string {
            const collection = WallpaperPolicy.validCollection([
                "/images/a.jpg", "/invalid.jpg", "/images/b.jpg",
                "/images/a.jpg", "relative.jpg", "/images/c.jpg"
            ], candidates);
            const assignments = WallpaperPolicy.validAssignments({
                "DP-2": "/images/b.jpg",
                "HDMI-A-1": "/invalid.jpg",
                "bad connector!": "/images/c.jpg"
            }, candidates);
            return JSON.stringify({
                validAbsolute: WallpaperPolicy.validPath("/images/a.jpg", candidates),
                invalidComma: WallpaperPolicy.validPath("/images/a,b.jpg", candidates),
                invalidUnknown: WallpaperPolicy.validPath("/images/nope.jpg", candidates),
                collection,
                assignments,
                single: WallpaperPolicy.effectiveMap(
                    "single", "/images/a.jpg", "", assignments, outputs, candidates),
                perMonitor: WallpaperPolicy.effectiveMap(
                    "per-monitor", "/images/a.jpg", "", assignments, outputs, candidates),
                slideshow: WallpaperPolicy.effectiveMap(
                    "slideshow", "/images/a.jpg", "/images/c.jpg", assignments, outputs, candidates),
                ordered: [
                    WallpaperPolicy.orderedNext(collection, "/images/a.jpg"),
                    WallpaperPolicy.orderedNext(collection, "/images/b.jpg"),
                    WallpaperPolicy.orderedNext(collection, "/images/c.jpg")
                ]
            });
        }

        function shuffle(): string {
            const values = [0.8, 0.1, 0.6, 0.2, 0.9, 0.4];
            let index = 0;
            const random = () => values[index++ % values.length];
            let state = { bag: [], current: "" };
            const emitted = [];
            for (let count = 0; count < 4; count++) {
                state = WallpaperPolicy.shuffledNext(candidates, state.bag, state.current, random);
                emitted.push(state.path);
                state.current = state.path;
            }
            return JSON.stringify({ emitted, remaining: state.bag });
        }
    }
}
