/*!
 *
 * Copyright (c) 2021-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    TextEntry.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      14-Apr-2023
 * \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

    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 ) {
            //TODO: Not sure why if there is no cursor all should be selected,
            //      tested and couln't find anything, disabled for now.
            //      was causeing on disabling the WiFi Static IP entry.
            // 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 {
        visible     : ! hasCursor
        anchors.fill: parent
        propagateComposedEvents: true
        onClicked: {
            _input.forceActiveFocus()
            doFocus(true)
            _root.clicked(mouse)
        }
    }
}
