/*!
 *
 * Copyright (c) 2021-2025 Diality Inc. - All Rights Reserved.
 * \copyright
 * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN
 * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER.
 *
 * \file    BaseComboBox.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      21-Aug-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  21-Aug-2025
 *
 */

// Qt
import QtQuick                  2.12
import QtQuick.Controls         2.12

//  Qml imports
import "qrc:/globals"

ComboBox { id: _root
    property bool isActive          : true // default leave as regular ComboBox behavior
    property alias iconSource       : _icon.source
    property alias iconAnchors      : _icon.anchors
    property alias backgroundColor  : _background.color
    property alias dropDownWidth    : _popup.width
    property int  delegateWidth     : dropDownWidth
    property int  delegateHeight    : height
    property bool   canOff          : false
    property bool centerHorizontally: false


    width                   : 300
    displayText             : _root.isActive ? currentText : Variables.emptyEntry
    currentIndex            : 0
    font.pixelSize          : Fonts.fontPixelTextRectTitle
    leftPadding             : centerHorizontally || ! _root.isActive ? 30 : 10

    signal clear()

    onClear                 : { currentIndex    = 0 }

    contentItem: Text { id: _displayText
        text                : canOff && parent.displayText === "0" ? qsTr("OFF") : parent.displayText
        color               : Colors.offWhite
        font.pixelSize      : _root.isActive ? Fonts.fontPixelTextRectTitle : Fonts.fontPixelValueControl
        verticalAlignment   : Text.AlignVCenter
        horizontalAlignment : centerHorizontally || ! _root.isActive ? Text.AlignHCenter : undefined
        rightPadding        : _root.isActive ? 15 : 0
        clip                : true
        elide               : Text.ElideRight
    }

    background: Rectangle { id: _background
        color   : Colors.comboBoxDisplay
        radius  : Variables.dialogRadius
    }

    delegate: ItemDelegate { id: _delegate
        width       : _root.delegateWidth
        height      : _root.delegateHeight
        highlighted : _root.highlightedIndex === index

        contentItem: Text {
            text                : canOff && modelData === "0" ? qsTr("OFF") :  modelData
            color               : Colors.offWhite
            font                : _root.font
            verticalAlignment   : Text.AlignVCenter
            horizontalAlignment : centerHorizontally ? Text.AlignHCenter : undefined
            leftPadding         : 10
            elide               : Text.ElideRight
        }

        background: Rectangle {
            anchors.fill    : parent
            visible         : _delegate.down || _delegate.highlighted || _delegate.visualFocus
            color           : _delegate.down ?  Colors.backgroundButtonSelect :
                                                Colors.backgroundButtonSelectDark
            radius          : Variables.dialogRadius
        }
    }

    popup: Popup { id: _popup
        y               : _root.height + 10
        x               : 0
        width           : _root.width
        implicitHeight  : contentItem.implicitHeight

        contentItem: ListView {
            clip            : true
            implicitHeight  : contentHeight
            currentIndex    : _root.highlightedIndex
            model           : _root.popup.visible ? _root.delegateModel : null
        }

        background: Rectangle {
            color   : Colors.treatmentSectionHeader
            radius  : Variables.dialogRadius
        }
    }

    indicator: Image { id: _icon
        source                  : enabled ? "qrc:/images/iChevronDown" : ""
        anchors.verticalCenter  : parent.verticalCenter
        anchors.right           : parent.right
        anchors.rightMargin     : Variables.defaultMargin
        width                   : Variables.iconButtonSize
        height                  : width
    }
}
