/*!
 *
 * Copyright (c) 2025-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    ValueAdjusterCustom.qml
 * \author  (last)      Nico Ramirez
 * \date    (last)      20-Nov-2025
 * \author  (original)  Nico Ramirez
 * \date    (original)  20-Nov-2025
 *
 */

import "qrc:/globals"
import "qrc:/components"

// Qt
import QtQuick 2.12
import QtQuick.Controls 2.12

Item { id: _root
    property var model                  : []
    property int currentIndex           : 0
    property int length                 : model.length === 0 ? 0 : model.length
    readonly property bool canIncrement : _root.currentIndex < _root.length - 1
    readonly property bool canDecrement : _root.currentIndex > 0
    property bool           grabbed     : false
    property bool          canOff       : false
    property bool           editable    : true
    property int            textWidth   : 230

    Slider { id: _slider
        property real pos           : 0

        anchors.fill        : parent
        anchors.rightMargin : Variables.defaultMargin * 3
        anchors.leftMargin  : Variables.defaultMargin * 2
        anchors.topMargin   : 5
        opacity             : 0
        stepSize            : 1
        from                : 0
        to                  : _root.length - 1
        value               : _root.currentIndex
        snapMode            : Slider.SnapOnRelease
        enabled             : _root.editable

        background: Rectangle {
            color: "transparent"
            Rectangle {
                anchors{
                    top         : parent.top
                    topMargin   : 1.5
                    left        : parent.left
                    right       : parent.right
                    rightMargin : Variables.defaultMargin * -1
                }
                height  : 1
                width   : parent.width
                color   : Colors.panelBorderColor
            }
        }

        handle: Rectangle { id: _knob
            width   : 20
            height  : 4
            radius  : height
            color   : Colors.borderButton
            x       : _slider.pos * _slider.width
        }

        MouseArea { id: _sliderMouseArea
            anchors.fill        : parent
            pressAndHoldInterval: 0
            drag.axis           : Drag.XAxis      // only horizontal

            onClicked: {
                _slider.opacity = 0
            }

            onReleased: {
                _root.grabbed = false
                _slider.opacity = 0
            }

            onPressAndHold: {
                _root.grabbed = true
            }

            onPositionChanged: {
                if (_root.grabbed) {
                    if ( _slider.opacity === 0 ) { _animator.start() }

                    _slider.pos = Math.max(0, Math.min(1, mouse.x / parent.width))
                    let raw =  _slider.from + _slider.pos * (_slider.to - _slider.from)
                    let stepped = Math.round((raw - _slider.from) / _slider.stepSize) * _slider.stepSize + _slider.from

                    _root.currentIndex = stepped
                }
            }

            onExited: {
                if ( ! _root.grabbed ) {
                    _animator.stop()
                    _slider.opacity = 0
                }
            }
        }

        OpacityAnimator { id: _animator
            target  : _slider
            from    : 0
            to      : 1
            duration: 350
            running : _root.grabbed
        }

        Behavior on opacity { NumberAnimation { duration: 200 } }
    }

    Row {
        spacing             : Variables.defaultMargin // spacing between items to match others
        anchors.centerIn    : parent

        IconButton { id: _leftArrow
            iconSize                : Variables.circleButtonDefaultDiameter
            enabled                 : _root.canDecrement
            visible                 : _root.editable
            iconImageSource         : enabled ? "qrc:/images/iArrowLeft" :
                                                "qrc:/images/iArrowLeftDisabled"
            onClicked               : _root.currentIndex -= 1
        }

        // Display current item
        Text { id: _currentItem
            text                : _root.model[_root.currentIndex] !== undefined ?
                                      _root.canOff ? _root.currentIndex === 0 ? qsTr("OFF") : _root.model[_root.currentIndex] :
                                            _root.model[_root.currentIndex] : ""
            color               : Colors.offWhite
            font.pixelSize      : Fonts.fontPixelValueControl
            horizontalAlignment : Text.AlignHCenter
            verticalAlignment   : Text.AlignVCenter
            height              : parent.height
            width               : _root.editable ? _root.textWidth : parent.width
        }

        IconButton { id: _rightArrow
            iconSize                : Variables.circleButtonDefaultDiameter
            enabled                 : _root.canIncrement
            visible                 : _root.editable
            iconImageSource         : enabled ? "qrc:/images/iArrowRight" :
                                                "qrc:/images/iArrowRightDisabled"
            onClicked               : _root.currentIndex += 1
        }
    }
}
