/*!
 *
 * Copyright (c) 2021-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    ScrollBar2.qml
 * \author  (last)      Vy
 * \date    (last)      04-Apr-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  06-May-2021
 *
 */

// Qt
import QtQuick 2.12


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

/*!
 * \brief   the ScrollBar Component
 */

Rectangle { id: _root
    enum Direction { Vertical, Horizontal }

    property int direction      : ScrollBar2.Vertical
    property Flickable flickable: undefined
    property alias scrollColor  : _scrollbar.color
    property alias backColor    : _root.color
    property real  scrollMargin : 0
    property alias handleWidth  : _scrollbar.width

    width   : Variables.dialogRadius
    height  : Variables.dialogRadius
    radius  : direction === ScrollBar2.Horizontal ? height : width
    clip    : true
    color   : "transparent"

    Rectangle { id: _scrollbar
        readonly property real  xPos: (flickable instanceof Flickable) ? flickable.visibleArea.xPosition * parent.width : 0
        readonly property real  yPos: (flickable instanceof Flickable) ? flickable.visibleArea.yPosition * parent.height : 0
        readonly property int   barWidth: (flickable instanceof Flickable) ? parent.width * flickable.visibleArea.widthRatio : 0
        readonly property int   barHeight: (flickable instanceof Flickable) ? parent.height * flickable.visibleArea.heightRatio : 0
        readonly property int   minSize: parent.radius * 2
        readonly property int   deltaWidth: direction === ScrollBar2.Horizontal
            ? xPos < 0 ? Math.abs(xPos) : Math.max(0, xPos - (parent.width - barWidth))
            : 0
        readonly property int   deltaHeight: direction === ScrollBar2.Vertical
            ? yPos < 0 ? Math.abs(yPos) : Math.max(0, yPos - (parent.height - barHeight))
            : 0

        anchors {
            bottom          : direction === ScrollBar2.Horizontal ? parent.bottom        : undefined
            bottomMargin    : direction === ScrollBar2.Horizontal ? parent.scrollMargin  : 0
            right           : direction === ScrollBar2.Horizontal ? undefined            : parent.right
            rightMargin     : direction === ScrollBar2.Horizontal ? 0                    : parent.scrollMargin
        }
        x               : direction === ScrollBar2.Horizontal ? Math.min(Math.max(0, xPos), parent.width - minSize) : 0
        y               : direction === ScrollBar2.Horizontal ? 0 : Math.min(Math.max(0, yPos), parent.height - minSize)
        width           : direction === ScrollBar2.Horizontal ? Math.max(minSize, barWidth - deltaWidth) : parent.width
        height          : direction === ScrollBar2.Horizontal ? parent.height : Math.max(minSize, barHeight - deltaHeight)
        radius          : parent.radius
        color           : Colors.scrollBarBgColor
    }
}
