/*!
 *
 * Copyright (c) 2021-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    PostTreatmentStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      11-Aug-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  13-Apr-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"
import "qrc:/compounds"
import "qrc:/pages/posttreatment"
/*!
 * \brief   the post treatment prime stack screen
 */
StackItem { id: _root
    objectName: "_PostTreatmentStack"

    stackView.initialItem   : null

    enum StackStep {
        Disconnection   ,
        Review          ,
        Disinfection
    }

    property var stepNames: [
        qsTr("Disconnection"),
        qsTr("Review"       ),
        qsTr("Disinfection" ),
    ]

    property int stackStepIndex : 0

    signal patientDisconnectionConfirm  ()
    signal treatmentReviewConfirm       ()
    signal disposablesRemovalConfirm    ()
    signal disposablesRemovalBack       ()

    // TODO fix in post treatment
    onPatientDisconnectionConfirm  : {      vPostTreatmentAdjustmentDisposablesRemovalConfirm      .doConfirm( ) }
//    /* No wait for Rsp, so navigates => */  page( _treatmentReviewConfirm                                      )
//    /* After navigation ask for Tx data */  vPostTreatmentAdjustmentTreatmentLog                   .doRequest( )}
    onTreatmentReviewConfirm       : {      vPostTreatmentAdjustmentPatientDisconnectionConfirm    .doConfirm( )}
//    onDisposablesRemovalConfirm    :        vPostTreatmentAdjustmentDisposablesRemovalConfirm      .doConfirm( )
    onDisposablesRemovalBack       :        page( _treatmentReviewConfirm                                      )
    onVisibleChanged: {
        if (visible) {
            _mainMenu.hidden = true
            _headerBar.titleText = qsTr("End Treatment")
        }
        else {
            stackView.initialItem = null
            _headerBar.titleText = ""
        }
    }

    function confirmClicked() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnection:
            _root.patientDisconnectionConfirm()
            break
        case PostTreatmentStack.Review:
            _root.treatmentReviewConfirm()
            break
        case PostTreatmentStack.Disinfection:
            _root.disposablesRemovalConfirm()
            break
        default:
            break
        }
    }

    function backClicked() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnection:
            break
        case PostTreatmentStack.Review:
            break
        case PostTreatmentStack.Disinfection:
             _root.disposablesRemovalBack   ()
            break
        default:
            break
        }
    }

    function confirmButtonText() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnection:
            return qsTr("Auto Eject")
        case PostTreatmentStack.Review:
            return qsTr("NEXT")
        case PostTreatmentStack.Disinfection:
        default:
            return qsTr("CONFIRM")
        }
    }

    StepNavigationTitleBar { id: _titleBar
        stepIndex       : _root.stackStepIndex
        anchors {
            top         : _root.top
            left        : parent.left
            right       : parent.right
            leftMargin  : Variables.defaultMargin * 2
            rightMargin : anchors.leftMargin
        }
        confirmText.text: confirmButtonText()
        confirmVisible  : true
        backVisible     : stackStepIndex === PostTreatmentStack.Disinfection
        width           : _root.width
        stepNames       : _root.stepNames
        onBackClicked   : _root.backClicked()
        onConfirmClicked: _root.confirmClicked()
    }

    ExportButton { id: _exportButton
        isSmall         : true
        anchors.top     : parent.top
        anchors.left    : parent.left
        visible         : stackStepIndex === PostTreatmentStack.Review
        enabled         : vPostTreatmentAdjustmentTreatmentLog.isIdle && _GuiView.usbIsReady && !_GuiView.exportRunning
        onClicked       : vPostTreatmentAdjustmentTreatmentLog.doExport()
    }

    USBButton { id: _usbButton
        anchors.top     : _exportButton.top
        anchors.left    : _exportButton.right
        anchors.leftMargin: Variables.minVGap
        enabled         : _GuiView.usbIsReady && !_GuiView.exportRunning
        visible         : stackStepIndex === PostTreatmentStack.Review
    }

    PostTreatmentBase { id: _patientDisconnectionConfirm
        objectName          :"_patientDisconnectionConfirm"
        instructionBased    : true
        onVisibleChanged    :  if (visible) { stackStepIndex = PostTreatmentStack.Disconnection }
    }

    PostTreatmentReview { id: _treatmentReviewConfirm
        objectName          :"_treatmentReviewConfirm"
        instructionBased    : false
        onVisibleChanged    :  if (visible) { stackStepIndex = PostTreatmentStack.Review }

        onItemClicked       : {
            _detailTest.title.text = itemsText[vIndex]
            push(_detailTest)
        }
    }

    PostTreatmentBase { id: _detailTest
        objectName      :"_detailTest"
        onBackClicked   : pop()
    }

    PostTreatmentBase { id: _disposablesRemovalConfirm
        objectName          :"_disposablesRemovalConfirm"
        instructionBased    : true
        onVisibleChanged    :  if (visible) { stackStepIndex = PostTreatmentStack.Disinfection }
    }

    // connections
    Connections { target: vPostTreatmentStates
        function onDrainReservoirsChanged       ( vValue ) { console.debug("drainReservoirs     ", vValue   )}
        function onPatientDisconnectionChanged  ( vValue ) { console.debug("patientDisconnection", vValue   )}
        function onDisposableRemovalChanged     ( vValue ) { console.debug("disposableRemoval   ", vValue   )}
        function onVerifyChanged                ( vValue ) { console.debug("verify              ", vValue   )}
    }

    Connections { target: vTDOpMode
        function onPostTreatmentChanged         ( vValue ) { page( _patientDisconnectionConfirm  , vValue   )}
    }

    Connections { target: vPostTreatmentAdjustmentPatientDisconnectionConfirm
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentPatientDisconnectionConfirm.adjustment_Accepted ) {
                _treatmentReviewConfirm.reasonText = ""
                page( _disposablesRemovalConfirm )

            } else {
                _treatmentReviewConfirm.reasonText = vPostTreatmentAdjustmentPatientDisconnectionConfirm.text()
            }
        }
    }

    Connections { target: vPostTreatmentAdjustmentDisposablesRemovalConfirm
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentDisposablesRemovalConfirm.adjustment_Accepted ) {
                _patientDisconnectionConfirm.reasonText = ""
                page( _treatmentReviewConfirm )
            } else {
                _patientDisconnectionConfirm.reasonText = vPostTreatmentAdjustmentDisposablesRemovalConfirm.text()
            }
        }
    }

    Connections { target: vPostTreatmentAdjustmentTreatmentLog
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentTreatmentLog.adjustment_Accepted ) {
                _treatmentReviewConfirm.reasonText = ""
            } else {
                _treatmentReviewConfirm.reasonText = vPostTreatmentAdjustmentTreatmentLog.text()
            }
        }
    }
}
