import Quickshell
import Quickshell.Io
import QtQuick

import qs.config
import qs.services

ShellRoot {
    IpcHandler {
        target: "displays-test"

        function status(): string {
            const monitor = Displays.monitors.length > 0 ? Displays.monitors[0] : null;
            return JSON.stringify({
                count: Displays.monitors.length,
                name: monitor ? monitor.name : "",
                width: monitor ? monitor.width : 0,
                height: monitor ? monitor.height : 0,
                refresh: monitor ? monitor.refreshRate : 0,
                mode: monitor ? monitor.mode : "",
                scale: monitor ? monitor.scale : 0,
                transform: monitor ? monitor.transform : -1,
                modes: monitor ? monitor.modes.length : 0,
                awaiting: Displays.awaitingConfirmation,
                canConfirm: Displays.canConfirm,
                secondsLeft: Displays.secondsLeft,
                lastError: Displays.lastError,
                overridden: monitor ? Displays.isOverridden(monitor.name) : false
            });
        }

        function applyScale(scale: real): bool {
            const monitor = Displays.monitors[0];
            if (!monitor) return false;
            const mode = monitor.mode;
            return Displays.apply(monitor.name, mode, scale, monitor.transform);
        }

        function refreshIdentityFixture(): string {
            const modes = Displays.normaliseModes([
                "1920x1080@59.94Hz",
                "1920x1080@60.00Hz"
            ]);
            const monitor = { width: 1920, height: 1080, refreshRate: 59.94 };
            return JSON.stringify({
                count: modes.length,
                modes: modes.map(mode => mode.mode),
                selected: modes.filter(mode => Displays.modeIsCurrent(monitor, mode)).map(mode => mode.mode)
            });
        }

        function applyBad(kind: string): bool {
            const monitor = Displays.monitors[0];
            if (!monitor) return false;
            const mode = monitor.mode;
            if (kind === "mode") return Displays.apply(monitor.name, "9999x9999@240", monitor.scale, monitor.transform);
            if (kind === "scale") return Displays.apply(monitor.name, mode, 1.37, monitor.transform);
            if (kind === "dirtyScale") {
                const dirty = Displays.scales.find(scale => !Displays.isScaleClean(mode, scale));
                return dirty === undefined ? false : Displays.apply(monitor.name, mode, dirty, monitor.transform);
            }
            if (kind === "transform") return Displays.apply(monitor.name, mode, monitor.scale, 9);
            if (kind === "output") return Displays.apply("NOPE-1", mode, monitor.scale, monitor.transform);
            return false;
        }

        function confirmChange(): bool { return Displays.confirm(); }
        function revertChange(): void { Displays.revert(); }
        function forget(): void {
            const monitor = Displays.monitors[0];
            if (monitor) Displays.forget(monitor.name);
        }
        function refresh(): void { Displays.refresh(); }
    }
}
