/*!
 *
 * Copyright (c) 2021-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    DiagnosticsDialog.qml
 * \author  (last)      Dara Navaei
 * \date    (last)      05-Mar-2024
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  11-May-2021
 *
 */

// Qt
import QtQuick 2.12
// Project
import Gui.Actions      0.1

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

/*!
 * \brief   the post treatment prime stack screen
 */
DiagnosticsBase { id: _root
    objectName  : "_DiagnosticsDD"
    verticalSpacing: Variables.defaultMargin * 0.5

    contentItem : Item { id: _content

        DebugDataColumn { id: _DDConcentratePumpColumn
            textObjectName  :"_DDConcentratePumpData"
            title           : "[0x25]" + qsTr(" Conc Pump ")
            anchors {
                top     : parent.top
                left    : parent.left
            }
            model: [
                vDDConcentratePump.D10PumpCurrentSetSpeed      .toFixed(2) ,
                vDDConcentratePump.D10PumpMeasuredSpeed        .toFixed(2) ,
                vDDConcentratePump.D10PumpTargetRevCount                   ,
                vDDConcentratePump.D10PumpMeasuredRevCount                 ,
                vDDConcentratePump.D10PumpState                            ,
                vDDConcentratePump.D10PumpPulseUS              .toFixed(2) ,
                vDDConcentratePump.D10PumpTargetSpeed          .toFixed(2) ,
                vDDConcentratePump.D10PumpParked                           ,
                vDDConcentratePump.D10PumpParkFault                        ,
                vDDConcentratePump.D11PumpCurrentSetSpeed      .toFixed(2) ,
                vDDConcentratePump.D11PumpMeasuredSpeed        .toFixed(2) ,
                vDDConcentratePump.D11PumpTargetRevCount                   ,
                vDDConcentratePump.D11PumpMeasuredRevCount                 ,
                vDDConcentratePump.D11PumpState                            ,
                vDDConcentratePump.D11PumpPulseUS              .toFixed(2) ,
                vDDConcentratePump.D11PumpTargetSpeed          .toFixed(2) ,
                vDDConcentratePump.D11PumpParked                           ,
                vDDConcentratePump.D11PumpParkFault                        ,
                vDDConcentratePump.D76PumpCurrentSetSpeed      .toFixed(2) ,
                vDDConcentratePump.D76PumpMeasuredSpeed        .toFixed(2) ,
                vDDConcentratePump.D76PumpTargetRevCount                   ,
                vDDConcentratePump.D76PumpMeasuredRevCount                 ,
                vDDConcentratePump.D76PumpState                            ,
                vDDConcentratePump.D76PumpPulseUS              .toFixed(2) ,
                vDDConcentratePump.D76PumpTargetSpeed          .toFixed(2) ,
            ]
            label: [
                "sSpd (D10)        ",
                "mSpd (D10)        ",
                "rCnt (D10)        ",
                "mCnt (D10)        ",
                "State (D10)       ",
                "Plse (D10)        ",
                "tSpd (D10)        ",
                "park (D10)        ",
                "pfault (D10)      ",
                "sSpd (D11)        ",
                "mSpd (D11)        ",
                "rCnt (D11)        ",
                "mCnt (D11)        ",
                "State (D11)       ",
                "Plse (D11)        ",
                "tSpd (D11)        ",
                "park (D11)        ",
                "pfault (D11)      ",
                "sSpd  (D76)       ",
                "mSpd  (D76)       ",
                "rCnt  (D76)       ",
                "mCnt  (D76)       ",
                "State (D76)       ",
                "Plse  (D76)       ",
                "tSpd  (D76)       ",
            ]
        }

        DebugDataColumn { id: _DDDialysatePumpColumn
            textObjectName  :"_DDDialysatePumpData"
            title           : "[0x27]" + qsTr(" Dialysate Pump ")
            anchors {
                top         : parent.top
                left        : _DDConcentratePumpColumn.right
                leftMargin  : spacing
            }
            model: [
                vDDDialysatePump.D12PumpTargetRPM             .toFixed(2) ,
                vDDDialysatePump.D12PumpMeasuredSpeed         .toFixed(2) ,
                vDDDialysatePump.D12PumpCurrentSpeed          .toFixed(2) ,
                vDDDialysatePump.D12PumpState                             ,
                vDDDialysatePump.D12PumpTargetPressure        .toFixed(2) ,
                vDDDialysatePump.D12PumpMeasuredPressure      .toFixed(2) ,
                vDDDialysatePump.D12PumpMeasuredCurrent       .toFixed(2) ,
                vDDDialysatePump.D12PumpControl                           ,
                vDDDialysatePump.D12PumpDirErrCnt                         ,
                vDDDialysatePump.D12PumpMeasuredDir                       ,
                vDDDialysatePump.D48PumpTargetRPM             .toFixed(2) ,
                vDDDialysatePump.D48PumpMeasuredSpeed         .toFixed(2) ,
                vDDDialysatePump.D48PumpCurrentSpeed          .toFixed(2) ,
                vDDDialysatePump.D48PumpState                             ,
                vDDDialysatePump.D48PumpTargetPressure        .toFixed(2) ,
                vDDDialysatePump.D48PumpMeasuredPressure      .toFixed(2) ,
                vDDDialysatePump.D48PumpMeasuredCurrent       .toFixed(2) ,
                vDDDialysatePump.D48PumpControl                           ,
                vDDDialysatePump.D48PumpDirErrCnt                         ,
                vDDDialysatePump.D48PumpMeasuredDir                       ,
            ]
            label: [
                "tRPM (D12)     ",
                "mSpd (D12)     ",
                "cSpd (D12)     ",
                "State (D12)    ",
                "tPres (D12)    ",
                "mPres (D12)    ",
                "Ctrl (D12)     ",
                "eCnt (D12)     ",
                "mCurr (D12)    ",
                "mDir (D12)     ",
                "tRPM (D48)     ",
                "mSpd (D48)     ",
                "cSpd (D48)     ",
                "State (D48)    ",
                "tPres (D48)    ",
                "mPres (D48)    ",
                "mCurr (D48)    ",
                "Ctrl (D48)     ",
                "eCnt (D48)     ",
                "mDir (D48)     ",
            ]
        }

        DebugDataColumn { id: _DDHeatersColumn
            textObjectName  :"_DDHeatersData"
            title           : "[0x28]" + qsTr(" Heaters ")
            anchors {
                top         : parent.top
                left        : _DDDialysatePumpColumn.right
                leftMargin  : spacing
            }
            model: [
                vDDHeaters.mainPrimayHeaterDC           .toFixed(2) ,
                vDDHeaters.primaryTargetTemp            .toFixed(2) ,
                vDDHeaters.primaryHeaterState                       ,
                vDDHeaters.primaryHeaterIntervalCount               ,
                vDDHeaters.trimmerHeaterDC              .toFixed(2) ,
                vDDHeaters.trimmerTargetTemp            .toFixed(2) ,
                vDDHeaters.trimmerHeaterState                       ,
                vDDHeaters.trimmerHeaterIntervalCount               ,
                vDDHeaters.dbg1                         .toFixed(2) ,
                vDDHeaters.dbg2                         .toFixed(2) ,
                vDDHeaters.dbg3                         .toFixed(2) ,
                vDDHeaters.dbg4                         .toFixed(2) ,
                vDDHeaters.dbg5                         .toFixed(2) ,
                vDDHeaters.dbg6                         .toFixed(2) ,
                vDDHeaters.dbg7                         .toFixed(2) ,
                vDDHeaters.dbg8                         .toFixed(2) ,
                vDDHeaters.dbg9                         .toFixed(2) ,
            ]
            label: [
                "pHeat (D5)         ",
                "pTemp (D5)         ",
                "pState (D5)        ",
                "pIC (D5)           ",
                "tHeat (D45)        ",
                "tTemp (D45)        ",
                "tstate (D45)       ",
                "tIC (D45)          ",
                "dbg1               ",
                "dbg2               ",
                "dbg3               ",
                "dbg4               ",
                "dbg5               ",
                "dbg6               ",
                "dbg7               ",
                "dbg8               ",
                "dbg9               ",
            ]
        }

        DebugDataColumn { id: _DDTemperatureColumn
            textObjectName  :"_DDTemperatureData"
            title           : "[0x26]" + qsTr(" Temperature ")
            anchors {
                top         : parent.top
                left        : _DDHeatersColumn.right
                leftMargin  : spacing
            }
            model: [
                vDDTemperatures.d1Temperature       .toFixed(2) ,
                vDDTemperatures.d78Temperature      .toFixed(2) ,
                vDDTemperatures.d4Temperature       .toFixed(2) ,
                vDDTemperatures.d50Temperature      .toFixed(2) ,
                vDDTemperatures.d99Temperature      .toFixed(2) ,
                vDDTemperatures.boardTemperature    .toFixed(2) ,
                vDDTemperatures.d16Conductivity     .toFixed(2) ,
                vDDTemperatures.d28Conductivity     .toFixed(2) ,
                vDDTemperatures.d30Conductivity     .toFixed(2) ,
                vDDTemperatures.d44Conductivity     .toFixed(2) ,
                vDDTemperatures.d75Conductivity     .toFixed(2) ,
                vDDTemperatures.d4MovingAverage     .toFixed(2) ,
                vDDTemperatures.d50MovingAverage    .toFixed(2) ,
                vDDTemperatures.d99MovingAverage    .toFixed(2) ,
                vDDTemperatures.d28MovingAverage    .toFixed(2) ,
                vDDTemperatures.d30MovingAverage    .toFixed(2) ,
                vDDTemperatures.d78MovingAverage    .toFixed(2) ,
                vDDTemperatures.d9PressureAverage   .toFixed(2) ,
                vDDTemperatures.d66PressureAverage  .toFixed(2) ,
                vDDTemperatures.d51PressureAverage  .toFixed(2) ,
                vDDTemperatures.d18PressureAverage  .toFixed(2) ,
                vDDTemperatures.d41PressureAverage  .toFixed(2) ,
                vDDTemperatures.d87PressureAverage  .toFixed(2) ,
            ]
            label: [
                "mTmp  (D1)         ",
                "mTmp  (D78)        ",
                "mTmp  (D4)         ",
                "mTmp  (D50)        ",
                "mTmp  (D99)        ",
                "board              ",
                "Cond  (D16)        ",
                "Cond  (D28)        ",
                "Cond  (D30)        ",
                "Cond  (D44)        ",
                "Cond  (D75)        ",
                "mAvg  (D4)         ",
                "mAvg  (D50)        ",
                "mAvg  (D99)        ",
                "mAvg  (D28)        ",
                "mAvg  (D30)        ",
                "mAvg  (D78)        ",
                "mPressAvg  (D9)    ",
                "mPressAvg  (D66)   ",
                "mPressAvg  (D51)   ",
                "mPressAvg  (D18)   ",
                "mPressAvg  (D41)   ",
                "mPressAvg  (D87)   ",
            ]
        }

        DebugDataColumn { id: _DDPressureColumn
            textObjectName  :"_DDPressureData"
            title           : "[0x1C]" + qsTr(" Pressure ")
            anchors {
                top         : parent.top
                left        : _DDTemperatureColumn.right
                leftMargin  : spacing
            }
            model: [
                vDDPressures.d9Pressure   .toFixed(2),
                vDDPressures.d66Pressure  .toFixed(2),
                vDDPressures.d51Pressure  .toFixed(2),
                vDDPressures.d18Pressure  .toFixed(2),
                vDDPressures.d41Pressure  .toFixed(2),
                vDDPressures.d87Pressure  .toFixed(2),
            ]
            label: [
                "D9     ",
                "D66    ",
                "D51    ",
                "D18    ",
                "D41    ",
                "D87    ",
            ]
        }

        DebugDataColumn { id: _DDOpModeColumn
            textObjectName  :"_DDOpModeColumn"
            title           : "[0x13]" + qsTr(" OpMode ")
            anchors {
                top         : parent.top
                left        : _DDPressureColumn.right
                leftMargin  : spacing
            }
            model: [
                vDDOperationMode.opMode     ,
                vDDOperationMode.subMode    ,
            ]
            label: [
                "opMode     " ,
                "subMode    "
            ]
        }

        DebugDataColumn { id: _DDGenDialColumn
            textObjectName  :"_DDGenDialData"
            title           : "[0x2F]" + qsTr(" GenDial ")
            anchors {
                top         : _DDOpModeColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDOpModeColumn.left
            }
            model: [
                vDDGenDialysate.genDialysateExec               ,
                vDDGenDialysate.ddInProgress                   ,
                vDDGenDialysate.isDialGood                     ,
                vDDGenDialysate.currentQd       .toFixed(2)    ,
            ]
            label: [
                "Exec           ",
                "inProg         ",
                "isGood         ",
                "current Qd     ",
            ]
        }

        DebugDataColumn { id: _DDHydraulicsValveColumn
            textObjectName  :"_DDHydraulicValveData"
            title           : "[0x1B]" + qsTr(" Hyd Valve ")
            anchors {
                top         : _DDGenDialColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDGenDialColumn.left
            }

            model: [
                Number(vDDValvesStates.hydraulicsOutletValve )           ,
                Number(vDDValvesStates.thermalDisinfectValve )           ,
                Number(vDDValvesStates.HydraulicsBypassValve )           ,
                Number(vDDValvesStates.rinsePortValve        )           ,
                Number(vDDValvesStates.drainValve            )           ,
                Number(vDDValvesStates.dialyzerBypassValve   )           ,
                Number(vDDValvesStates.purgeValve1           )           ,
                Number(vDDValvesStates.pressureTestValve     )           ,
                Number(vDDValvesStates.dryBicarbInletValve   )           ,
                Number(vDDValvesStates.dialyzerInletValve    )           ,
                Number(vDDValvesStates.dialyzerOutletValve   )           ,
                Number(vDDValvesStates.purgeValve2           )           ,
                Number(vDDValvesStates.hydraulicsInletValve  )           ,
                Number(vDDValvesStates.dryBicarbValve        )           ,
                Number(vDDValvesStates.deScalerValve        )            ,
            ]
            label: [
                "HydOut (D14)         ",
                "ThemD (D52)          ",
                "HydByP (D8)          ",
                "RinseP (D54)         ",
                "Drain (D53)          ",
                "DialByP (D34)        ",
                "PurgV1 (D64)         ",
                "PresT (D31)          ",
                "DryBic (D65)         ",
                "DialIn (D35)         ",
                "DialOut (D40)        ",
                "PurgV2 (D47)         ",
                "HydIn (D3)           ",
                "Dry Bicarb (D80)     ",
                "De-scaler (D81)      ",
            ]
        }

        DebugDataColumn { id: _DDCondColumn
            textObjectName  :"_DDCondlData"
            title           : "[0x1F]" + qsTr(" Conductivity ")
            anchors {
                top         : _DDDialysatePumpColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDDialysatePumpColumn.left
            }
            model: [
                vDDConductivity.bicarbCondutivity1     .toFixed(2),
                vDDConductivity.acidBicarbCondutivity1 .toFixed(2),
                vDDConductivity.acidBicarbCondutivity2 .toFixed(2),
                vDDConductivity.spentCondutivity1      .toFixed(2),
                vDDConductivity.bicarbCondutivity2     .toFixed(2),
            ]
            label: [
                "bicarb1 (D17) ",
                "acidb1 (D27)  ",
                "acidb1 (D29)  ",
                "spent (D43)   ",
                "bicarb2 (D74) ",
            ]
        }

        DebugDataColumn { id: _DDLevelColumn
            textObjectName  :"_DDLevelData"
            title           : "[0x29]" + qsTr(" Levels ")
            anchors {
                top         : _DDTemperatureColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDTemperatureColumn.left
            }
            model: [
                vDDLevel.d46Level       ,
                vDDLevel.d63Level       ,
                vDDLevel.d98Level       ,
                vDDLevel.d6Level        ,
                vDDLevel.bicardLevel    ,
            ]
            label: [
                "D46    ",
                "D63    ",
                "D98    ",
                "D6     ",
                "bicarb ",
            ]
        }

        DebugDataColumn { id: _DDBCValvesColumn
            textObjectName  :"_DDBCValvesData"
            title           : "[0x1B]" + qsTr(" BC Valves ")
            anchors {
                top         : _DDBloodLeakColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDBloodLeakColumn.left
            }
            model: [
                Number(vDDValvesStates.balancingChamberValve1 ) ,
                Number(vDDValvesStates.balancingChamberValve2 ) ,
                Number(vDDValvesStates.balancingChamberValve3 ) ,
                Number(vDDValvesStates.balancingChamberValve4 ) ,
                Number(vDDValvesStates.balancingChamberValve5 ) ,
                Number(vDDValvesStates.balancingChamberValve6 ) ,
                Number(vDDValvesStates.balancingChamberValve7 ) ,
                Number(vDDValvesStates.balancingChamberValve8 ) ,
            ]
            label: [
                "BC1 (D23) ",
                "BC2 (D19) ",
                "BC3 (D25) ",
                "BC4 (D21) ",
                "BC5 (D24) ",
                "BC6 (D20) ",
                "BC7 (D26) ",
                "BC8 (D22) ",
            ]
        }

        DebugDataColumn { id: _DDIOValvesColumn
            textObjectName  :"_DDIOValvesColumn"
            title           : "[0x1B]" + qsTr(" IO Valves ")
            anchors {
                top         : _DDCondColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDCondColumn.left
            }
            model: [
                Number(vDDValvesStates.ioValve0 ) ,
                Number(vDDValvesStates.ioValve1 ) ,
            ]
            label: [
                "IO0 (M4)  ",
                "IO1 (M12) ",
            ]
        }

        DebugDataColumn { id: _DDSpareValvesColumn
            textObjectName  :"_DDSpareValvesColumn"
            title           : "[0x1B]" + qsTr(" Spare Valves ")
            anchors {
                top         : _DDHeatersColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDHeatersColumn.left
            }
            model: [
                Number(vDDValvesStates.spareValve0 ) ,
                Number(vDDValvesStates.spareValve1 ) ,
                Number(vDDValvesStates.spareValve2 ) ,
                Number(vDDValvesStates.spareValve3 ) ,
                Number(vDDValvesStates.spareValve4 ) ,
                Number(vDDValvesStates.spareValve5 ) ,
                Number(vDDValvesStates.spareValve6 ) ,
                Number(vDDValvesStates.spareValve7 ) ,
            ]
            label: [
                "D79            ",
                "D82            ",
                "D91            ",
                "Spare Valve 3  ",
                "Spare Valve 4  ",
                "Spare Valve 5  ",
                "D100           ",
                "Spare Valve 7  ",
            ]
        }

        DebugDataColumn { id: _DDFPValvesColumn
            textObjectName  :"_DDFPValvesColumn"
            title           : "[0x1B]" + qsTr(" FP Valves ")
            anchors {
                top         : _DDConcentratePumpColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDConcentratePumpColumn.left
            }
            model: [
                Number(vDDValvesStates.fpValve0)    ,
                Number(vDDValvesStates.fpValve1)    ,
                Number(vDDValvesStates.fpValve2)    ,
                Number(vDDValvesStates.fpValve3)    ,
                Number(vDDValvesStates.fpValve4)    ,
                Number(vDDValvesStates.fpValve5)    ,
            ]
            label: [
                "P11    ",
                "P33    ",
                "P34    ",
                "P37    ",
                "P39    ",
                "P6     ",
            ]
        }

        DebugDataColumn { id: _DDBloodLeakColumn
            textObjectName  :"_DDBloodLeakData"
            title           : "[0x61]" + qsTr(" Blood Leak ")
            anchors {
                top         : _DDPressureColumn.bottom
                topMargin   : verticalSpacing
                left        : _DDPressureColumn.left
            }
            model: [
                vDDBloodLeak.bloodLeakStatus                        ,
                vDDBloodLeak.bloodLeakState                         ,
                vDDBloodLeak.bloodLeakPersistentCounter             ,
                vDDBloodLeak.bloodLeakSerialCommState               ,
                vDDBloodLeak.bloodLeakIntensity                     ,
                vDDBloodLeak.bloodLeakDetect                        ,
                vDDBloodLeak.bloodLeakIntensityMovingAvg.toFixed(2) ,
                vDDBloodLeak.bloodLeakTimeSinceZeroMS               ,
                vDDBloodLeak.driftInRangeStatus                     ,
                vDDBloodLeak.driftUpperRangeStatus                  ,
            ]
            label: [
                "Status                      ",
                "State                       ",
                "PersistentCounter           ",
                "SerialCommState             ",
                "Intensity                   ",
                "Detect                      ",
                "IntensityMovingAvg          ",
                "TimeSinceZeroMS             ",
                "DriftInRangeStatus          ",
                "DriftUpperRangeStatus       ",
            ]
        }

        // TODO: The TouchRect should be unnecessary since the first communication message with UI/TD is the version.
        TouchRect {
            width       : _DDVersionsColumn.width
            height      : _DDVersionsColumn.height
            onClicked   : vAdjustmentVersions.doAdjustment()
            radius      : 5
            anchors {
                bottom          : parent.bottom
                right           : parent.right
            }

            DebugDataColumn { id: _DDVersionsColumn
                textObjectName  :"_DDVersionsData"
                title           : ""
                horizontalAlignmentText: Text.AlignLeft
                fontPixelSizeText: Fonts.fontPixelDebugTitle
                isTouchRect             : true
                model: [ "DD: v%1.%2.%3-%4 v%5.%6.%7-%8 c%9\nDD Serial#: %10"
                    .arg(vAdjustmentVersions.ddVerMajor             )
                    .arg(vAdjustmentVersions.ddVerMinor             )
                    .arg(vAdjustmentVersions.ddVerMicro             )
                    .arg(vAdjustmentVersions.ddVerBuild             )
                    .arg(vAdjustmentVersions.ddVerFPGAMajor         )
                    .arg(vAdjustmentVersions.ddVerFPGAMinor         )
                    .arg(vAdjustmentVersions.ddVerFPGALab           )
                    .arg(vAdjustmentVersions.ddVerFPGAId            )
                    .arg(vAdjustmentVersions.ddVerCompatibilityRev  )
                    .arg(vAdjustmentVersions.ddSerial               )
                ]
            }
        }
    }
}
