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

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

    title: "Treatment Delivery & Blood Handling"

    DebugDataColumn { id: _TDBloodPumpColumn
        textObjectName  :"_TDBloodPumpData"
        title           : qsTr(" BP ")
        x: col(0)
        y: row(0)
        model: [
            vTDBloodPump.setFlow                       ,
            vTDBloodPump.measFlow          .toFixed(2) ,
            vTDBloodPump.rotorSpeed        .toFixed(2) ,
            vTDBloodPump.motorSpeed        .toFixed(2) ,
            vTDBloodPump.currMotor         .toFixed(2) ,
            vTDBloodPump.setRPM            .toFixed(2) ,
            vTDBloodPump.pressureFlow                  ,
            vTDBloodPump.rotationCount                 ,
            vTDBloodPump.rotorHallState                ,

        ]
        label: [
            "sFLow      ",
            "mFlow      ",
            "rSpd       ",
            "mSpd       ",
            "cMot       ",
            "RPM        ",
            "pFlow      ",
            "rCnt       ",
            "state      ",
        ]
    }

    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.valveState      ,
            vTDAirTrap.controlling     ,
        ]
        label: [
            "ATL",
            "ATU",
            "rATL",
            "rATU",
            "state",
            "ctrl",
        ]
    }

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

        ]
        label: [
            "state      ",
            "Spd        " ,
        ]
    }

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

        ]
        label: [
            "door        ",
        ]
    }


    DebugDataColumn { id: _TDBatteryCoulumn
        textObjectName  :"_TDBatterypData"
        title           : qsTr(" Battery ")
        x: col(0)
        y: row(3.5)
        model: [
            vTDBattery.aCPower          ,
            vTDBattery.capacity         ,
        ]
        label: [
            "AC     ",
            "cap    ",
        ]
    }

    DebugDataColumn { id: _TDVoltageCoulumn
        textObjectName  :"_TDVoltageData"
        title           : qsTr(" Vol ")
        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: _TDTemperatureCoulumn
        textObjectName  :"_TDTemperatureData"
        title           : qsTr(" Temp ")
        x: col(3)
        y: row(3.5)
        model: [
            vTDTemperature.boardTemp    .toFixed(2)
        ]
        label: [
            "board",
        ]
    }

    // TODO: The TouchRect should be unnecessary since the first communication message with UI/HD is the version.
    TouchRect {
        x           : col(6.0)
        y           : row(6.4)
        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]
            }
        }
    }
}
