import QtQuick
import QtQuick.Controls
import QtQuick.Layouts
import ISC
import ISC.Wafer

Item {
    id: root
    property real blend: 0.0
    property bool showLabels: true
    property alias showExtremes: map.showExtremes
    property alias showThickness: map.showThickness
    property alias hasThickness: map.hasThickness

    WaferMapItem {
        id: map
        anchors.fill: parent
        sensors: streamController.sensorLayout      // [{label,x,y}]
        values: streamController.sensorValues       // [float]
        bands: streamController.sensorBands         // ["in_range"|"high"|"low"]
        shape: streamController.waferShape
        size: streamController.waferSize
        target: streamController.target
        margin: streamController.margin
        blend: root.blend
        showLabels: root.showLabels
        // Bind to Theme so colors update on dark/light mode switch
        ringColor: Theme.waferRingColor
        axisColor: Theme.waferAxisColor
        lowColor: Theme.sensorLow
        inRangeColor: Theme.sensorInRange
        highColor: Theme.sensorHigh
        textColor: Theme.headingColor

        // Grows the hovered marker smoothly; hoverScale setter triggers a repaint.
        hoverScale: 1.0 + 0.35 * hoverPulse
        property real hoverPulse: map.hoveredIndex >= 0 ? 1.0 : 0.0
        Behavior on hoverPulse {
            NumberAnimation { duration: 150; easing.type: Easing.OutQuad }
        }

        HoverHandler {
            id: hoverHandler
            onPointChanged: {
                map.hoveredIndex = streamController.mode === "live"
                    ? -1
                    : map.which_marker(hoverHandler.point.position.x, hoverHandler.point.position.y);
            }
            onHoveredChanged: if (!hoverHandler.hovered) map.hoveredIndex = -1
            cursorShape: map.hoveredIndex >= 0 ? Qt.PointingHandCursor : Qt.ArrowCursor
        }

        TapHandler {
            onTapped: ev => {
                if (streamController.mode === "live")
                    return;
                var idx = map.which_marker(ev.position.x, ev.position.y);
                if (idx >= 0)
                    replaceDialog.openFor(idx);
            }
        }
    }

    ReplaceSensorDialog {
        id: replaceDialog
    }

    // ── Threshold legend: what Low/In-range/High mean, and whether the
    // band is auto-derived (frame mean ± 1σ) or the manual set point.
    // Same computation in live and review, so no mode-specific text needed —
    // the resolved values below just reflect whichever frame is current.
    Rectangle {
        id: thresholdLegend
        visible: streamController.sensorValues.length > 0
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.margins: 10
        radius: Theme.radiusSm
        color: Theme.sidePanelBackground
        opacity: 0.9
        border.color: Theme.sideBorder
        border.width: Theme.borderThin
        implicitWidth: legendRow.implicitWidth + 20
        implicitHeight: legendRow.implicitHeight + 12

        RowLayout {
            id: legendRow
            anchors.centerIn: parent
            spacing: 14

            Row {
                spacing: 10
                Row {
                    spacing: 8
                    Rectangle { width: 8; height: 8; radius: 4; color: Theme.sensorLow; anchors.verticalCenter: parent.verticalCenter }
                    Label { text: "Low"; font.pixelSize: Theme.fontXs; color: Theme.sideMutedText; anchors.verticalCenter: parent.verticalCenter }
                }
                Row {
                    spacing: 8
                    Rectangle { width: 8; height: 8; radius: 4; color: Theme.sensorInRange; anchors.verticalCenter: parent.verticalCenter }
                    Label { text: "In-range"; font.pixelSize: Theme.fontXs; color: Theme.sideMutedText; anchors.verticalCenter: parent.verticalCenter }
                }
                Row {
                    spacing: 8
                    Rectangle { width: 8; height: 8; radius: 4; color: Theme.sensorHigh; anchors.verticalCenter: parent.verticalCenter }
                    Label { text: "High"; font.pixelSize: Theme.fontXs; color: Theme.sideMutedText; anchors.verticalCenter: parent.verticalCenter }
                }
            }

            Rectangle {
                Layout.preferredWidth: Theme.borderThin
                Layout.preferredHeight: 14
                Layout.alignment: Qt.AlignVCenter
                color: Theme.sideBorder
            }

            Label {
                text: (streamController.thresholdAuto ? "Auto · " : "Manual · ")
                    + streamController.target.toFixed(1) + " ± " + streamController.margin.toFixed(1) + "°C"
                font.pixelSize: Theme.fontXs
                color: Theme.headingColor
            }
        }
    }

    function exportImage(filePath, extra) {
        return map.export_image(filePath, extra || "");
    }

    function loadThickness(filePath) {
        return map.loadThickness(filePath);
    }
}
