/*!
 *
 * 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    EndTreatmentRecirculateStack.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      07-Oct-2021
 * \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: "_EndTreatmentRecirculateStack"

    stackView.initialItem           : null

    property int stackStepIndex     : 0

    signal reconnectClicked         ()
    signal recirculateConfirmClicked()
    signal recirculateBackClicked   ()
    signal treatmentEndClicked      ()

    // Recirculate
    EndTreatmentBase           { id : _endTreatmentRecirculate
        title.text                  : qsTr("Recirculate")
        instructionBased            : true
        hasTimeout                  : vTreatmentRecirculate .timeoutTotal
        timeoutValue                : vTreatmentRecirculate .timeoutCountDown * 60

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

        FooterStatic {
            childrenWidth           : 300
            anchors.fill            : parent.footer
            children                : [
                Item      { },
                TouchRect {
                    text.text       : qsTr("RECONNECT")
                    isDefault       : true
                    onClicked       : reconnectClicked()
                },
                TouchRect {
                    visible         : vTreatmentTime        .time_IsLeft
                    text.text       : qsTr("END TREATMENT")
                    onClicked       : treatmentEndClicked()
                }
            ]
        }
    }

    EndTreatmentBase           { id : _endTreatmentReconnect
        title.text                  : qsTr("Reconnect")
        instructionBased            : true
        header.confirmVisible       : vTreatmentTime        .time_IsLeft
        hasTimeout                  : vTreatmentRecirculate .timeoutTotal
        timeoutValue                : vTreatmentRecirculate .timeoutCountDown * 60
        onConfirmClicked            : recirculateConfirmClicked()

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

        FooterStatic {
            childrenWidth           : 300
            anchors.fill            : parent.footer
            children                : [
                Item      { },
                TouchRect {
                    visible         : vTreatmentTime        .time_IsLeft
                    text.text       : qsTr("BACK TO RECIRCULATE")
                    onClicked       : recirculateBackClicked()
                },
                TouchRect {
                    visible         : vTreatmentTime        .time_IsLeft
                    text.text       : qsTr("END TREATMENT")
                    onClicked       : treatmentEndClicked()
                }
            ]
        }
    }

    onReconnectClicked          : vTreatmentAdjustmentRecirculate.doReconnect   () //84.0
    onRecirculateConfirmClicked : vTreatmentAdjustmentRecirculate.doConfirm     () //84.1
    onRecirculateBackClicked    : vTreatmentAdjustmentRecirculate.doResume      () //84.2
    onTreatmentEndClicked       : vTreatmentAdjustmentRecirculate.doTreatmentEnd() //84.3

    Connections { target: vHDTreatmentStates
        onRcStartedChanged      : { page( _endTreatmentRecirculate      , vrcStarted    )}
        onRcStoppedChanged      : { page( _endTreatmentReconnect        , vrcStopped    )}
    }

    Connections { target: vTreatmentAdjustmentRecirculate
        onAdjustmentTriggered: {
            // 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.
            _endTreatmentRecirculate    .reasonText = vTreatmentAdjustmentRecirculate.text();
            _endTreatmentReconnect      .reasonText = vTreatmentAdjustmentRecirculate.text();
        }
    }
}
