/*!
 *
 * Copyright (c) 2020-2023 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)      21-Oct-2022
 * \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 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  step        : _slider.step
    property alias  inActiveZero: _slider.inActiveZero

    property string zeroLabel   : ""

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

    signal pressed ()
    signal released()

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

    anchors.horizontalCenter: parent.horizontalCenter

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

    function setValue() {
        let mValue = "__"
        let unit  = " " + _root.unit
        if ( _root.active ) {
            if ( _slider.value === 0 && zeroLabel !== "" ) {
                return _root.zeroLabel
            }
            mValue = _slider.value
        }
        return mValue + unit
    }

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

    function setActiveValid() {
        _root.active = _root.valid = 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.sliderCreateTreatmentHeight
        diameter        : Variables.sliderCircleDiameter
        touchMargin     : Variables.createTreatmentSliderMargin
        handlerColor    : Colors.createTreatmentInactive
        handlerVisible  : _root.adjustable
        isActive        : _root.active
        ticks           : true
        isRoundedEnds   : true
        hasBorder       : true
        onPressed               : { setInteractive(false) ; _root.pressed (     ) ; setActiveValid(     ) }
        onDragged               : { setInteractive(false) ;                       ; setActiveValid(     ) }
        onReleased              : { setInteractive(true ) ; _root.released(     ) ;                       }
        onClicked               : {                       ;                       ; setActiveValid(     ) }
        onProgressRectClicked   : {                       ;                       ; setActiveValid(     ) }
        onProgressRectDragged   : {                       ;                       ; setActiveValid(     ) }
    }
}
