import Quickshell
import Quickshell.Io
import QtQuick

import qs.modules.settings

ShellRoot {
    id: root

    property var calls: []

    function record(call: string): void {
        root.calls = root.calls.concat([call]);
    }

    WallpaperControls {
        id: controls
        width: 620
        outputs: ["DP-2", "HDMI-A-1"]
        mode: "per-monitor"
        setModeAction: mode => root.record("mode:" + mode)
        setIntervalAction: minutes => root.record("interval:" + minutes)
        setShuffleAction: enabled => root.record("shuffle:" + enabled)
    }

    WallpaperPicker {
        id: picker
        width: 620
        selectedOutput: "DP-2"
        mode: "single"
        activeByOutput: ({ "DP-2": "/images/a.jpg", "HDMI-A-1": "/images/b.jpg" })
        slideshowPaths: ["/images/b.jpg", "/images/c.jpg"]
        assignments: ({ "DP-2": "/images/c.jpg" })
        setSingleAction: path => root.record("single:" + path)
        toggleSlideshowAction: path => root.record("toggle:" + path)
        setAssignmentAction: (output, path) => root.record("assign:" + output + ":" + path)
    }

    IpcHandler {
        target: "wallpaper-settings-test"

        function activate(mode: string, path: string): string {
            root.calls = [];
            picker.mode = mode;
            picker.activate(path);
            return JSON.stringify(root.calls);
        }

        function selectOutput(output: string): void {
            controls.selectedOutput = output;
        }

        function updateOutputs(outputs: string): void {
            controls.outputs = outputs === "" ? [] : outputs.split("|");
        }

        function selectedOutput(): string { return controls.selectedOutput; }

        function states(): string {
            picker.mode = "single";
            const single = {
                current: picker.isCurrent("/images/a.jpg"),
                selected: picker.selected("/images/a.jpg")
            };
            picker.mode = "slideshow";
            const slideshow = {
                current: picker.isCurrent("/images/b.jpg"),
                selected: picker.selected("/images/b.jpg")
            };
            picker.mode = "per-monitor";
            const perMonitor = {
                current: picker.isCurrent("/images/c.jpg"),
                selected: picker.selected("/images/c.jpg")
            };
            return JSON.stringify({
                selectedOutput: controls.selectedOutput,
                single,
                slideshow,
                perMonitor
            });
        }
    }
}
