/*!
 *
 * 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)      16-Mar-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
        let overlap = _textMinimum.x + _textMinimum.width - _textMaximum.x + minimumGap

        // Check if there exists an overlap, if there is an overlap, adjust the text padding
        // to provide a gap between the min and max text labels. Otherwise, set them to 0
        if(overlap > 0){
            _textMinimum.rightPadding = overlap / 2
            _textMaximum.leftPadding  = overlap / 2
        } else {
            _textMinimum.rightPadding = 0
            _textMaximum.leftPadding  = 0
        }
    }

    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)
        }
    }
}
