/*!
 *
 * Copyright (c) 2020-2024 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    RangeMarker.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      16-Oct-2020
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Jan-2020
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   Denali project simple logo (No 'Diality')
 */
Item { id : _root
    property real  value        : 0
    property int   decimal      : 0

    property alias text         : _textValue
    property alias handle       : _handle
    property alias font         : _textValue.font
    property color color        : Colors.rangeMarker
    property alias thickness    : _root.width
    property alias hasHandle    : _handle.visible
    property bool  valueOnTop   : false
    property string unitText    : ""

    height: parent.height
    width : Variables.rangeMarkerWidth

    anchors {
        bottom  : parent.bottom
    }

    Rectangle { id: _handle
        visible : false

        width   : _root.width * 3
        height  : width
        radius  : width
        color   : _root.color
        anchors {
            top : _root.top
            horizontalCenter: parent.horizontalCenter
        }
    }

    Rectangle { id: _stick
        width   : _root.width
        height  : _root.height
        color   : _root.color
        anchors {
            top : _root.top
            horizontalCenter: parent.horizontalCenter
        }
    }

    Text { id: _textValue
        font {
            pixelSize   : Fonts.fontPixelRangeMarker
            weight      : Font.Bold
        }
        anchors {
            right       : valueOnTop ? parent.left : undefined
            top         : valueOnTop ? undefined :
                                       parent.top
            bottom      : valueOnTop ? _handle.top :
                                       undefined
            bottomMargin: valueOnTop ? 5 : 0
            rightMargin : valueOnTop ? -12 : 5
            topMargin   : valueOnTop ? 0 : -5
            horizontalCenter: valueOnTop ? _handle.horizontalCenter : undefined
            horizontalCenterOffset: {
                if (valueOnTop) {
                    if (_root.parent !== undefined) {
                        let xDelta = (_root.width-implicitWidth)/2
                        // left boundary
                        if (_root.x + xDelta < 0) {
                            return -(_root.x + xDelta)
                        }
                        // right boundary
                        let rightOverflow = _root.x + _root.width - xDelta
                        if (rightOverflow > _root.parent.width) {
                            return -(rightOverflow - _root.parent.width)
                        }
                    }
                }
                return 0
            }
        }
        horizontalAlignment: valueOnTop ? Text.AlignHCenter : Text.AlignLeft
        color   : _root.color
        text    : value.toFixed(decimal) + " " + unitText
    }
}
