/*!
 *
 * Copyright (c) 2020-2026 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    SliderCreateTreatment.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      26-Jan-2024
 * \author  (original)  Peter Lucia
 * \date    (original)  07-Jul-2020
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"

/*!
 * \brief   Slider component with a title and the currently selected value
 */
Rectangle { id: _root
    property Flickable flickable: null

    property var    toggleSwich : null // it is the treatment slider custom Switch bu it will be defined outside of this component and the var type would suffice.

    property bool   adjustable  : true
    property alias  label       : _label.text
    property alias  decimal     : _slider.decimal
    property alias  minimum     : _slider.minimum
    property alias  maximum     : _slider.maximum
    property alias  value       : _slider.value
    property alias defaultValue : _slider.defaultValue
    property alias  step        : _slider.step
    property alias  inActiveZero: _slider.inActiveZero

    property string zeroLabel   : ""

    property string unit        : ""
    property bool   active      : false
    property bool   valid       : true

    property alias  enableAdjustButtons : _sliderArrows.enabled

    signal pressed ()
    signal released()

    height  : Variables.createTreatmentSliderHeight
    width   : Variables.createTreatmentSliderWidth
    color   : Colors.transparent

    anchors.horizontalCenter: parent.horizontalCenter

    function clear() {
        reset(defaultValue)
        if ( toggleSwich ) {
            toggleSwich.checked = false
            toggleSwich.active  = false
        }
        _root.active = false
    }

    function reset(vValue) {
        _slider.reset(vValue)
    }

    function setColor() {
        let color = Colors.textMain
        if ( ! _root.valid  ) { color = Colors.createTreatmentInvalidParam  ; return color }
        if ( ! _root.active ) { color = Colors.textDisableButton            ; return color }
                                                                              return color
    }

    function setValue() {
        // The slider is not adjustable, implying it won't have a value change
        if ( !adjustable && zeroLabel !== "") {
            return _root.zeroLabel
        }

        let mValue = "__"
        let unit  = " " + _root.unit

        if ( _root.active ) {
            if ( _slider.value === 0 && zeroLabel !== "" ) {
                return _root.zeroLabel
            }
            mValue = _slider.value.toFixed(decimal)
        }
        return mValue + unit
    }

    function setInteractive( vInteractive ) {
        if (_root.flickable) {
            _root.flickable.interactive = vInteractive
        }
    }

    function setActiveValid() {
        _root.active = true
    }

    Text { id: _label
        text            : ""
        anchors.top     : parent.top
        anchors.left    : parent.left
        font.pixelSize  : Fonts.fontPixelFluidText
        color           : _root.setColor()
    }

    Text { id: _value
        objectName      : _root.objectName + "Value"
        text            : _root.setValue()
        anchors.top     : parent.top
        anchors.right   : parent.right
        font.pixelSize  : Fonts.fontPixelFluidUnit
        color           : _root.setColor()
    }

    Slider { id : _slider
        objectName      : _root.objectName + "Slider"
        enabled         : _root.adjustable
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottom          : parent.bottom
        width           : Variables.createTreatmentSliderWidth
        height          : Variables.sliderDefaultBodyHeight
        diameter        : Variables.progressbarHandler
        touchMargin     : Variables.createTreatmentSliderMargin
        handlerColor    : Colors.createTreatmentInactive
        handlerVisible  : _root.adjustable
        isActive        : _root.active
        ticks           : true
        onDragged               : { setInteractive(false) ;                  ; }
        onPressed               : { setInteractive(false) ; _root.pressed () ; }
        onReleased              : { setInteractive(true ) ; _root.released() ; }
        onHandleSelected        : { setActiveValid();                          }
        onSliderSelected        : { setActiveValid();                          }
    }

    SliderArrows{ id:_sliderArrows
        anchors.verticalCenter  : _slider.verticalCenter
        anchors.left            : _slider.right
        anchors.leftMargin      : Variables.sliderAdjustButtonLeftMargin

        // NOTE: @inActiveZero
        // the slider which use inActiveZero are the once, with OFF button.
        // these sliders have 0 as defalut to indicate OFF.
        // when activated the first value they get is still the 0 ( OFF ).
        // in that case we decrement once to get the correct minimum value.
        // as example heparin dispensing after gets ON, firsrt decrement shows OFF ( 0 ).
        // NOTE: if 0=defaultValue is in range (minimum < defaultValue < maximum), we still have problem, and needs more investigation
        onIncrementValue        : {
            if ( _slider.isActive) {
                _slider.incrementValue(true)
            } else {
                setActiveValid()
                if ( inActiveZero ) { // NOTE: @inActiveZero
                    _slider.decrementValue()
                }
            }
        }
        onDecrementValue        : {
            if ( _slider.isActive) {
                _slider.decrementValue(true)
            } else {
                setActiveValid()
                if ( inActiveZero ) { // NOTE: @inActiveZero
                    _slider.decrementValue()
                }
            }
        }
    }

}
