/*!
 *
 * Copyright (c) 2021-2026 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    TextEntry.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      16-Jul-2024
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  16-Apr-2021
 *
 */

// Qt
import QtQuick 2.12

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

Item { id: _root
    enum Constants {
        EntryWidth  = 350 ,
        EntryHeight = 40  ,
        InputWidth  = 100
    }

    property alias      textInput   : _input
    property alias      line        : _line
    property alias      label       : _label
    property alias      separator   : _separator
    property alias      validator   : _input.validator
    property var        nextInput   : undefined
    property alias      text        : _input.text
    property bool       hasCursor   : true
    property alias      lengthMax   : _input.maximumLength

    readonly property alias isValid : _input.acceptableInput

    signal enterPressed ()
    signal clicked      (var vMouse)

    width   : _label.width + _input.width
    height  : TextEntry.EntryHeight

    onEnterPressed  : {
        if (nextInput) {
            if (textInput.acceptableInput) {
                nextInput.textInput.focus = true
            }
        }
    }

    function doFocus( vFocus ) {
        if ( vFocus ) {
            // if has cursor (is editable), by clicking on the entry the keyboard shall pop up
            // since the MouseArea is covering the entry, user can't move the cursur to delete one specific character,
            // all will be selected to overwrite the entire text for easy modification.
            // it was better if the keyboard has the cursor arrows
            if ( _root.hasCursor ) {
                _input.selectAll()
            }
            _keyboard.setVisible(true)
        }
    }

    Text { id           : _label
        height          : parent.height
        anchors.left    : parent.left
        anchors.top     : parent.top
        color           : Colors.textMain
        font.pixelSize  : Fonts.fontPixelTextRectExtra
    }

    Text { id           : _separator
        visible         : text
        width           : 0
        text            : ""
        height          : parent.height
        anchors.left    : _label.right
        anchors.top     : parent.top
        color           : Colors.textMain
        font.pixelSize  : Fonts.fontPixelTextRectExtra
        horizontalAlignment: Text.AlignLeft
    }

    TextInput { id          : _input
        enabled             : hasCursor
        height              : parent.height
        width               : TextEntry.InputWidth
        text                : ""
        font.pixelSize      : Fonts.fontPixelTextRectExtra
        color               : acceptableInput ? Colors.textMain : Colors.red
        selectionColor      : Colors.borderButtonHalfDarker
        selectedTextColor   : acceptableInput ? Colors.textMain : Colors.red
        anchors.left        : _separator.right
        horizontalAlignment : TextInput.AlignHCenter
        inputMethodHints    : Qt.ImhDigitsOnly
        selectByMouse       : true
        activeFocusOnPress  : true
        onAccepted          : _root.enterPressed()
        onFocusChanged      : doFocus(focus)
    }

    Line { id       : _line
        visible     : hasCursor
        color       : Colors.borderButtonHalfDarker
        width       : _input.width
        anchors.top : _input.bottom
        anchors.left: _input.left
        anchors.topMargin   : -thickness // move it up to be in the container rect in case the TextEntry clip set true.
    }

    MouseArea {
        // if it has cursor since it is possible to have only one entry.
        // then just click on the entry will not do anything and we need a mouse area to display the keyboard
        // if in any case the keyboard becomes hidden to make the rest of the screen visible.
        visible     : hasCursor
        anchors.fill: parent
        propagateComposedEvents: true
        onClicked: {
            _input.forceActiveFocus()
            doFocus(true)
            _root.clicked(mouse)
        }
    }
}
