import QtQuick 2.0

/*!
 *
 * 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    PressureRangeSlider.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      15-May-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  12-Oct-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

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


/*!
 * \brief   PressureRangeSlider.qml is a compound component
 * \detail  Which is used in the in-line arterial and venus presseure adjustment screen.
 *          It contains of label for the Arterial/Venous and the unit next to it on top left.
 *          And on the left side a RangeBar on top of a RangeSlider that are layed out in a Column.
 *          The RangeSlider and RangeBar are connected and RangeSlider change affects RangeBar min/max.
   \verbatim
     minimum       : 0
     minimum       :                                                                                                   100
     rangebar      : ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
     limitGap      : ^                  ┌─────────────────────────────────────────────┐                                 ^
                     │                  v                                             v                                 │
     rangeslider   : │    ╠══════(══════@═══)═══════════════════════════════(═════════@══════════)══════════╣           │
                     │    ^      ^      ^   ^                               ^         ^          ^          ^           │
     minimumMargin : └────┘      │      │   │                               │         │          │          │           │
     maximumMargin :             │      │   │                               │         │          │          └───────────┘
     lowerBoundMin : ────────────┘      │   │                               │         │          │
     lowerBound    : ───────────────────┘   │                               │         │          │
     lowerBoundDef : ───────────────────┘   │                               │         │          │
     lowerBoundMax : ───────────────────────┘                               │         │          │
     upperBoundMin : ───────────────────────────────────────────────────────┘         │          │
     upperBound    : ─────────────────────────────────────────────────────────────────┘          │
     upperBoundDef : ─────────────────────────────────────────────────────────────────┘          │
     upperBoundMax : ────────────────────────────────────────────────────────────────────────────┘
     step          : minimum space/movement between each range slider values
   \endverbatim
 */
Row { id: _root
    property real   minimum       :  0      ///< the minimum value of the range (adjustable slider might be different regarding the minimumMargin) 
    property real   minimumMargin :  0      ///< the margin of the adjustable minimum value from the minimm
    property real   lowerBoundMin :  0      ///< the adjutable lowerband minimum acceptable value
    property real   lowerBound    :  0      ///< the adjustable lower bound of the value
    property real   lowerBoundMax :  0      ///< the adjutable lowerband maximum acceptable value
    property real   upperBoundMin :  0      ///< the adjutable upperband minimum acceptable value
    property real   upperBound    :  0      ///< the adjustable upper bound of the value
    property real   upperBoundMax :  0      ///< the adjutable upperband maximum acceptable value
    property real   maximumMargin :  0      ///< the margin between slider maximum and rangebar maximum
    property real   maximum       :  0      ///< the maximum value of the pressure on the rangebar
    property real   value         :  0      ///< the pressure value
    property real   step          : 10      ///< minimum space/movement between each range slider values
    property int    limitGap      :  0      ///< the closest the two of lowerBound and upperBound can get.

    property bool   ticksVisible  : false   ///< hide or show the tick marks help of the step

    property string titleText           : ""                                    ///< the component title
    property string titleUnit           : Variables.unitTextBloodPressure       ///< the component unit
    property int    titleFontSize       : Fonts.fontPixelPresseuresLabel        ///< the component title font size
    property int    titleWidth          : 200                                   ///< the component title width

    property int    progressWidth       : Variables.pressuresProgressbarWidth   ///< rangebar width
    property color  progressColor       : Colors.highlightProgressBar           ///< rangebar color

    property int    sliderTopMargin     : 100                                   ///< top margin between the slider and the rangebar

    spacing: 25

    TextRect { id: _pressureTextRect
        anchors.top: parent.top
        width: titleWidth
        label: _root.titleText
        extra: _root.titleUnit
        labelFont.pixelSize: _root.titleFontSize
    }

    Rectangle { id: _pressureRect
        width   : _root.progressWidth
        height: 10
        color: Colors.transparent

        property int  minMargin: _root.minimumMargin
        property int  maxMargin: _root.maximumMargin

        RangeBar { id: _pressureRangeBar
            property int  minDiff: (_pressureRect.minMargin * width)/( _root.maximum - _root.minimum ) // Map the value to pixel
            property int  maxDiff: (_pressureRect.maxMargin * width)/( _root.maximum - _root.minimum ) // Map the value to pixel

            anchors {
                top  : _pressureRect.top
                left : _pressureRect.left
                right: _pressureRect.right
            }

            height  :  15

            rangebar.color: _root.progressColor

            minimum     : _root.minimum
            lowerBound  : _root.lowerBound
            upperBound  : _root.upperBound
            maximum     : _root.maximum
            value       : _root.value

            minText.visible : _root.lowerBound !== minimum
            maxText.visible : _root.upperBound !== maximum

            minText.anchors.topMargin: 25
            maxText.anchors.topMargin: 25
            minTextHorizontalCenter: true
            maxTextHorizontalCenter: true

            minText.font.pixelSize          : Fonts.fontPixelPresseuresText
            maxText.font.pixelSize          : Fonts.fontPixelPresseuresText
            rangebar.minText.font.pixelSize : Fonts.fontPixelPresseuresText
            rangebar.maxText.font.pixelSize : Fonts.fontPixelPresseuresText
            markerFontSize                  : Fonts.fontPixelPresseuresText

            onLowerBoundChanged: {
                _pressureSlider.minValue = lowerBound
            }
            onUpperBoundChanged: {
                _pressureSlider.maxValue = upperBound
            }

            markerOutRangeNotify    : true
        }
        RangeSlider { id: _pressureSlider
            anchors {
                top         : _pressureRangeBar.top
                topMargin   : sliderTopMargin
                left        : _pressureRect.left
                leftMargin  : _pressureRangeBar.minDiff
                right       : _pressureRect.right
                rightMargin : _pressureRangeBar.maxDiff
            }

            height  : 5
            diameter: Variables.sliderCircleDiameter

            decimal : 1
            bgColor : Colors.pressuresOutOfRaneBg

            minText {
                text : qsTr("LOW")
                font.bold: true
                font.pixelSize: Fonts.fontPixelPresseuresText
                anchors.leftMargin: - minText.width      - 20
                anchors.topMargin : - minText.height / 2 -  5
                color: Colors.textMain
            }
            maxText {
                text : qsTr("HIGH")
                font.bold: true
                font.pixelSize: Fonts.fontPixelPresseuresText
                anchors.rightMargin: - minText.width      - 20
                anchors.topMargin  : - minText.height / 2 -  5
                color: Colors.textMain
            }

            // **** Ranges vvvv
            minimum : _root.minimum + _pressureRect.minMargin

            minValueLowerBound  : _root.lowerBoundMin
            minValue            : _root.lowerBound
            minValueUpperBound  : _root.lowerBoundMax

            maxValueLowerBound  : _root.upperBoundMin
            maxValue            : _root.upperBound
            maxValueUpperBound  : _root.upperBoundMax

            maximum : _root.maximum - _pressureRect.maxMargin

            gapValue: _root.limitGap
            // **** Ranges ^^^^

            unit    : Variables.unitTextBloodPressure
            step    : _root.step
            ticks   : _root.ticksVisible

            onMinValueChanged: {
                lowerBound = minValue
            }
            onMaxValueChanged: {
                upperBound = maxValue
            }
        }
    }
}
