/*!
 *
 * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved.
 * \copyright                                                       \n
 *          THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM,  \n
 *          IN PART OR IN WHOLE,                                    \n
 *          WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n
 *
 * \file    Slider.qml
 * \date    2020/03/13
 * \author  Behrouz NematiPour
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   Denali project ProgressBar
 */
RangeRect { id: _root
    property alias  value       : _progressRect.value

    property real   step        : 1
    property bool   stepSnap    : false

    property bool   ticks       : false

    property alias  color       : _progressRect.color
    property alias  bgColor     : _root.color

    property alias  handler     : _handler


    height          : Variables.progressbarHeight
    touchMargin     : 25

    minimum         : 0
    maximum         : 0

    // real-time bound change should effect the current set value
    onMinimumChanged: {
        if (value < minimum )
            value = minimum
    }
    onMaximumChanged: {
        if (value > maximum )
            value = maximum
    }

    minText {
        visible          : true
        anchors.topMargin: Variables.sliderTextMargin
        font.pixelSize   : Fonts.fontPixelSliderMarker
        font.bold        : false
    }
    maxText {
        visible          : true
        anchors.topMargin: Variables.sliderTextMargin
        font.pixelSize   : Fonts.fontPixelSliderMarker
        font.bold        : false
    }

    function getValueOfX(x) {
        return ( x * ( maximum - minimum ) ) / width + minimum
    }

    function setValue(x) {
        if ( x <     0 ) { value = minimum;                             return; }
        if ( x > width ) { value = maximum;                             return; }

        value = getValueOfX(x)

        if ( step === 1 ) { /* keep the value and return */             return; }

        var start = 0
        if ( ! stepSnap ) start = minimum

        value = Math.round((value - start) / step) * step + start

        if ( value < minimum ) { value = minimum;                       return; }
        if ( value > maximum ) { value = maximum;                       return; }
    }

    ProgressRect { id: _progressRect
        color       : Colors.highlightProgressBar
        touchMargin : parent.touchMargin
        decimal : _root.decimal
        minimum : _root.minimum
        maximum : _root.maximum

        // propagation is not working on drag !
        onDragged: {
            setValue(vMouseEvent.x)
        }
        onClicked: {
            setValue(vMouseEvent.x)
        }
    }

    // used loader for performance since it may not always be required.
    // and can be a heavy Component
    Loader { id: _ticksLoader
        active          : ticks
        anchors.fill    : parent
        sourceComponent : TickMarks {
            decimal     : _root.decimal
            minimum     : _root.minimum
            maximum     : _root.maximum
            step        : _root.step
            stepSnap    : _root.stepSnap
            textColor   : _root.color
        }
    }

    onDragged: {
        setValue(vMouseEvent.x)
    }
    onClicked: {
        setValue(vMouseEvent.x)
    }

    Rectangle { id: _handler
        property real diameter  : Variables.progressbarHandler

        anchors.verticalCenter  : parent.verticalCenter
        anchors.horizontalCenter: _progressRect.right

        width   : diameter
        height  : diameter
        radius  : diameter
        color   : Colors.highlightProgressBar
        border  {
            width: 4
            color: "white"
        }
    }
}
