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

    title: qsTr("Treatment Delivery & Blood Handling")

    DebugDataColumn { id: _TDBloodFlowColumn
        textObjectName  :"_TDBloodFlowData"
        title           : qsTr(" BP(H4)")
        x: col(0)
        y: row(0)
        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 ")
        x: col(1.5)
        y: row(0)
        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 ")
        x: col(3)
        y: row(0)
        model: [
            vTDAirPump.airPumpState             ,
            vTDAirPump.airPumpPower             ,

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

    DebugDataColumn { id: _TDAirBubblesColumn
        textObjectName  :"_TDAirBubblesData"
        title           : qsTr(" Bubbles ")
        x: col(3)
        y: row(1.5)
        model: [
            vTDAirBubble.venousAirBubbleStatus            ,
            vTDAirBubble.venousAirBubbleState             ,

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

    DebugDataColumn { id: _TDValvesColumn
        textObjectName  :"_TDValvesData"
        title           : qsTr(" Valves ")
        x: col(4.5)
        y: row(0)
        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 ")
        x: col(6)
        y: row(0)
        model: [
            vTDEjector.state                    ,
            vTDEjector.setSpeed    .toFixed(2)  ,

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

    DebugDataColumn { id: _TDSwitchesColumn
        textObjectName  :"_TDSwitchesData"
        title           : qsTr(" Switches ")
        x: col(7.5)
        y: row(0)
        model: [
            vTDSwitches.door             ,

        ]
        label: [
            "door  (H9)  ",
        ]
    }

    DebugDataColumn { id: _TDTXStatesColumn
        textObjectName  :"_TDTxStatesData"
        title           : qsTr(" TXStates ")
        x: col(0)
        y: row(3.5)
        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: _TDOpModeColumn
        textObjectName  :"_TDOpmodeData"
        title           : qsTr(" Opmode ")
        x: col(0)
        y: row(7)
        model: [
            vTDOpMode.opMode   ,
            vTDOpMode.subMode
        ]
        label: [
            "opMode    " ,
            "subMode     "
        ]
    }

    DebugDataColumn { id: _TDVoltageCoulumn
        textObjectName  :"_TDVoltageData"
        title           : qsTr(" Volume ")
        x: col(1.5)
        y: row(3.5)
        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: _TDPressureOcclusionCoulumn
        textObjectName  :"_TDPressureData"
        title           : qsTr(" Pressure ")
        x: col(3)
        y: row(3.5)
        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 ")
        x: col(4.5)
        y: row(3.5)
        model: [
            vTDTemperature.boardTemp    .toFixed(2)
        ]
        label: [
            "board",
        ]
    }

    DebugDataColumn { id: _TDBatteryCoulumn
        textObjectName  :"_TDBatteryData"
        title           : qsTr(" Battery ")
        x: col(6)
        y: row(3.5)
        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 {
        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: _TDVersionsColumn
                textObjectName  :"_TDVersionsData"
                title           : ""
                horizontalAlignmentText: Text.AlignLeft
                fontPixelSizeText: Fonts.fontPixelDebugTitle
                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]
            }
        }
    }
}
