/*!
 *
 * Copyright (c) 2021-2022 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)      18-Jan-2022
 * \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
        }

                           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
            hasCarret           : 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
        onValidateParametersChanged : { pagePatientID                  ( vvalidateParameters )}
    }

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

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

    Connections { target: vTreatmentCreate
        onFwValidationSuccess       : { page( _pretreatmentConfirm                          )}
    }

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

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