/*!
 *
 * 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: "_DiagnosticsTD"

    contentItem : Item { id: _content

        DebugDataColumn { id: _TDBloodFlowColumn
            textObjectName  :"_TDBloodFlowData"
            title           : qsTr(" BP(H4)")
            anchors {
                top     : parent.top
                left    : parent.left
            }

            model: [
                vTreatmentBloodFlow.setFlowRate                 ,
                vTreatmentBloodFlow.measFlow        .toFixed(2) ,
                vTreatmentBloodFlow.measRotorSpd    .toFixed(2) ,
                vTreatmentBloodFlow.measPumpSpd     .toFixed(2) ,
                vTreatmentBloodFlow.measCurr        .toFixed(2) ,
                vTreatmentBloodFlow.setRPM          .toFixed(2) ,
                vTreatmentBloodFlow.rotorCount                  ,
                vTreatmentBloodFlow.presFlow                    ,
                vTreatmentBloodFlow.rotorHallState              ,

            ]
            label: [
                "sFLow   (H4)   ",
                "mFlow   (H4)   ",
                "rSpd    (H4)   ",
                "mSpd    (H4)   ",
                "mCurr   (H4)   ",
                "RPM     (H4)   ",
                "rCnt    (H4)   ",
                "pFlow   (H4)   ",
                "rHall   (H6)   ",
            ]
        }

        DebugDataColumn { id: _TDAirTrapCoulumn
            textObjectName  :"_TDAirTrapData"
            title           : qsTr(" Air Trap ")
            anchors {
                top         : parent.top
                left        : _TDPressureOcclusionColumn.right
                leftMargin  : spacing
            }

            model: [
                vTDAirTrap.lowLevel            ,
                vTDAirTrap.upLevel             ,
                vTDAirTrap.lowLevelRaw         ,
                vTDAirTrap.upLevelRaw          ,
                vTDAirTrap.valveIntakeState    ,
                vTDAirTrap.valveOutletState    ,
                vTDAirTrap.controlling         ,
            ]
            label: [
                "ATL    (H17)",
                "ATH    (H16)",
                "rARL   (H17)",
                "rATH   (H16)",
                "IState (H13)",
                "OState (H20)",
                "ctrl        ",
            ]
        }

        DebugDataColumn { id: _TDAirPumpColumn
            textObjectName  :"_TDAirPumpData"
            title           : qsTr(" Air Pumps ")
            anchors {
                top         : _TDAirTrapCoulumn.bottom
                topMargin   : spacing
                left        : _TDAirTrapCoulumn.left
            }

            model: [
                vTDAirPump.airPumpState             ,
                vTDAirPump.airPumpPower             ,

            ]
            label: [
                "state  (H12)  ",
                "power (H12)  ",
            ]
        }

        DebugDataColumn { id: _TDAirBubblesColumn
            textObjectName  :"_TDAirBubblesData"
            title           : qsTr(" Bubbles ")
            anchors {
                top         : _TDPressureOcclusionColumn.bottom
                topMargin   : spacing
                left        : _TDPressureOcclusionColumn.left
            }

            model: [
                vTDAirBubble.venousAirBubbleStatus            ,
                vTDAirBubble.venousAirBubbleState             ,

            ]
            label: [
                "status (H18)  ",
                "state (H18)  ",
            ]
        }

        DebugDataColumn { id: _TDValvesColumn
            textObjectName  :"_TDValvesData"
            title           : qsTr(" Valves ")
            anchors {
                top         : parent.top
                left        : _TDAirTrapCoulumn.right
                leftMargin  : spacing
            }
            model: [
                vTDValves.valveId          ,
                vTDValves.state            ,
                vTDValves.posName          ,
                vTDValves.posCount         ,
                vTDValves.nextPos          ,

            ]
            label: [
                "ID     ",
                "state  ",
                "Name   ",
                "Cnt    ",
                "nPos   ",
            ]
        }

        DebugDataColumn { id: _TDEjectorColumn
            textObjectName  :"_TDEjectorData"
            title           : qsTr(" Ejector ")
            anchors {
                top         : parent.top
                left        : _TDValvesColumn.right
                leftMargin  : spacing
            }
            model: [
                vTDEjector.state                    ,
                vTDEjector.setSpeed    .toFixed(2)  ,

            ]
            label: [
                "State  (H5)    ",
                "Spd    (H5)    " ,
            ]
        }

        DebugDataColumn { id: _TDSwitchesColumn
            textObjectName  :"_TDSwitchesData"
            title           : qsTr(" Switches ")
            anchors {
                top         : _TDVoltageCoulumn.bottom
                topMargin   : spacing
                left        : parent.left
            }
            model: [
                vTDSwitches.door             ,
            ]
            label: [
                "door  (H9)  ",
            ]
        }

        DebugDataColumn { id: _TDOpModeColumn
            textObjectName  :"_TDOpmodeData"
            title           : qsTr(" Opmode ")
            anchors {
                top         : parent.top
                left        : _TDEjectorColumn.right
                leftMargin  : spacing
            }
            model: [
                vTDOpMode.opMode   ,
                vTDOpMode.subMode
            ]
            label: [
                "opMode    " ,
                "subMode     "
            ]
        }

        DebugDataColumn { id: _TDTXStatesColumn
            textObjectName  :"_TDTxStatesData"
            title           : qsTr(" TXStates ")

            anchors {
                top         : _TDOpModeColumn.bottom
                topMargin   : spacing
                left        : _TDOpModeColumn.left
            }

            model: [
                Number(vTDTreatmentStates.subMode        ),
                Number(vTDTreatmentStates.txBloodPrime   ),
                Number(vTDTreatmentStates.txDialysis     ),
                Number(vTDTreatmentStates.ufState        ),
                Number(vTDTreatmentStates.txStop         ),
                Number(vTDTreatmentStates.txRinseback    ),
                Number(vTDTreatmentStates.txRecirculate  ),
                Number(vTDTreatmentStates.txEnd          ),
                Number(vTDTreatmentStates.salineState    ),
                Number(vTDTreatmentStates.heparin        ),
            ]
            label: [
                "subMode    " ,
                "BPrime     " ,
                "Dial       " ,
                "UF         " ,
                "Stop       " ,
                "RB         " ,
                "Recirc     " ,
                "End        " ,
                "Saline     " ,
                "Hep        " ,
            ]
        }

        DebugDataColumn { id: _TDVoltageCoulumn
            textObjectName  :"_TDVoltageData"
            title           : qsTr(" Volume ")
            anchors {
                top         : _TDBloodFlowColumn.bottom
                topMargin   : spacing
            }

            model: [
                vTDVoltage.line_1_2V    .toFixed(2) ,
                vTDVoltage.line_3_3V    .toFixed(2) ,
                vTDVoltage.logic5V      .toFixed(2) ,
                vTDVoltage.sensors5V    .toFixed(2) ,
                vTDVoltage.line_24V     .toFixed(2) ,
                vTDVoltage.regen24V     .toFixed(2) ,
                vTDVoltage.fpgaAdcRef   .toFixed(2) ,
                vTDVoltage.presRef      .toFixed(2) ,
                vTDVoltage.fpgaVcc      .toFixed(2) ,
                vTDVoltage.fpgaVaux     .toFixed(2) ,
                vTDVoltage.fpgaVpvn     .toFixed(2)
            ]
            label: [
                "1.2V"  ,
                "3.3V"  ,
                "L5V"   ,
                "S5V"   ,
                "24V"   ,
                "R24V"  ,
                "ADC"   ,
                "Ref"   ,
                "VCC"   ,
                "Vaux"  ,
                "VPN"   ,
            ]
        }

        DebugDataColumn { id: _TDPressureOcclusionColumn
            textObjectName  :"_TDPressureData"
            title           : qsTr(" Pressure ")

            anchors {
                top         : parent.top
                left        : _TDBloodFlowColumn.right
                leftMargin  : spacing
            }
            model: [
                vTreatmentPressureOcclusion.arterialPressure    .toFixed(2),
                vTreatmentPressureOcclusion.venousPressure                 ,
                vTreatmentPressureOcclusion.limitState                     ,
                vTreatmentPressureOcclusion.arterialMin                    ,
                vTreatmentPressureOcclusion.arterialMax                    ,
                vTreatmentPressureOcclusion.venousMin                      ,
                vTreatmentPressureOcclusion.venousMax                      ,
                vTreatmentPressureOcclusion.arterialLongFilter  .toFixed(2),
                vTreatmentPressureOcclusion.venousLongFilter    .toFixed(2),
                vTreatmentPressureOcclusion.tmpPressure         .toFixed(2),
                vTreatmentPressureOcclusion.tmpMin              .toFixed(2),
                vTreatmentPressureOcclusion.tmpMax              .toFixed(2)
            ]
            label: [
                "Arterial (H12) " ,
                "Venous (H14)   " ,
                "limit          " ,
                "artMin (H12)   " ,
                "artMax (H12)   " ,
                "venMin (H14)   " ,
                "venMax (H14)   " ,
                "aLong (H12)    " ,
                "vLong (H14)    " ,
                "TMP            " ,
                "tmpMin         " ,
                "tmpMax         " ,
            ]
        }

        DebugDataColumn { id: _TDTemperatureCoulumn
            textObjectName  :"_TDTemperatureData"
            title           : qsTr(" Temp ")
            anchors {
                top         : _TDValvesColumn.bottom
                topMargin   : spacing
                left        : _TDValvesColumn.left
            }
            model: [
                vTDTemperature.boardTemp    .toFixed(2)
            ]
            label: [
                "board",
            ]
        }

        DebugDataColumn { id: _TDBatteryCoulumn
            textObjectName  :"_TDBatteryData"
            title           : qsTr(" Battery ")
            anchors {
                top         : _TDEjectorColumn.bottom
                topMargin   : spacing
                left        : _TDEjectorColumn.left
            }
            model: [
                vTDBattery.acPower          ,
                vTDBattery.capacity         ,
            ]
            label: [
                "AC     ",
                "cap    ",
            ]
        }

        //        // TODO: The TouchRect should be unnecessary since the first communication message with UI/TD is the version.
        TouchRect {
            width       : _TDVersionsColumn.width
            height      : _TDVersionsColumn.height
            onClicked   : vAdjustmentVersions.doAdjustment()
            radius      : 5
            anchors {
                bottom          : parent.bottom
                right           : parent.right
            }
            DebugDataColumn { id: _TDVersionsColumn
                textObjectName          :"_TDVersionsData"
                horizontalAlignmentText : Text.AlignLeft
                fontPixelSizeText       : Fonts.fontPixelDebugTitle
                isTouchRect             : true
                model: [ "TD: v%1.%2.%3-%4 v%5.%6.%7-%8 c%9\nTD Serial#: %10"
                    .arg(vAdjustmentVersions.tdVerMajor             )
                    .arg(vAdjustmentVersions.tdVerMinor             )
                    .arg(vAdjustmentVersions.tdVerMicro             )
                    .arg(vAdjustmentVersions.tdVerBuild             )
                    .arg(vAdjustmentVersions.tdVerFPGAMajor         )
                    .arg(vAdjustmentVersions.tdVerFPGAMinor         )
                    .arg(vAdjustmentVersions.tdVerFPGALab           )
                    .arg(vAdjustmentVersions.tdVerFPGAId            )
                    .arg(vAdjustmentVersions.tdVerCompatibilityRev  )
                    .arg(vAdjustmentVersions.tdSerial               )
                ]
            }
        }
    }

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