/*!
 *
 * 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    TreatmentAdjustmentFlow.qml
 * \author  (last)      Behrouz NematiPour
 * \date    (last)      03-Oct-2022
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  22-Mar-2020
 *
 */

// Qt
import QtQuick 2.12

// Project

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

/*!
 * \brief   TreatmentAdjustmentFlow.qml is the screen
 *          To adjust the treatment blood/dialysate flow
 */
TreatmentAdjustmentBase { id: _root
    contentItem.objectName: "TreatmentAdjustmentFlow"  //SquishQt testability

    property alias      bloodFlowRateValue   :     _bloodFlowSlider.value
    property alias      bloodFlowRateMinimum :     _bloodFlowSlider.minimum
    property alias      bloodFlowRateMaximum :     _bloodFlowSlider.maximum
    property alias      bloodFlowRateStep    :     _bloodFlowSlider.step

    property alias  dialysateFlowRateValue   : _dialysateFlowSlider.value
    property alias  dialysateFlowRateMinimum : _dialysateFlowSlider.minimum
    property alias  dialysateFlowRateMaximum : _dialysateFlowSlider.maximum
    property alias  dialysateFlowRateStep    : _dialysateFlowSlider.step

    property int    textWidth   : 200

    titleText: qsTr("FLOWS")

    // Note :   The slider values are not bound and will be set by user freely
    //          and will be used to be sent for adjustment
    //          if accepted will not changed and
    //          if rejected then will be reset to the values passed by adjustment response

    // reset the flow values to the current flow values
    function currentFlows() {
            _bloodFlowSlider.value =     vTreatmentBloodFlow.    bloodFlow_FlowSetPoint
        _dialysateFlowSlider.value = vTreatmentDialysateFlow.dialysateFlow_FlowSetPoint
    }

    // reset the flow values to the values of the adjustment response
    function resetFlows() {
            _bloodFlowSlider.value = vTreatmentAdjustmentFlows.    bloodFlow_FlowSetPoint
        _dialysateFlowSlider.value = vTreatmentAdjustmentFlows.dialysateFlow_FlowSetPoint
    }

    onAboutToShow: {
        // TODO : When Testing data on Setting screen is removed
        //        This needs to be moved into the Treatment______.qml
        currentFlows()
    }

    Connections { target: vTreatmentAdjustmentFlows
        onAdjustmentTriggered: {
            if ( vTreatmentAdjustmentFlows.adjustment_Accepted ) {
                accept()
            } else {
                resetFlows()
                console.debug(vTreatmentAdjustmentFlows.text())
                notification.text = vTreatmentAdjustmentFlows.text()
            }
        }
    }

    Column { id : _flowsColumn
        spacing: 100
        anchors.centerIn: parent
        Row { id : _bloodFlowRow
            spacing: 100
            anchors.horizontalCenter: parent.horizontalCenter
            TextRect { id: _bloodFlowTextRect
                anchors.top: parent.top
                anchors.topMargin: 25
                width: textWidth
                title: qsTr("Blood Flow Rate")
                label: _bloodFlowSlider.value
                extra: Variables.unitTextFlowRate
                labelFont.weight: Font.ExtraLight
            }
            Slider { id : _bloodFlowSlider
                anchors.verticalCenter: parent.verticalCenter
                width   : 600
                height  : 5
                minimum : Variables.bloodFlowMin
                maximum : Variables.bloodFlowMax
                unit    : Variables.unitTextFlowRate
                step    : Variables.bloodFlowResolution
                ticks   : true
            }
        }
        Line {
            anchors.horizontalCenter: parent.horizontalCenter
            length  : _root.width - 100
            color   : Colors.separatorLine
        }
        Row { id : _dialysateInletFlowRow
            spacing: 100
            anchors.horizontalCenter: parent.horizontalCenter
            TextRect { id: _dialysateInletFlowTextRect
                anchors.top: parent.top
                anchors.topMargin: 25
                width: textWidth
                title: qsTr("Dialysate Flow Rate")
                label: _dialysateFlowSlider.value
                extra: Variables.unitTextFlowRate
                labelFont.weight: Font.ExtraLight
            }
            Slider { id : _dialysateFlowSlider
                anchors.verticalCenter: parent.verticalCenter
                width   : 600
                height  : 5
                minimum : vTreatmentRanges.treatmentRanges_Dialysate_Flow_Min
                maximum : vTreatmentRanges.treatmentRanges_Dialysate_Flow_Max
                unit    : Variables.unitTextFlowRate
                step    : Variables.dialysateFlowResolution
                ticks   : true
            }
        }
    }
}
