/*!
 *
 * 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 {
        Disconnect      ,
        Remove          ,
        Review          ,
        Disinfection
    }

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

             property int stackStepIndex    : 0
    readonly property bool isDisconnect     : _root.stackStepIndex === PostTreatmentStack.StackStep.Disconnect
    readonly property bool isReview         : _root.stackStepIndex === PostTreatmentStack.StackStep.Review

    onVisibleChanged: {
        if (visible) {
            _mainMenu.hidden = true
            _headerBar.titleText = qsTr("End Treatment")
        }
        else {
            stackView.initialItem = null
            _headerBar.titleText = ""
        }
    }

    function confirmClicked() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnect:
            _postTreatmentDisconnectStack.continueClicked()
            break
        case PostTreatmentStack.Remove:
            vPostTreatmentAdjustmentDisposablesRemovalConfirm.doConfirm()
            break
        case PostTreatmentStack.Review:
            page(  _postTreatmentDisinfection )
            break
        case PostTreatmentStack.Disinfection:
             // Skip button - go back to standby -- Finish Treatment
            break
        default:
            break
        }
    }

    function backClicked() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnect:
            _postTreatmentDisconnectStack.backClicked()
            break
        case PostTreatmentStack.Disinfection:
            page(  _postTreatmentReview)
            break
        case PostTreatmentStack.Remove:
        case PostTreatmentStack.Review:
        default:
            break
        }
    }

    function confirmButtonText() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnect:
        case PostTreatmentStack.Review:
            return qsTr("Continue")
        case PostTreatmentStack.Remove:
            return qsTr("Auto Eject")
        case PostTreatmentStack.Disinfection:
            return qsTr("Skip")
        default:
            return qsTr("Next")
        }
    }

    function backEnabled() {
        switch ( _root.stackStepIndex ) {
        case PostTreatmentStack.Disconnect:
            return _postTreatmentDisconnectStack.backEnabled()
        case PostTreatmentStack.Remove:
        case PostTreatmentStack.Review: // fallthrough
            return false
        case PostTreatmentStack.Disinfection:
        default:
            return true
        }
    }

    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     : _root.backEnabled()
        width           : _root.width
        stepNames       : _root.stepNames

        subStepIndex    : isDisconnect ? _postTreatmentDisconnectStack.subStepIndex + 1 : 0

        subSteps        : {
            let arr =Array(stepNames.length).fill(0)
            arr[ PostTreatmentStack.Disconnect] = PostTreatmentDisconnectStack.Step.Count
            return arr
        }

        onBackClicked   : _root.backClicked()
        onConfirmClicked: _root.confirmClicked()

        Row { id: _qrRow
            spacing         : Variables.defaultMargin
            visible         :    _root.isReview

            anchors {
                left        : _titleBar.left
                leftMargin  : Variables.defaultMargin * 2
                top         : _titleBar.top
                topMargin   : Variables.defaultMargin * 2
            }

            QRCode { id: _qrCode
                anchors.verticalCenter      : parent.verticalCenter
                anchors.verticalCenterOffset: -5
                qrcode          : vPostTreatmentAdjustmentTreatmentLog.txCode
                clear           : ! _root.visible
            }

            Text { id: _txCodeText
                anchors.bottom  : _qrCode.bottom
                text            : vPostTreatmentAdjustmentTreatmentLog.txCode ? qsTr("Tx Code: ") + vPostTreatmentAdjustmentTreatmentLog.txCode : " "
                color           : "#DBE9FA"
                font.pixelSize  : Fonts.fontPixelButton
            }
        }
    }

    PostTreatmentDisconnectStack { id: _postTreatmentDisconnectStack
        objectName          : "_postTreatmentDisconnectStack"
        onVisibleChanged    :  if (visible) { stackStepIndex = PostTreatmentStack.Disconnect;   initStack()     }
    }

    PostTreatmentBase { id: _postTreatmentRemove
        objectName          : "_postTreatmentRemove"
        instructionBased    : true
        onVisibleChanged    :  if (visible) { stackStepIndex = PostTreatmentStack.Remove;       updateModel()   }
    }

    PostTreatmentReview { id:  _postTreatmentReview
        objectName          : " _postTreatmentReview"
        onVisibleChanged    :  if (visible) {   stackStepIndex = PostTreatmentStack.Review;
                                                vPostTreatmentAdjustmentTreatmentLog.doRequest()                }
    }

    PostTreatmentBase { id:  _postTreatmentDisinfection
        objectName          :" _postTreatmentDisinfection"
        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( _postTreatmentDisconnectStack  , vValue   )}
    }

    // Disconnect stack
    Connections { target: _postTreatmentDisconnectStack
        function onGoToNextStep ()      { page ( _postTreatmentRemove       )}
    }

    // Confirm Patient Disconnection
    Connections { target: vPostTreatmentAdjustmentPatientDisconnectionConfirm
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentPatientDisconnectionConfirm.adjustment_Accepted ) {
                _postTreatmentReview.reasonText = ""
                _postTreatmentDisconnectStack.goToDrain()

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

    // Auto - Eject
    Connections { target: vPostTreatmentAdjustmentDisposablesRemovalConfirm
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentDisposablesRemovalConfirm.adjustment_Accepted ) {
                _postTreatmentRemove.reasonText = ""
                page(  _postTreatmentReview )
            } else {
                _postTreatmentRemove.reasonText = vPostTreatmentAdjustmentDisposablesRemovalConfirm.text()
            }
        }
    }

    // Treatment Review
    Connections { target: vPostTreatmentAdjustmentTreatmentLog
        function onAdjustmentTriggered          ( vValue ) {
            if ( vPostTreatmentAdjustmentTreatmentLog.adjustment_Accepted ) {
                _postTreatmentReview.reasonText = ""
            } else {
                _postTreatmentReview.reasonText = vPostTreatmentAdjustmentTreatmentLog.text()
            }
        }
    }
}
