// Copyright (C) 2025-2026 Petr Mironychev
// SPDX-License-Identifier: GPL-3.0-or-later
// Additional attribution terms under GPLv3 §7(b) apply — see LICENSE

import QtQuick
import QtQuick.Controls
import QtQuick.Controls.Basic as Basic

Basic.ComboBox {
    id: control

    property real popupContentWidth: 100

    implicitWidth: Math.min(contentItem.implicitWidth + 8, 300)
    implicitHeight: 30

    function updatePopupWidth() {
        var maxWidth = 100;
        if (model) {
            for (var i = 0; i < model.length; i++) {
                textMetrics.text = model[i];
                maxWidth = Math.max(maxWidth, textMetrics.width + 40);
            }
        }
        popupContentWidth = Math.min(maxWidth, 350);
    }

    onModelChanged: updatePopupWidth()
    Component.onCompleted: updatePopupWidth()
    clip: true

    indicator: Image {
        id: dropdownIcon

        x: control.width - width - 10
        y: control.topPadding + (control.availableHeight - height) / 2

        width: 12
        height: 8
        source: palette.window.hslLightness > 0.5
                ? "qrc:/qt/qml/UIControls/icons/dropdown-arrow-light.svg"
                : "qrc:/qt/qml/UIControls/icons/dropdown-arrow-dark.svg"
        sourceSize: Qt.size(width, height)

        rotation: control.popup.visible ? 180 : 0

        Behavior on rotation {
            NumberAnimation { duration: 150; easing.type: Easing.OutQuad }
        }
    }

    background: Rectangle {
        id: bg

        implicitWidth: control.implicitWidth
        implicitHeight: 30
        color: !control.enabled || !control.down ? palette.button : palette.dark
        border.color: !control.enabled || (!control.hovered && !control.visualFocus) 
                      ? palette.mid 
                      : palette.highlight
        border.width: 1
        radius: 5

        Behavior on color {
            ColorAnimation { duration: 150 }
        }

        Behavior on border.color {
            ColorAnimation { duration: 150 }
        }

        Rectangle {
            anchors.fill: bg
            radius: bg.radius
            gradient: Gradient {
                GradientStop { position: 0.0; color: Qt.alpha(palette.highlight, 0.4) }
                GradientStop { position: 1.0; color: Qt.alpha(palette.highlight, 0.2) }
            }
            opacity: control.hovered ? 0.3 : 0.01
            
            Behavior on opacity {
                NumberAnimation { duration: 250 }
            }
        }
    }

    contentItem: Text {
        leftPadding: 10
        rightPadding: 30
        text: control.displayText
        font.pixelSize: 12
        color: palette.text
        verticalAlignment: Text.AlignVCenter
        elide: Text.ElideNone
    }

    popup: Popup {
        y: control.height + 2
        width: Math.max(control.width, control.popupContentWidth)
        implicitHeight: Math.min(contentItem.implicitHeight, 300)
        padding: 4

        contentItem: ListView {
            clip: true
            implicitHeight: contentHeight
            model: control.popup.visible ? control.delegateModel : null
            currentIndex: control.highlightedIndex
            boundsBehavior: ListView.StopAtBounds
            highlightMoveDuration: 0

            ScrollBar.vertical: ScrollBar {
                policy: ScrollBar.AsNeeded
            }
        }

        background: Rectangle {
            color: palette.base
            border.color: Qt.lighter(palette.mid, 1.1)
            border.width: 1
            radius: 5
        }

        enter: Transition {
            NumberAnimation { property: "opacity"; from: 0.0; to: 1.0; duration: 150 }
        }

        exit: Transition {
            NumberAnimation { property: "opacity"; from: 1.0; to: 0.0; duration: 100 }
        }
    }

    delegate: ItemDelegate {
        width: control.popup.width - 8
        height: 32

        contentItem: Text {
            text: modelData
            color: palette.text
            font.pixelSize: 12
            elide: Text.ElideRight
            verticalAlignment: Text.AlignVCenter
            leftPadding: 10
        }

        highlighted: control.highlightedIndex === index

        background: Rectangle {
            radius: 4
            color: highlighted
                   ? Qt.alpha(palette.highlight, 0.2)
                   : parent.hovered
                   ? (palette.window.hslLightness > 0.5
                      ? Qt.darker(palette.base, 1.05)
                      : Qt.lighter(palette.base, 1.15))
                   : "transparent"

            Behavior on color {
                ColorAnimation { duration: 100 }
            }
        }
    }

    TextMetrics {
        id: textMetrics
        font.pixelSize: 12
    }
}

