/*!
 *
 * Copyright (c) 2019-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    TouchRect.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      03-Nov-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
    //TODO need to update code to use bgColor instead of backgroundColor property
//  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
    property int   debouncingTime   : 350
    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)

    Timer { id: clickDebounceTimer
        interval: _root.debouncingTime // ms
    }
    Timer { id: pressDebounceTimer
        interval: _root.debouncingTime // ms
    }
    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
        }

        property bool clickDebouncing: clickDebounceTimer.running   // if a click/press happens the timer starts, meaning we have to debounce.
        property bool pressDebouncing: pressDebounceTimer.running   // if a click/press happens the timer starts, meaning we have to debounce.
        property bool pressed       : false                         // this will take care of releases happening during debounced press, therefor no release should happen as well.
    }

    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

    function onMouseEventLog(vMsg) {
        // DEBUG: console.log(vMsg, _text.text, Qt.formatTime(new Date(), "hh:mm:ss.zzz"))
    }

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

    MouseArea { id: _mouseArea
        enabled         : _root.touchable
        anchors.fill    : parent
        anchors.margins : touchExpanding * -1
        onClicked       : { if ( ! _private.clickDebouncing                    ) { onMouseEventLog("Clicked       ") ;                            _root.clicked     (); clickDebounceTimer.start(); } focus = true;  }
        onPressed       : { if ( ! _private.pressDebouncing                    ) { onMouseEventLog("Pressed       ") ; _private.pressed = true ;  _root.pressed     (); pressDebounceTimer.start(); }   }
        onReleased      : { if (                              _private.pressed ) { onMouseEventLog("Released      ") ; _private.pressed = false;  _root.released    (); pressDebounceTimer.start(); }   }
        onPressAndHold  : {                                                        onMouseEventLog("PressAndHold  ") ;                            _root.pressAndHold();                                 }
    }
}
