import QtQuick
import QtQuick.Layouts
import QtQuick.Window

import "../style"

Window {
    id: root

    default property alias panelContent: panelContentLayout.data

    property string panelTitle: ""
    property string panelCounterText: ""
    property string densityMode: "normal"
    // Detached panels are native windows from their first platform surface.
    // This keeps the client geometry and the DPI-scaled Windows frame in sync.
    property bool systemWindowMode: true
    property bool locked: false
    property int contentMargins: 10
    property int contentSpacing: 8
    property int dragOffset: contentMargins

    signal dockRequested()
    signal windowDragUpdated(real globalX, real globalY)
    signal windowDragFinished(real globalX, real globalY)
    signal lockToggled(bool locked)

    visible: false
    transientParent: null
    flags: systemWindowMode
           ? Qt.Window | Qt.CustomizeWindowHint | Qt.WindowTitleHint
             | Qt.WindowSystemMenuHint | Qt.WindowMinimizeButtonHint
             | Qt.WindowMaximizeButtonHint | Qt.WindowCloseButtonHint
           : Qt.Window | Qt.FramelessWindowHint
    color: applicationStyle.backgroundColor
    title: panelTitle

    function showWithCurrentGeometry(keepPosition) {
        var requestedWidth = Math.max(minimumWidth, Math.round(width))
        var requestedHeight = Math.max(minimumHeight, Math.round(height))
        var requestedX = Math.round(x)
        var requestedY = Math.round(y)
        showNormal()
        // showNormal() may restore geometry saved by Windows. Reapply the
        // panel geometry after the native frame exists so the swapchain and
        // QML content item receive exactly the same client size.
        width = requestedWidth
        height = requestedHeight
        if (keepPosition) {
            x = requestedX
            y = requestedY
        }
        Qt.callLater(function() {
            if (!root.visible)
                return
            root.width = requestedWidth
            root.height = requestedHeight
            if (keepPosition && !moveTracker.active) {
                root.x = requestedX
                root.y = requestedY
            }
        })
    }

    function openPanel() {
        systemWindowMode = true
        showWithCurrentGeometry(false)
        raise()
        requestActivate()
    }

    function openPanelForDrag(globalX, globalY,
                              pressOffsetX, pressOffsetY) {
        // Create the detached panel with its native frame immediately.  A
        // late frameless -> native transition recreates the Win32 surface
        // during a drag and may expose an unpainted black strip.
        systemWindowMode = true
        x = Math.round(globalX - pressOffsetX - dragOffset)
        y = Math.round(globalY - pressOffsetY - dragOffset)
        showWithCurrentGeometry(true)
        raise()
        requestActivate()
        moveTracker.begin(globalX, globalY)
        startSystemMove()
    }

    onClosing: function(close) {
        root.dockRequested()
    }

    ApplicationStyle {
        id: applicationStyle
        densityMode: root.densityMode
    }

    Rectangle {
        anchors.fill: parent
        color: applicationStyle.backgroundColor
        z: -1000
    }

    PanelTitleBar {
        id: embeddedTitleBar

        anchors.left: parent.left
        anchors.right: parent.right
        anchors.top: parent.top
        anchors.leftMargin: root.contentMargins
        anchors.rightMargin: root.contentMargins
        anchors.topMargin: root.contentMargins
        height: visible ? implicitHeight : 0
        visible: !root.systemWindowMode
        densityMode: root.densityMode
        title: root.panelTitle
        counterText: root.panelCounterText
        lockButtonVisible: true
        locked: root.locked
        moveWindow: root.locked ? null : root
        closeButtonVisible: true
        onLockToggled: function(locked) {
            root.lockToggled(locked)
        }
        onCloseRequested: root.close()
        onWindowMoveStarted: function(globalX, globalY) {
            moveTracker.begin(globalX, globalY)
        }
        onWindowMoveUpdated: function(globalX, globalY) {
            moveTracker.pointerMoved(globalX, globalY)
        }
        onWindowMoveFinished: function(globalX, globalY) {
            moveTracker.finish(globalX, globalY)
        }
    }

    ColumnLayout {
        id: panelContentLayout

        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        anchors.top: embeddedTitleBar.visible
                     ? embeddedTitleBar.bottom : parent.top
        anchors.leftMargin: root.contentMargins
        anchors.rightMargin: root.contentMargins
        anchors.bottomMargin: root.contentMargins
        anchors.topMargin: embeddedTitleBar.visible
                           ? root.contentSpacing : root.contentMargins
        spacing: 0
    }

    Rectangle {
        anchors.fill: parent
        color: "transparent"
        border.width: applicationStyle.borderWidth
        border.color: applicationStyle.borderColor
        visible: !root.systemWindowMode
        z: 900
    }

    WindowResizeHandles {
        anchors.fill: parent
        targetWindow: root
        // Native windows already have DPI-aware system resize borders.
        enabled: !root.systemWindowMode && !root.locked
        visible: enabled
        z: 1000
    }

    WindowGeometryLock {
        targetWindow: root
        locked: root.locked
    }

    DockWindowMoveTracker {
        id: moveTracker

        targetWindow: root
        onDragUpdated: function(globalX, globalY) {
            root.windowDragUpdated(globalX, globalY)
        }
        onDragFinished: function(globalX, globalY) {
            root.windowDragFinished(globalX, globalY)
        }
    }
}
