/*!
 *
 * 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    PreTreatmentCreateStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      02-Apr-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  12-Mar-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/pages/pretreatment"
import "qrc:/pages/pretreatment/create"
/*!
 * \brief   the pre treatment parameters create stack screen
 */
StackItem { id: _root
    objectName: "_PreTreatmentCreateStack"

    stackView.initialItem   : null

    property int stackStepIndex : 0

    property alias patientID: _pretreatmentPatientIDEntry.text

    function setPatientID(vPatientID) {
        vTreatmentCreate                    .patientID = vPatientID // in case it needs to be displayed on Pre-Treatment Confirm Screen
        vPostTreatmentAdjustmentTreatmentLog.patientID = vPatientID // store for the TreatmentLog
    }

    function  pagePatientID( vCondition ) {
        if ( vCondition !== undefined && vCondition === false ) {
            _keyboard.setVisible(false)
            return
        }

        _root.patientID = ""
        page( _pretreatmentPatientID )
              _pretreatmentPatientID.setFocus()
    }

    PreTreatmentBase            { id:       _pretreatmentPatientID

        function setFocus() {
            _pretreatmentPatientIDEntry.textInput.forceActiveFocus()
            _keyboard.setVisible(true)
        }

        title.text              : qsTr("Patient ID")
        header.backVisible      : true
        header.confirmVisible   : true
        header.confirmText.text : _pretreatmentPatientIDEntry.text ? qsTr("CONTINUE") : qsTr("SKIP")
        TextEntry               { id  :     _pretreatmentPatientIDEntry
            property int topMarginContent   : 200

            clip                : true
            hasCursor           : true
            textInput   .width  : 450
            text                : vTreatmentCreate.patientID
            anchors {
                top             : parent.top
                topMargin       : topMarginContent + 120 // moved a little down to be more on center and close to keyboard top edge.
                horizontalCenter: parent.horizontalCenter
            }
            textInput.inputMethodHints  : Qt.ImhPreferLowercase
            textInput.echoMode          : TextInput.Normal
            textInput.validator         : RegExpValidator {
                regExp: Variables.regExp_PatientID
            }
        }
    }

    PreTreatmentCreate          { id:       _pretreatmentCreate
        header.backVisible      : true
        header.confirmVisible   : true
        header.confirmText.text : qsTr("CONTINUE")
        title.text              : qsTr("Create a Custom Treatment")
    }

    PreTreatmentConfirm         { id:      _pretreatmentConfirm
        header.backVisible      : true
        header.confirmVisible   : true
        title.text              : qsTr("Confirm Treatment")
    }

    Connections { target: vHDOperationMode
        function onValidateParametersChanged    ( vValue ) { pagePatientID                         ( vValue     )}
    }

    Connections { target:  _pretreatmentPatientID
        function onBackClicked                  (        ) { vPreTreatmentAdjustmentInitTreatment.doCancel     ()}
        function onConfirmClicked               (        ) { setPatientID                     ( _root.patientID )
                                                           page( _pretreatmentCreate                            )}
    }

    Connections { target:  _pretreatmentCreate
        function onBackClicked                  (        ) { pagePatientID                                     ()}
        function onConfirmClicked               (        ) { vTreatmentCreate.doValidation                     ()}
    }

    Connections { target: vTreatmentCreate
        function onDidValidationPass            ( vValue ) { page( _pretreatmentConfirm                         )}
    }

    Connections { target: _pretreatmentConfirm
        function onBackClicked                  (        ) { vTreatmentCreate.doCancel                         ()
                                                             page( _pretreatmentCreate                          )}
        function onConfirmClicked               (        ) { vTreatmentCreate.doConfirm                        ()}
    }

    onVisibleChanged: {
        if (visible) {
            _mainMenu.hidden = true
        }
        else {
            stackView.initialItem = null
        }
    }
}
