import Quickshell
import Quickshell.Io
import QtQuick

import qs.modules.settings
import qs.services

ShellRoot {
    DisplayIdentify {}

    QtObject {
        id: fixtureService

        property var monitors: [
            {
                name: "DP-2", description: "Primary display", width: 4500, height: 3000,
                refreshRate: 60, mode: "4500x3000@60.00", scale: 1.5,
                transform: 0, x: 0, y: 0, primary: true
            },
            {
                name: "HDMI-A-1", description: "Second display", width: 2560, height: 1440,
                refreshRate: 60, mode: "2560x1440@60.00", scale: 1,
                transform: 0, x: 3000, y: 0, primary: false
            }
        ]
        property var applied: []

        function currentLayout(): var {
            return monitors.map(record => Object.assign({}, record));
        }

        function applyLayout(layout: var): bool {
            applied = layout.map(record => Object.assign({}, record));
            return true;
        }
    }

    DisplayArrangement {
        id: arrangement
        width: 800
        displayService: fixtureService
        selectedOutput: "HDMI-A-1"
    }

    IpcHandler {
        target: "display-arrangement-test"

        function status(width: int): string {
            arrangement.width = width;
            arrangement.resetDraft();
            return JSON.stringify(arrangement.canvasSnapshot());
        }

        function dragFixture(): string {
            arrangement.resetDraft();
            arrangement.setDraftPosition("HDMI-A-1", 3016, 0, true);
            arrangement.applyDraft();
            return JSON.stringify(fixtureService.applied);
        }

        function keyboardFixture(): string {
            arrangement.resetDraft();
            arrangement.nudge("HDMI-A-1", -10, 0);
            const afterArrow = arrangement.draftLayout.find(record => record.name === "HDMI-A-1").x;
            arrangement.nudge("HDMI-A-1", -100, 0);
            const afterShiftArrow = arrangement.draftLayout.find(record => record.name === "HDMI-A-1").x;
            return JSON.stringify({ afterArrow, afterShiftArrow });
        }

        function primaryFixture(): string {
            arrangement.resetDraft();
            arrangement.makePrimary("HDMI-A-1");
            return JSON.stringify(fixtureService.applied.map(record => ({
                name: record.name, x: record.x, y: record.y, primary: record.primary
            })));
        }

        function identify(): void { Displays.identify(); }
        function identifying(): bool { return Displays.identifying; }
    }
}
