/*!
 *
 * 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    EndTreatmentRecirculateStack.qml
 * \author  (last)      Vy
 * \date    (last)      17-May-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  11-Apr-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
//  Qml imports
import "qrc:/globals"
import "qrc:/components"

/*!
 * \brief   the end treatment recirculate stack screen
 */
StackItem { id: _root
    objectName: "_recirculateRecirculateStack"

    stackView.initialItem           : null

    property int stackStepIndex     : 0

    signal reconnectClicked         ()
    signal confirmDisconnectClicked ()
    signal confirmReconnectClicked  ()
    signal recirculateBackClicked   ()
    signal treatmentEndClicked      ()

    onReconnectClicked                          : vTreatmentAdjustmentRecirculate.doReconnect           () //84.0
    onConfirmReconnectClicked                   : vTreatmentAdjustmentRecirculate.doConfirmReconnect    () //84.1
    onRecirculateBackClicked                    : vTreatmentAdjustmentRecirculate.doResume              () //84.2
    onTreatmentEndClicked                       : vTreatmentAdjustmentRecirculate.doTreatmentEnd        () //84.3
    onConfirmDisconnectClicked                  : vTreatmentAdjustmentRecirculate.doConfirmDisconnect   () //84.4

    readonly property bool  timeoutTotal        : vTreatmentRecirculate .timeoutTotal
    readonly property int   timeoutCountdown    : vTreatmentRecirculate .timeoutCountDown * 60
    readonly property bool  treatmentTimeLeft   : true // vTreatmentTime        .time_IsLeft // regarding the conversation with Sean (04/23/2023).

    component ButtonBackToRecirculate   : TouchRect {
        text.text                       : qsTr("BACK TO RECIRCULATE")
        onClicked                       : recirculateBackClicked()
    }

    component ButtonEndTreatment        : TouchRect {
        visible                         : treatmentTimeLeft
        text.text                       : qsTr("END TREATMENT")
        onClicked                       : treatmentEndClicked()
    }

    component ButtonReconnect           : TouchRect {
        text.text                       : qsTr("RESUME TREATMENT")
        isDefault                       : true
        onClicked                       : reconnectClicked()
    }

    component RecirculateBase           : EndTreatmentBase {
        property alias footerButtons    : _footer.children
        property bool  confirmVisible   : false

        header.confirmVisible           : confirmVisible
        instructionBased                : true
        hasTimeout                      : timeoutTotal
        timeoutValue                    : timeoutCountdown

        footer {
            width                       : parent.width
            height                      : Variables.touchRectHeight
        }

        FooterStatic                { id: _footer
            childrenWidth               : 300
            anchors.fill                : parent.footer
        }
    }

    // Disconnect
    RecirculateBase                 { id: _recirculateDisconnect
        title.text                      : qsTr("Recirculate Disconnect")
        confirmVisible                  : true
        onConfirmClicked                : confirmDisconnectClicked()
        footerButtons                   : [
            ButtonEndTreatment          {},
            Item                        {}, // button placeholder to align the buttons
            Item                        {}  // button placeholder to align the buttons
        ]
    }

    // Recirculate
    RecirculateBase                 { id: _recirculateRecirculate
        title.text                      : qsTr("Recirculate")
        footerButtons                   : [
            ButtonEndTreatment          {},
            ButtonReconnect             {},
            Item                        {} // button placeholder to align the buttons
        ]
    }

    // Stopped
    RecirculateBase                 { id: _recirculateStopped
        title.text                      : qsTr("Recirculate Stopped")
        footerButtons                   : [
            ButtonEndTreatment          {},
            ButtonBackToRecirculate     {},
            Item                        {} // button placeholder to align the buttons

        ]
    }

    // Reconnect
    RecirculateBase                 { id: _recirculateReconnect
        title.text                      : qsTr("Recirculate Reconnect")
        confirmVisible                  : true
        onConfirmClicked                : confirmReconnectClicked()
        footerButtons                   : [
            ButtonEndTreatment          {},
            ButtonBackToRecirculate     {},
            Item                        {} // button placeholder to align the buttons
        ]
    }

    Connections { target: vHDTreatmentStates
        function onRcDisconnectChanged          ( vValue ) { page( _recirculateDisconnect   , vValue   )}
        function onRcStartedChanged             ( vValue ) { page( _recirculateRecirculate  , vValue   )}
        function onRcStoppedChanged             ( vValue ) { page( _recirculateStopped      , vValue   )}
        function onRcReconnectChanged           ( vValue ) { page( _recirculateReconnect    , vValue   )}
    }

    Connections { target: vTreatmentAdjustmentRecirculate
        function onAdjustmentTriggered          ( vValue ) {
            // there is only one message to handle all the sub pages of rinseback
            // since the rejection reason will be cleared by each trasition
            // so it's fine to set all at the same time
            // and let them cleared for their own message when each get visible.
            _recirculateDisconnect  .reasonText = vTreatmentAdjustmentRecirculate.text();
            _recirculateRecirculate .reasonText = vTreatmentAdjustmentRecirculate.text();
            _recirculateStopped     .reasonText = vTreatmentAdjustmentRecirculate.text();
            _recirculateReconnect   .reasonText = vTreatmentAdjustmentRecirculate.text();
        }
    }
}
