import Quickshell
import Quickshell.Io
import QtQuick

import "services/DisplayLayout.js" as DisplayLayout

ShellRoot {
    readonly property var fixture: [
        { name: "DP-2", width: 4500, height: 3000, scale: 1.5, transform: 0, x: 140, y: 80, primary: true },
        { name: "HDMI-A-1", width: 2560, height: 1440, scale: 1, transform: 1, x: 3140, y: 80, primary: false }
    ]

    IpcHandler {
        target: "display-layout-test"

        function status(): string {
            const normalized = DisplayLayout.normalize(fixture);
            const canvas = DisplayLayout.canvasRects(normalized, 800, 500, 20);
            const near = normalized.map(record => Object.assign({}, record));
            near[1].x = 3016;
            const far = normalized.map(record => Object.assign({}, record));
            far[1].x = 3017;
            return JSON.stringify({
                valid: DisplayLayout.validate(fixture),
                sizes: fixture.map(DisplayLayout.logicalSize),
                normalized: normalized.map(record => ({ name: record.name, x: record.x, y: record.y, primary: record.primary })),
                bounds: canvas.bounds,
                canvasScale: canvas.scale,
                canvasRects: canvas.rects,
                near: DisplayLayout.snap(near, "HDMI-A-1", 16).find(record => record.name === "HDMI-A-1").x,
                far: DisplayLayout.snap(far, "HDMI-A-1", 16).find(record => record.name === "HDMI-A-1").x
            });
        }

        function invalid(): string {
            const base = fixture.map(record => Object.assign({}, record));
            const cases = [];
            const add = layout => cases.push(DisplayLayout.validate(layout));
            add([base[0], Object.assign({}, base[1], { name: "DP-2" })]);
            add(base.map(record => Object.assign({}, record, { primary: false })));
            add(base.map(record => Object.assign({}, record, { primary: true })));
            add([Object.assign({}, base[0], { x: 0.5 }), base[1]]);
            add([Object.assign({}, base[0], { scale: 0 }), base[1]]);
            add([Object.assign({}, base[0], { transform: 4 }), base[1]]);
            add([Object.assign({}, base[0], { width: Infinity }), base[1]]);
            add([Object.assign({}, base[0], { width: 0 }), base[1]]);
            return JSON.stringify(cases);
        }
    }
}
