import QtQuick
import QtQuick.Templates as T
import QtQuick.Controls
import QtQuick.Controls.impl

T.ComboBox {
    id: control

    font: Typography.body
    implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
                             implicitContentHeight + topPadding + bottomPadding,
                             implicitIndicatorHeight + topPadding + bottomPadding)


    contentItem: T.TextField {
        id: test
        autoScroll: control.editable
        color: Colors.foreground
        enabled: control.editable

        font: Typography.body
        implicitHeight: Typography.body.pixelSize + topPadding + bottomPadding
        inputMethodHints: control.inputMethodHints
        padding: Dimensions.m
        readOnly: control.down
        selectByMouse: control.selectTextByMouse
        text: control.editable ? control.editText : control.displayText
        validator: control.validator
        width: control.width - indicator.width
        verticalAlignment: Text.AlignVCenter
    }
    background: Rectangle {
        border.color: Colors.interactive
        border.width: 1
        color: Colors.mantle
        radius: Dimensions.radius
        width: parent.width
    }

    delegate: ItemDelegate {
        required property var model
        required property int index

        width: ListView.view.width
        text: model[control.textRole]
        highlighted: control.highlightedIndex === index
        hoverEnabled: control.hoverEnabled
    }
    indicator: Rectangle {
        id: indicator

        border.color: Colors.interactive
        bottomRightRadius: Dimensions.radius
        color: Colors.primary
        height: control.height
        topRightRadius: Dimensions.radius
        width: 20 + Dimensions.s * 2
        x: control.width - width
        y: 0
        z: 2

        IconLabel {
            anchors.fill: parent
            bottomPadding: Dimensions.s
            icon.color: Colors.foreground
            icon.source: "qrc:/images/ChevronDown.svg"
            leftPadding: Dimensions.s
            rightPadding: Dimensions.s
            topPadding: Dimensions.s
        }
        MouseArea {
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor

            onPressed: () => {
                control.popup.visible = true;
                control.popup.forceActiveFocus()
            }
        }
    }
    popup: T.Popup {
        bottomMargin: 6
        height: Math.min(contentItem.implicitHeight + 2, control.Window.height - topMargin - bottomMargin)
        padding: 1
        topMargin: 6
        width: control.width
        y: control.height

        background: Rectangle {
            border.color: Colors.interactive
            color: Colors.mantle
            radius: Dimensions.radius
        }
        contentItem: ListView {
            clip: true
            currentIndex: control.highlightedIndex
            implicitHeight: contentHeight
            model: control.popup.visible ? control.delegateModel : null

            T.ScrollBar.vertical: ScrollBar {
            }
            highlight: Rectangle {
                color: Colors.primary
                opacity: Colors.highlightOpacity
            }
        }
    }
}
