// Qt
import QtQuick 2.12

//  Qml imports
import "qrc:/globals"
import "qrc:/compounds"
import "qrc:/components"
import "qrc:/pages/settings"


SettingsBase    { id: _root
    itemIndex                               : ServiceModeMainMenu.CalibrateHydraulics
    confirmVisible                          : false
    property int currentIndex               : ServiceModeHydraulics.DeaerationPressure
    property string instructionTitle        : ""
    property var    instructionStepNames    : []
    property var    instructionStepImages   : []

    property bool deaerationInProgress      : false

    property var options      : [
        qsTr("Deaeration Pressure"  ),
        qsTr("Flow Pressure"        ),
        qsTr("Balance Chamber"      ),
        qsTr("Acid Pump Volume"     ),
        qsTr("Bic Pump Volume"      ),
        qsTr("UF Pump Volume"       ),
    ]

    enum OptionsIndex {
        DeaerationPressure  ,
        FlowPressure        ,
        BalanceChamber      ,
        AcidPumpVolume      ,
        BicPumpVolume       ,
        UFPumpVolume
    }

    onVisibleChanged        : if ( visible) updateModel ()
    onCurrentIndexChanged   : updateModel ()

    function updateModel () {
        let  stepName           = _root.options[_root.currentIndex] ?? ""
        let group               = vSettings.groupFormat(stepName)
        let instructionsGroup   = vSettings.instructions[group]

        if ( ! instructionsGroup ) { return }

        instructionTitle      = instructionsGroup.title
        instructionStepNames  = instructionsGroup.keys
        instructionStepImages = instructionsGroup.values
    }

    contentItem:  Row { id: _row
        anchors.fill        : parent
        anchors.topMargin   : Variables.defaultMargin * -4
        anchors.bottomMargin: Variables.defaultMargin * 5
        spacing             : Variables.defaultMargin

        Column { id: _optionsColumn
            width       : _row.width * 0.2
            height      : _row.height
            spacing     : Variables.defaultMargin
            topPadding: Variables.defaultMargin * 4

            Repeater { id: _repeater

                model   : _root.options

                delegate : Rectangle { id: _delegate
                    color   : Colors.panelBackgroundColor
                    width   : _optionsColumn.width
                    height  : Variables.adjustmentLabelUnitContainerHeight
                    radius  : Variables.touchRectRadius

                    border {
                        width    : 2
                        color    : _root.currentIndex === index ?   Colors.backgroundButtonSelect :
                                                                    Colors.panelBorderColor
                    }

                    Text {
                        text            : modelData
                        anchors.centerIn: parent
                        color           : Colors.offWhite
                        font.pixelSize  : Fonts.fontPixelDefaultButton
                        font.weight     : Font.Medium
                    }

                    MouseArea {
                        anchors.fill: parent
                        onClicked   :  _root.currentIndex = index
                    }
                }
            }
        }

        InstructionView { id: _instructionView
            title           : _root.instructionTitle
            stepNames       : _root.instructionStepNames
            stepImages      : _root.instructionStepImages
            width           : _row.width * 0.8 - _row.spacing
            height          : _row.height
            showAutoStep    : false
            darkModeOnly    : true
        }
    }

    Row { id: _dataRow
        anchors {
            bottom          : _adjustPumpRate.top
            bottomMargin    : Variables.defaultMargin * 4
            horizontalCenter: parent.horizontalCenter
        }
        spacing : Variables.defaultMargin
        visible : _root.currentIndex === ServiceModeHydraulics.DeaerationPressure

        LabelUnitText { id: _d9Pressure
            width       : _root.width / 6
            height      : Variables.createRxLabelUnitContainerHeight
            label       : qsTr("D9 Pressure")
            unit        : Variables.unitPressure
            value       : vDDPressures.d9Pressure.toFixed(Variables.psiPrecision)
            valueColor  : Colors.offWhite
            color       : Colors.calibrationData
            border.width: 0
        }

        LabelUnitText { id: _d18Pressure
            width       : _root.width / 6
            height      : Variables.createRxLabelUnitContainerHeight
            label       : qsTr("D18 Pressure")
            unit        : Variables.unitPressure
            value       : vDDPressures.d18Pressure.toFixed(Variables.psiPrecision)
            valueColor  : Colors.offWhite
            color       : Colors.calibrationData
            border.width: 0
        }

        LabelUnitText { id: _d4Temp
            width       : _root.width / 6
            height      : Variables.createRxLabelUnitContainerHeight
            label       : qsTr("D4 Temperature")
            unit        : Variables.unitPressure
            value       : vDDTemperatures.d4Temperature.toFixed(Variables.psiPrecision)
            valueColor  : Colors.offWhite
            color       : Colors.calibrationData
            border.width: 0
        }
    }

    LabelUnitContainer { id: _adjustPumpRate
        anchors {
            bottom          : _buttonRow.top
            bottomMargin    : Variables.settingsContentBottomMargin
            horizontalCenter: parent.horizontalCenter
        }
        text                : qsTr("Current Pump Rate")
        unitText            : Varaibles.unitTextPumpRate
        showEdit            : true
        width               :  _root.width / 3
        visible             : _root.currentIndex === ServiceModeHydraulics.DeaerationPressure

//        onEditClicked       : _numPad.open(_pumpRate, qsTr("Current Pump Rate"),"","", Varaibles.unitTextPumpRate)

        contentItem : Text { id: _pumpRate
//            textInput.width             : parent.width - Variables.defaultMargin * 4
            text                        : "23424234"//vTreatmentCreate.patientID
            anchors.centerIn            : parent
//            textInput.maximumLength     : 20  // LEAHI-PRS-236
//            textInput.rightPadding      : Variables.defaultMargin
//            textInput.leftPadding       : Variables.defaultMargin * 14
//            textInput.inputMethodHints  : Qt.ImhPreferLowercase
//            textInput.echoMode          : TextInput.Normal
//            textInput.validator         : RegExpValidator { regExp: Variables.regExp_PatientID }
//            line.visible                : false
//            textInput.onTextChanged     : vTreatmentCreate.patientID = textInput.text

            Text { id: _placeHolderText
                text                : Variables.emptyEntry
                anchors.fill        : parent
                rightPadding        : 125
                horizontalAlignment : Text.AlignRight
                font.pixelSize      : Fonts.fontPixelValueControl
                color               : Colors.offWhite
                visible             :   _pretreatmentPatientIDEntry.textInput.text.length === 0
            }
        }
    }

    Row { id: _buttonRow
        anchors {
            bottom          : parent.bottom
            bottomMargin    : Variables.settingsContentBottomMargin
            horizontalCenter: parent.horizontalCenter
        }
        spacing : Variables.defaultMargin * 3
        visible : _root.currentIndex === ServiceModeHydraulics.DeaerationPressure

        TouchRect { id : _startCalibrationButton
            width       : Variables.defaultButtonWidth
            height      : Variables.defaultButtonHeight
            textString  : qsTr("Start Calibration" )
            isDefault   : true
            visible     : ! _root.deaerationInProgress && _root.currentIndex === ServiceModeHydraulics.DeaerationPressure

            onClicked   : {

                print("Start Calibration" )
                _root.deaerationInProgress = !_root.deaerationInProgress
            }
        }

        TouchRect { id : _cancelButton
            width       : Variables.defaultButtonWidth
            height      : Variables.defaultButtonHeight
            textString  : qsTr("Cancel" )
            isDefault   : true
            visible     : _root.deaerationInProgress && _root.currentIndex === ServiceModeHydraulics.DeaerationPressure

            onClicked   : {

                print("Cancel" )
                _root.deaerationInProgress = !_root.deaerationInProgress
            }
        }

        TouchRect { id : _setPumpRateButton
            width       : Variables.defaultButtonWidth
            height      : Variables.defaultButtonHeight
            textString  : qsTr("Set Pump Rate" )
            isDefault   : true
            visible     : _root.deaerationInProgress && _root.currentIndex === ServiceModeHydraulics.DeaerationPressure

            onClicked   : print("Set Pump Rate" )
        }
    }
}
