import QtQuick
import QtQuick.Controls
import QtQuick.Layouts

import "../style"

ItemDelegate {
    id: root

    property int treeDepth: 0
    property bool nodeExpanded: false
    property bool nodeHasChildren: false
    property string nodeName: ""
    property string nodeType: ""
    property string nodeState: ""
    property color nodeStateColor: "#788797"
    property bool nodeHasExceedance: false
    property bool selectedNode: false
    property string nodeKey: ""
    property var sourceNode: null
    property bool pointClickSuppressed: false
    property bool pointDragEnabled: false

    signal expansionRequested()
    signal nodeRequested()

    width: ListView.view ? ListView.view.width : implicitWidth
    height: 38
    leftPadding: 8 + treeDepth * 18
    rightPadding: 8
    hoverEnabled: true

    background: Rectangle {
        color: root.selectedNode || root.down
               ? "#D7E7F8"
               : root.hovered ? "#EDF4FC" : "transparent"
        radius: 5
        border.width: root.selectedNode ? 1 : 0
        border.color: "#005FCC"
    }

    contentItem: RowLayout {
        spacing: 5

        AppToolButton {
            Layout.preferredWidth: 24
            Layout.preferredHeight: 24
            enabled: root.nodeHasChildren
            opacity: root.nodeHasChildren ? 1.0 : 0.0
            text: root.nodeHasChildren
                  ? (root.nodeExpanded ? "▾" : "▸") : ""
            font.pixelSize: 15
            onClicked: root.expansionRequested()
        }

        StateIndicatorDelegate {
            Layout.preferredWidth: 18
            Layout.preferredHeight: 18
            indicatorColor: root.nodeStateColor
            stateText: root.nodeState
            hasExceedance: root.nodeHasExceedance
        }

        ColumnLayout {
            Layout.fillWidth: true
            spacing: 0

            Text {
                renderType: Text.NativeRendering
                Layout.fillWidth: true
                text: root.nodeName.length > 0 ? root.nodeName : "Без имени"
                color: "#111827"
                font.pixelSize: 13
                font.bold: false
                elide: Text.ElideRight
            }

            Text {
                renderType: Text.NativeRendering
                Layout.fillWidth: true
                visible: root.nodeType.length > 0
                text: root.nodeType
                color: "#526274"
                font.pixelSize: 10
                elide: Text.ElideRight
            }
        }
    }

    MouseArea {
        id: pointDragArea

        x: root.leftPadding + (root.nodeHasChildren ? 29 : 0)
        y: 0
        width: Math.max(0, parent.width - x)
        height: parent.height
        z: 20
        enabled: root.pointDragEnabled
        acceptedButtons: Qt.LeftButton
        hoverEnabled: true
        preventStealing: false
        cursorShape: drag.active
                     ? Qt.ClosedHandCursor : Qt.OpenHandCursor
        drag.target: pointDragProxy
        drag.axis: Drag.XAndYAxis
        drag.threshold: 1
        drag.smoothed: false

        onPressed: function(mouse) {
            pointClickSuppressionTimer.stop()
            root.pointClickSuppressed = false
            var overlayPosition = pointDragArea.mapToItem(
                        pointDragProxy.parent, mouse.x, mouse.y)
            pointDragProxy.x = overlayPosition.x - pointDragProxy.width / 2
            pointDragProxy.y = overlayPosition.y - pointDragProxy.height / 2
        }

        onPositionChanged: {
            if (drag.active)
                root.pointClickSuppressed = true
        }

        onClicked: {
            if (!root.pointClickSuppressed)
                root.nodeRequested()
        }

        onReleased: {
            if (pointDragProxy.Drag.active)
                pointDragProxy.Drag.drop()
            if (root.pointClickSuppressed)
                pointClickSuppressionTimer.restart()
        }

        onCanceled: {
            if (root.pointClickSuppressed)
                pointClickSuppressionTimer.restart()
        }
    }

    Item {
        id: pointDragProxy
        property string draggedNodeKey: root.nodeKey
        property string draggedNodeName: root.nodeName
        property string draggedNodeType: root.nodeType
        property var draggedNode: root.sourceNode

        parent: Overlay.overlay ? Overlay.overlay : root
        z: 1000000
        width: 24
        height: 24
        visible: root.pointDragEnabled && pointDragArea.pressed

        Drag.active: pointDragArea.drag.active
        Drag.source: pointDragProxy
        Drag.keys: ["vibro-route-node", "vibro-route-point"]
        Drag.dragType: Drag.Internal
        Drag.supportedActions: Qt.CopyAction
        Drag.proposedAction: Qt.CopyAction
        Drag.hotSpot.x: width / 2
        Drag.hotSpot.y: height / 2
        Drag.onDragFinished: {
            pointDragProxy.x = 0
            pointDragProxy.y = 0
        }

        Rectangle {
            anchors.fill: parent
            radius: width / 2
            color: "#005FCC"
            border.width: 3
            border.color: "#FFFFFF"
        }
    }

    Timer {
        id: pointClickSuppressionTimer
        interval: 500
        repeat: false
        onTriggered: root.pointClickSuppressed = false
    }

    onClicked: nodeRequested()
}
