import QtQuick
import Quickshell
import Quickshell.Wayland

import "../config" as Cfg

PanelWindow {
    id: barWindow

    required property var modelData
    property bool isTop

    readonly property var cfg: Cfg.Config

    color: "transparent"
    anchors {
        top:    isTop
        bottom: !isTop
        left:   true
        right:  true
    }
    implicitHeight: cfg.barHeight

    WlrLayershell.layer: WlrLayer.Top
    WlrLayershell.namespace:     "main-shell-bar"
    WlrLayershell.exclusiveZone: cfg.barHeight

    Item {
        id: animatedWrapper
        anchors.fill: parent
        
        opacity: 0
        scale: 0.95
        y: barWindow.isTop ? -20 : 20 

        Component.onCompleted: barEntrance.start()

        ParallelAnimation {
            id: barEntrance
            
            NumberAnimation { 
                target: animatedWrapper
                property: "opacity"
                from: 0; to: 1
                duration: 400
                easing.type: Easing.OutCubic 
            }
            
            NumberAnimation { 
                target: animatedWrapper
                property: "scale"
                from: 0.95; to: 1.0
                duration: 500
                easing.type: Easing.OutBack 
            }

            NumberAnimation {
                target: animatedWrapper
                property: "y"
                to: 0
                duration: 600
                easing.type: Easing.OutExpo
            }
        }

        Item {
            id: barContent
            anchors.fill:        parent
            anchors.leftMargin:  cfg.margin
            anchors.rightMargin: cfg.margin

            function bindModule(item, side) {
                item.height = Qt.binding(() => barContent.height - 12)

                const props = {
                    "panelRadius":  cfg.radius / 2,
                    "borderWidth":  cfg.panelBorderWidth,
                    "isTop":        barWindow.isTop,
                    "targetScreen": barWindow.modelData,
                    "barHeight":    cfg.barHeight,
                    "barMargin":    cfg.margin,
                    "barSide":      side,
                    "menuWindow":   barWindow
                }
                for (let prop in props) {
                    if (prop in item) item[prop] = props[prop]
                }
            }

            readonly property real sideMaxWidth: Math.max(0,
                (width - centerRow.implicitWidth - 2 * cfg.panelSpacing) / 2)

            Item {
                id: leftContainer
                anchors.left:           parent.left
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height
                width:  Math.max(0, Math.min(leftRow.implicitWidth, barContent.sideMaxWidth))
                clip:   true

                Row {
                    id: leftRow
                    anchors.left:           parent.left
                    anchors.verticalCenter: parent.verticalCenter
                    spacing:         cfg.moduleSpacing
                    transformOrigin: Item.Left
                    scale: leftContainer.width / Math.max(leftContainer.width, implicitWidth)

                    Repeater {
                        model: cfg.leftModules
                        Loader {
                            required property string modelData
                            source: Qt.resolvedUrl("modules/" + modelData + ".qml")
                            onLoaded: barContent.bindModule(item, "left")
                        }
                    }
                }
            }

            Item {
                id: rightContainer
                anchors.right:          parent.right
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height
                width:  Math.max(0, Math.min(rightRow.implicitWidth, barContent.sideMaxWidth))
                clip:   true

                Row {
                    id: rightRow
                    anchors.right:          parent.right
                    anchors.verticalCenter: parent.verticalCenter
                    spacing:         cfg.moduleSpacing
                    transformOrigin: Item.Right
                    scale: rightContainer.width / Math.max(rightContainer.width, implicitWidth)

                    Repeater {
                        model: cfg.rightModules
                        Loader {
                            required property string modelData
                            source: Qt.resolvedUrl("modules/" + modelData + ".qml")
                            onLoaded: barContent.bindModule(item, "right")
                        }
                    }
                }
            }

            Item {
                id: centerContainer
                anchors.verticalCenter: parent.verticalCenter
                height: parent.height
                width: Math.max(0, Math.min(
                           centerRow.implicitWidth,
                           barContent.width
                               - 2 * (Math.max(leftContainer.width, rightContainer.width)
                                      + cfg.panelSpacing)
                       ))
                x: (barContent.width - width) / 2
                clip: true

                Row {
                    id: centerRow
                    anchors.centerIn:  parent
                    spacing:           cfg.moduleSpacing
                    transformOrigin:   Item.Center
                    scale: centerContainer.width / Math.max(centerContainer.width, implicitWidth)

                    Repeater {
                        model: cfg.centerModules
                        Loader {
                            required property string modelData
                            source: Qt.resolvedUrl("modules/" + modelData + ".qml")
                            onLoaded: barContent.bindModule(item, "center")
                        }
                    }
                }
            }
        }
    }
}
