/*!
 *
 * Copyright (c) 2019-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    TouchRect.qml
 * \author  (last)      Vy
 * \date    (last)      21-Mar-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  18-Oct-2019
 *
 */

// Qt
import QtQuick 2.12

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

/*!
 * \brief   The TouchRect Component
 * which is used a general default round rect button
 */
Rectangle { id : _root
    property alias text             : _text
    property alias pixelSize        : _text.font.pixelSize
    property alias fgColor          : _text.color
    property alias bgColor          : _root.color
    property alias textString       : _text.text

    property bool  touchable        : true
    property bool  animated         : true
    property bool  isDefault        : false
    property bool  selectable       : false
    property bool  selected         : false

    property bool  fading           : false

    readonly property alias isPressed : _mouseArea.pressed

    property color textColor        : Colors.textButton
    property color borderColor      : Colors.borderButton
    property color backgroundColor  : Colors.backgroundButtonNormal
    property color defaultColor     :
        backgroundColor == Colors.transparent ?
            Colors.backgroundButtonSelect     :
            Qt.lighter(_root.backgroundColor, 1.15)

    property color selectColor      :
        backgroundColor == Colors.transparent ?
            Colors.backgroundButtonSelectDark :
            Qt.darker (_root.backgroundColor, 1.15)

    QtObject { id: _private
        function color() {
            var mBackgroundColor = _root.backgroundColor
            if (  isDefault          ) mBackgroundColor = _root.defaultColor
            if (! enabled            ) return Colors.transparent
            if (  selectable         )
                if (  selected       ) return Colors.backgroundButtonSelectDark
                else                   return mBackgroundColor
            if (! animated           ) return mBackgroundColor
            if (  _mouseArea.pressed ) return _root.selectColor
            return mBackgroundColor
        }

        function borderColor() {
            if ( ! enabled    ) return Colors.borderDisableButton
            if (   selectable )
                if ( selected ) {
                    return _root.borderColor
                } else {
                    return Colors.borderButtonUnselected
                }
            return _root.borderColor
        }
    }

    color       : _private.color()
    border.color: _private.borderColor()

    Rectangle { id: _rectAnim
        visible : _root.fading
        radius  : width
        color   : Colors.transparent
        anchors.fill: parent
        SequentialAnimation { id: _clickAnimation
            loops       : Animation.Infinite
            running     : _rectAnim.visible
            onStopped   : _rectAnim.color = Colors.transparent
            onFinished  : _rectAnim.color = Colors.transparent
            ColorAnimation { target: _rectAnim; property: "color"; to: Colors.transparent; duration: 2000; }
            ColorAnimation { target: _rectAnim; property: "color"; to: _root.border.color; duration: 2000; }
        }
    }

    property int   touchExpanding   : 0

    signal pressed
    signal released
    signal clicked
    signal pressAndHold

    width   : Variables.touchRectWidth
    height  : Variables.touchRectHeight
    radius  : Variables.touchRectRadius
    border.width: Variables.borderWidth

    Text { id: _text
        anchors.verticalCenter  : parent.verticalCenter
        anchors.horizontalCenter: parent.horizontalCenter
        color: enabled ? _root.textColor : Colors.textDisableButton
        font.pixelSize: Fonts.fontPixelButton
    }

    MouseArea { id: _mouseArea
        enabled         : _root.touchable
        anchors.fill    : parent
        anchors.margins : touchExpanding * -1
        onClicked       : _root.clicked()
        onPressed       : _root.pressed()
        onReleased      : _root.released()
        onPressAndHold  : _root.pressAndHold()
    }
}
