/*!
 *
 * Copyright (c) 2020-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    TreatmentHeparin.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      02-Apr-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  20-Nov-2020
 *
 */

// Qt
import QtQuick 2.12

// Project
import VTreatmentHeparin 0.1;

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

/*!
 * \brief   Treatment Screen Heparin Management section
 */
TreatmentFluid { id: _root
    readonly property bool isOff            : vHDTreatmentStates.hpOff
    readonly property bool isStopped        : vHDTreatmentStates.hpStopped
    readonly property bool isPaused         : vHDTreatmentStates.hpPaused
    readonly property bool isBolus          : vHDTreatmentStates.hpInitial_bolus
    readonly property bool isDispensing     : vHDTreatmentStates.hpDispensing
    readonly property bool isCompleted      : vHDTreatmentStates.hpCompleted
    readonly property bool isEmpty          : vHDTreatmentStates.hpEmpty
    readonly property bool isRejected       : vTreatmentAdjustmentHeparin.adjustment_Accepted
    readonly property bool displayReason    : _private.displayReason

    readonly property bool isTxRunning      : vHDTreatmentStates.txDialysis

    QtObject { id: _private
        readonly property string textHeparin        : qsTr("HEPARIN DELIVERY")
        readonly property string textOff            : qsTr("OFF")
        readonly property string textPause          : qsTr("PAUSE DELIVERY")
        readonly property string textResume         : qsTr("RESUME DELIVERY")
        readonly property string textComplete       : qsTr("OFF")
        readonly property string textVolumeTarget   : "(" + valueTarget + unit + ")"
        readonly property string mesgCompleted      : qsTr("Maximum Cumulative Heparin Volume Delivered")
        readonly property string mesgBolus          : qsTr("Bolus Active")
        readonly property string mesgEmpty          : qsTr("Syringe Empty")
        readonly property string mesgOff            : qsTr("") // it is redundant since the button text is now display OFF
        readonly property string mesgStopped        : qsTr("Delivery Stopped")
        readonly property string mesgPaused         : qsTr("Delivery Paused")
        readonly property string mesgDispensing     : qsTr("Dispensing Active")

                 property bool      displayReason   : false
        readonly property string mesgRejectReason   : vTreatmentAdjustmentHeparin.adjustment_ReasonText
    }

    title   : qsTr("HEPARIN")
    cumulativeVisible   : false
    targetVisible       : false
    progressBarVisible  : false

    hasArrow        : false
    isTouchable     : false
    buttonEnabled   :  ! (isOff || isStopped || isBolus || isEmpty
                                || isCompleted                          // decided to not to enable user to resume the heparin while completed (1/Apr/2021 - I,Sean,Nick)
                         )      && ! isSBInProgress && isTxRunning
    buttonText      : ! isTxRunning   ? _private.textHeparin  :
                        isDispensing  ? _private.textPause    :
                        isPaused      ? _private.textResume   :
                        isCompleted   ? _private.textComplete :         // decided to not to enable user to resume the heparin while completed (1/Apr/2021 - I,Sean,Nick)
                        isOff         ? _private.textOff      :
                                        _private.textHeparin
    unit            : Variables.unitTextSaline

    valueDecimal    : Variables.heparinPrecision
    valueDelivered  : vTreatmentHeparin.cumulative // this is the cumulative volume of the Heparin bolus and despensing.

    VTreatmentHeparin  { id: vTreatmentHeparin }

    onClicked: {
        // if ( isCompleted  ) vTreatmentAdjustmentHeparin.doResume()   // decided to not to enable user to resume the heparin while completed (1/Apr/2021 - I,Sean,Nick)
        if ( isPaused     ) vTreatmentAdjustmentHeparin.doResume()
        if ( isDispensing ) vTreatmentAdjustmentHeparin.doPause ()
    }

    Connections { target: vTreatmentAdjustmentHeparin
        onAdjustmentTriggered   : {
            _private.displayReason = ! vTreatmentAdjustmentHeparin.adjustment_Accepted
        }
    }
    Connections { target: vHDTreatmentStates
        onHeparinChanged: {
            _private.displayReason = false
        }

    }

    notification {
        visible : true
        text    : displayReason ? _private.mesgRejectReason :
                  isBolus       ? _private.mesgBolus        :
                  isOff         ? _private.mesgOff          :
                  isStopped     ? _private.mesgStopped      :
                  isPaused      ? _private.mesgPaused       :
                  isCompleted   ? _private.mesgCompleted    :
                  isEmpty       ? _private.mesgEmpty        :
                  isDispensing  ? _private.mesgDispensing   : ""
    }
}
