/*!
 *
 * 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"
import "qrc:/compounds"

/*!
 * \brief   the post treatment prime stack screen
 */
DiagnosticsBase { id: _root
    objectName: "_DiagnosticsDD"

    title: qsTr("Dialysate Delivery")

    DebugDataColumn { id: _DDConcentratePumpColumn
            textObjectName  :"_DDConcentratePumpData"
            title           : qsTr(" Conc Pump ")
            x: col(0)
            y: row(0)
            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           : qsTr(" Dial Pump ")
            x: col(1.5)
            y: row(0)
            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           : qsTr(" Heaters ")
            x: col(3)
            y: row(0)
            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           : qsTr(" Temp ")
            x: col(4.5)
            y: row(0)
            model: [
                vDDTemperatures.inletHeatExchanger       .toFixed(2) ,
                vDDTemperatures.outletHeatExchanger      .toFixed(2) ,
                vDDTemperatures.hydraulicsPrimaryHeater  .toFixed(2) ,
                vDDTemperatures.trimmerHeater            .toFixed(2) ,
                vDDTemperatures.boardTemperature         .toFixed(2) ,
                vDDTemperatures.barometricTemperature    .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.D28MovingAverage         .toFixed(2) ,
                vDDTemperatures.D30MovingAverage         .toFixed(2) ,
            ]
            label: [
                "inHE  (D1)         ",
                "outHE (X6)         ",
                "hydr  (D4)         ",
                "trim  (D50)        ",
                "board              ",
                "baro               ",
                "Cond  (D16)        ",
                "Cond  (D28)        ",
                "Cond  (D30)        ",
                "Cond  (D44)        ",
                "Cond  (D75)        ",
                "mAvg  (D4)         ",
                "mAvg  (D50)        ",
                "mAvg  (D28)        ",
                "mAvg  (D30)        ",
            ]
        }

    DebugDataColumn { id: _DDPressureColumn
        textObjectName  :"_DDPressureData"
        title           : qsTr(" Pressure ")
        x: col(6)
        y: row(0)
        model: [
            vDDPressures.waterInletPSI      .toFixed(2),
            vDDPressures.waterOutletPSI     .toFixed(2),
            vDDPressures.hydrolicOutletPSI  .toFixed(2),
            vDDPressures.bicarbBagPSI       .toFixed(2),
            vDDPressures.spentDialysatePSI  .toFixed(2),
            vDDPressures.freshDialysatePSI  .toFixed(2),
            vDDPressures.transmembranePSI   .toFixed(2),
            vDDPressures.waterInletTemp     .toFixed(2),
            vDDPressures.waterOutletTemp    .toFixed(2),
            vDDPressures.hydrolicOutletTemp .toFixed(2),
            vDDPressures.bicarbBagTemp      .toFixed(2),
            vDDPressures.spentDialysateTemp .toFixed(2),
            vDDPressures.freshDialysateTemp .toFixed(2),
            vDDPressures.transmembraneTemp  .toFixed(2),
        ]
        label: [
            "WInPSI   (M1)     ",
            "WOutPSI  (M3)     ",
            "HOutPSI  (D9)     ",
            "BBagPSI  (D66)    ",
            "SDialPSI (D51)    ",
            "FDialPSI (D18)    ",
            "TransPSI (D41)    ",
            "WInTmp   (M1)     ",
            "WOutTmp  (M3)     ",
            "HOutTmp  (D9)     ",
            "BBagTmp  (D66)    ",
            "SDialTmp (D51)    ",
            "FDialTmp (D18)    ",
            "TransTmp (D41)    ",
        ]
    }

    DebugDataColumn { id: _DDGenDialColumn
        textObjectName  :"_DDGenDialData"
        title           : qsTr(" GenDial ")
        x: col(7.5)
        y: row(0)

        model: [
            vDDGenDialysate.genDialysateExec               ,
            vDDGenDialysate.ddInProgress                   ,
            vDDGenDialysate.isDialGood                     ,
            vDDGenDialysate.fltLevel1                      ,
            vDDGenDialysate.bicarbLevel                    ,
            vDDGenDialysate.spentLevel                     ,
            vDDGenDialysate.hydNegativePressure .toFixed(2),
            vDDGenDialysate.hydPositivePressure .toFixed(2),
            vDDGenDialysate.spentPressure       .toFixed(2),
        ]
        label: [
            "Exec           ",
            "inProg         ",
            "isGood         ",
            "fltLvL (D6)    ",
            "bicarbLvL (D63)",
            "sptLvL (D46)   ",
            "hydNeg (D19)   ",
            "hydPos (D18)   ",
            "sptPres (D51)  ",
        ]
    }

    DebugDataColumn { id: _DDHydraulicsValveColumn
        textObjectName  :"_DDHydraulicValveData"
        title           : qsTr(" Hyd Valve ")
        x: col(7.8)
        y: row(3.1)

        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.waterInletValve       )           ,
        ]
        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)           ",
            "WaterIn (M4)         ",
        ]
    }

    DebugDataColumn { id: _DDCondColumn
        textObjectName  :"_DDCondlData"
        title           : qsTr(" Cond ")
        x: col(9)
        y: row(0)
        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           : qsTr(" Levels ")
        x: col(9)
        y: row(2)
        model: [
            vDDLevel.floaterLevel         ,
            vDDLevel.bicarbLevel          ,
            vDDLevel.spentDialysateLevel  ,
        ]
        label: [
            "floater (D6)  ",
            "bicarb (D63)  ",
            "sptDial (D46) ",
        ]
    }

    DebugDataColumn { id: _DDBCValvesColumn
        textObjectName  :"_DDBCValvesData"
        title           : qsTr(" BC Valves ")
        x: col(9.5)
        y: row(3.3)
        model: [
            Number(vDDValvesStates.balancingChamberValve1 ) ,
            Number(vDDValvesStates.balancingChamberValve2 ) ,
            Number(vDDValvesStates.balancingChamberValve3 ) ,
            Number(vDDValvesStates.balancingChamberValve7 ) ,
            Number(vDDValvesStates.balancingChamberValve5 ) ,
            Number(vDDValvesStates.balancingChamberValve6 ) ,
            Number(vDDValvesStates.balancingChamberValve4 ) ,
            Number(vDDValvesStates.balancingChamberValve8 ) ,
        ]
        label: [
            "BC1 (D23)  ",
            "BC2 (D19)  ",
            "BC3 (D25) ",
            "BC7 (D26) ",
            "BC5 (D24) ",
            "BC6 (D20) ",
            "BC4 (D21) ",
            "BC8 (D22) ",
        ]
    }

    DebugDataColumn { id: _DDBloodLeakColumn
        textObjectName  :"_DDBloodLeakData"
        title           : qsTr(" Blood Leak ")
        x: col(10.5)
        y: row(0)
        model: [
            vDDBloodLeak.bloodLeak         ,
        ]
        label: [
            "BL         ",
        ]
    }

    // TODO: The TouchRect should be unnecessary since the first communication message with UI/TD is the version.
    TouchRect {
        x           : col(9.0)
        y           : row(8.0)
        width       : 425
        height      : 80
        onClicked   : vAdjustmentVersions.doAdjustment()
        radius      : 10
        Column {
            anchors.top : parent.top
            anchors.topMargin: -20
            anchors.left: parent.left
            anchors.leftMargin: 5

            spacing: -30

            DebugDataColumn { id: _DDVersionsColumn
                textObjectName  :"_DDVersionsData"
                title           : ""
                horizontalAlignmentText: Text.AlignLeft
                fontPixelSizeText: Fonts.fontPixelDebugTitle
                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               )
                ]
            }
        }
    }


    Connections { target: _GuiView
        function onDidActionReceive( vAction, vData ) {
            // DEBUG: console.debug("onDidActionReceive", vAction, vData)
            if (vAction === GuiActions.ID_CANBusFaultCount ) {
                _canbusFaultCountText.count = vData[0]
            }
        }
    }
}
