/*!
 *
 * 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    RangeRect.qml
 * \author  (last)      Vy
 * \date    (last)      28-Apr-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  24-Jan-2020
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   Denali project RangeRect
 */
Rectangle { id: _root
    signal clicked(var vMouseEvent)
    signal dragged(var vMouseEvent)
    signal released(var vMouseEvent)
    signal pressed(var vMouseEvent)

    property int    decimal : 0
    property real   minimum : 0 ///< minimum value of the range
    property real   maximum : 0 ///< maximum value of the range

    property alias  minText : _textMinimum
    property alias  maxText : _textMaximum

    property bool   minTextHorizontalCenter: false
    property bool   maxTextHorizontalCenter: false

    property string unit    : ""

    property real   touchMargin         : 0
    property real   leftRightTouchMargin: 0

    function adjustOverlap() {
        // Due to font size not being fixed and be different depending on text,
        // setting a minimal expected gap
        let minimumGap = 4

        // Reset the padding of the text Items to initially assume there is no
        // overlap adjustment. This avoids causing the padding to grow accumulatively
        // if we enter the "overlapped" condition multiple times consecutively
        _textMinimum.rightPadding = 0
        _textMaximum.leftPadding  = 0

        // A special case in which only the max label is shown, this ensures centering the label
        if(minimum == maximum) return

        // Considering half the minimum text's width as the threshold to determine
        // whether an overlap of the labels exist. This avoids relying solely on
        // the x of the maximum label or the width of the root. Debugging the code
        // for overlapping revealed that the max's X and the width of the root can be
        // unexpectedly larger than what is display. This causes the calculation of
        // "overlapping" to be incorrect leading to some cases not correctly padding.
        let overlapThreshold = ( _textMinimum.width  + minimumGap )/ 2
        let overlap = overlapThreshold - _textMaximum.x

        if(_root.width < overlapThreshold || _textMaximum.x < overlapThreshold){
                _textMinimum.rightPadding = overlap
                _textMaximum.leftPadding  = overlap
        }
    }

    // The min/max labels are anchored to the left/right sides of _root
    // so, an update of overlapping is necessary if the width changes
    onWidthChanged: adjustOverlap()

    width   : parent.width
    height  : parent.height

    color   : Colors.backgroundRangeRect

    Text { id: _textMinimum
        visible :  false
        font.pixelSize  : Fonts.fontPixelRangeRectText
        font.bold       : true

        color   : Colors.textProgressBar
        text    : minimum.toFixed(decimal) + unit
        onTextChanged   : adjustOverlap()

        anchors {
            left        : parent.left
            top         : parent.bottom
            topMargin   : Variables.rangeRectTextMargin
            leftMargin  : minTextHorizontalCenter ? -minText.width / 2 : 0
        }
    }

    Text { id: _textMaximum
        visible :  false

        font.pixelSize  : Fonts.fontPixelRangeRectText
        font.bold       : true

        color   : Colors.textProgressBar
        text    : maximum.toFixed(decimal) + unit
        onTextChanged   : adjustOverlap()

        anchors {
            right       : parent.right
            top         : parent.bottom
            topMargin   : Variables.rangeRectTextMargin
            rightMargin : maxTextHorizontalCenter ? -maxText.width / 2 : 0
        }
    }

    MouseArea { id: _mouseArea
        anchors.bottomMargin    : -touchMargin
        anchors.topMargin       : -touchMargin

        anchors.leftMargin      : -leftRightTouchMargin
        anchors.rightMargin     : -leftRightTouchMargin

        anchors.centerIn: parent
        anchors.fill: parent

        onPositionChanged: {
            _root.dragged(mouse)
        }
        onClicked: {
            _root.clicked(mouse)
        }
        onReleased: {
            _root.released(mouse)
        }
        onPressed: {
            _root.pressed(mouse)
        }
    }
}
