/*!
 *
 * 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    RangeBar.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      13-Sep-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Jan-2020
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   Denali project Progress Rect
 * \details This rectangle fits into the rect area and will calculate its length
 *          in regards to the min, max, width, current value of the parent
 */
RangeRect { id: _root
    property int    value       : 0
    property int    lowerBound  : 0
    property int    upperBound  : 0

    property alias  rangebar    : _rangeRect

    QtObject { id: _private
        property int  val           : isOutLower ? min : isOutUpper ? max : value
        property int  min           : minimum
        property int  max           : maximum
        property bool isOutLower    : value < min
        property bool isOutUpper    : value > max
        property bool isOutRange    : isOutLower || isOutUpper
    }

    property int    markerHeight: Variables.rangeMarkerHeightMidle
    property color  markerColor : markerOutRangeNotify ? ( _private.isOutRange ? Colors.red : Colors.rangeMarker) : Colors.rangeMarker
    property bool   markerOutRangeNotify: true

    property bool   markerVisible       : true
    property int    markerBoundHeight   : Variables.rangeMarkerHeightShort
    property color  markerBoundColor    : Colors.rangeMarkerShort
    property int    markerFontSize      : Fonts.fontPixelRangeMarker

    property bool   lowerTextHorizontalCenter: true
    property bool   upperTextHorizontalCenter: true

    RangeRect { id: _rangeRect
        color:  "red"
        x       : ((parent.width * (lowerBound - parent.minimum)) / (parent.maximum - parent.minimum))
        height  : parent.height
        width   : ((parent.width * (upperBound - lowerBound)) / (parent.maximum - parent.minimum))

        radius  : 0
        decimal : _root.decimal
        minimum : lowerBound
        maximum : upperBound

        minText.visible: minimum != maximum // when min==max, only show the maximum label to avoid overlap
        maxText.visible: true

        minText.anchors.leftMargin  : lowerTextHorizontalCenter ? -minText.width / 2 : 0
        maxText.anchors.rightMargin : upperTextHorizontalCenter ? -maxText.width / 2 : 0

        RangeMarker { id: _rangeMarkerLowerBound
            decimal : _root.decimal
            text.visible: false
            height  : markerBoundHeight
            color   : markerBoundColor
            anchors.left: parent.left
        }

        RangeMarker { id: _rangeMarkerUpperBound
            decimal : _root.decimal
            text.visible: false
            height  : markerBoundHeight
            color   : markerBoundColor
            anchors.left: parent.right
        }
        onClicked: _root.clicked(vMouseEvent)
    }

    RangeMarker { id: _rangeMarkerValue
        visible: _root.markerVisible
        decimal: _root.decimal
        value   : parent.value
        x       : ((parent.width * (_private.val - _private.min)) / (_private.max - _private.min))
        text.visible: true
        text.font.pixelSize: _root.markerFontSize
        hasHandle   : true
        height  : markerHeight
        color   : markerColor
    }
}
