/*!
 *
 * Copyright (c) 2020-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    TreatmentAdjustmentDuration.qml
 * \author  (last)      Vy
 * \date    (last)      11-May-2023
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  27-Apr-2020
 *
 */

// Qt
import QtQuick 2.12
import QtQuick.Controls 2.12 // StackView

// Project

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

/*!
 * \brief   TreatmentAdjustmentDuration.qml is the screen
 *          To adjust the treatment duration
 */
TreatmentAdjustmentBase { id: _root
    objectName          : "_TreatmentAdjustmentDuration"  // SquishQt testability

    closeVisible        : true
    confirmVisible      : false
    backVisible         : _treatmentDurationStack.stackView.depth > 1

    onAboutToShow       : {
        // reset the state of the edit screen when edit dialog is first opened to currently set duration
        // cannot reset in StackView.onActivating or edit screen will reset when going back from confirm screen
        // and user modified duration will be reset back to currently set duration
        _treatmentAdjustmentDurationEdit.reset()
        // reset the state of the stack and push the initial screen
        _treatmentDurationStack.reset()
    }
    onClosed            : { _treatmentDurationStack.reset() }
    onBackClicked       : { _treatmentDurationStack.pop()   }

    StackItem { id: _treatmentDurationStack
        objectName  : "_treatmentDurationStack"
        anchors {
            top     : parent.top
            bottom  : notification.top
            left    : parent.left
            right   : parent.right
        }
        visible     : true
        stackView {
            initialItem : _treatmentAdjustmentDurationEdit
            popEnter    : null
            popExit     : null
            pushEnter   : null
            pushExit    : null
        }

        TreatmentAdjustmentDurationEdit { id: _treatmentAdjustmentDurationEdit
            objectName  : "_treatmentAdjustmentDurationEdit"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Edit Treatment Duration")
                _root.notificationText  = ""
            }

            onContinueClicked       : function(vNewTreatmentDuration) {
                // send new duration to TD and wait
                // if accepted moves to confirm screen, otherwise show error in notification
                vTreatmentAdjustmentDurationEdit.doAdjustment(vNewTreatmentDuration)
            }
        }

        TreatmentAdjustmentDurationConfirm { id: _treatmentAdjustmentDurationConfirm
            objectName  : "_treatmentAdjustmentDurationConfirm"
            visible     : false

            StackView.onActivating  : {
                _root.titleText         = qsTr("Confirm Treatment Duration")
                _root.notificationText  = ""
            }

            onConfirmClicked       : function(vNewTreatmentDuration) {
                // send new duration to TD and wait
                // if accepted moves to confirm screen, otherwise show error in notification
                vTreatmentAdjustmentDurationConfirm.doConfirm(vNewTreatmentDuration)
            }
        }

        Connections { target: vTreatmentAdjustmentDurationEdit
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentDurationEdit.adjustment_Accepted) {
                    notification.text = ""

                    _treatmentAdjustmentDurationConfirm.newTreatmentDuration    = vTreatmentAdjustmentDurationEdit.duration
                    _treatmentAdjustmentDurationConfirm.ufVolumeGoal            = vTreatmentAdjustmentDurationEdit.ufVolumeGoal
                    _treatmentAdjustmentDurationConfirm.ufRate                  = vTreatmentAdjustmentDurationEdit.ufRate

                    _treatmentDurationStack.page(_treatmentAdjustmentDurationConfirm)
                }
                else {
                    notification.text = vTreatmentAdjustmentDurationEdit.adjustment_ReasonText
                }
            }
        }

        Connections { target: vTreatmentAdjustmentDurationConfirm
            function onAdjustmentTriggered              ( vValue ) {
                if (vTreatmentAdjustmentDurationConfirm.adjustment_Accepted) {
                    notification.text = ""
                    close()
                }
                else {
                    notification.text = vTreatmentAdjustmentDurationConfirm.adjustment_ReasonText
                }
            }
        }
    }
}
