/*!
 *
 * Copyright (c) 2021-2022 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)      Behrouz NematiPour
 * \date    (last)      30-Jul-2021
 * \author  (original)  Behrouz NematiPour
 * \date    (original)  11-May-2021
 *
 */

// Qt
import QtQuick 2.12

// Project
import Gui.Actions      0.1
import Gui.VEventSpy    0.1

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

/*!
 * \brief   Diagnostics is the screen
 */
ModalDialog { id: _root

    width   : Variables.applicationWidth
    height  : Variables.applicationHeight   - 60

    function col(idx, gap = 0) {
        return idx * 155 + gap
    }

    function row(idx, gap = 10) {
        return idx * 100 + gap
    }

    Rectangle {
        anchors.centerIn: _closeButton
        border.color    : Colors.white
        color           : Colors.transparent
        border.width    : Variables.borderWidth
        width           : _closeButton.width + 10
        height          : width
        radius          : width
    }
    CloseButton { id    : _closeButton
        anchors.left    : undefined
        anchors.right   : parent.right
        anchors.top     : undefined
        anchors.bottom  : parent.bottom
        anchors.margins : 10
        onClicked       : close()
    }

    DebugDataColumn { id: _DGROPumpColumn
        x: col(0)
        y: row(0)
        title: qsTr(" ROP ")
        model: [
            vDGROPump.pressure .toFixed(2) ,
            vDGROPump.flowRate .toFixed(2) ,
            vDGROPump.pwm      .toFixed(2) ,
            vDGROPump.state
        ]
        label: [
            "MaxPrs",
            "Flow",
            "PWM",
            "stt"]
        textObjectName: "_DGROPumpData"
    }

    DebugDataColumn { id: _DGDrainPumpColumn
        x: col(1)
        y: row(0)
        title: qsTr(" DRP ")
        model: [
            vDGDrainPump.rpmi   ,
            vDGDrainPump.dac    ,
            vDGDrainPump.state  ,
            vDGDrainPump.rpmo
        ]
        label: [
            "TgtSpd",
            "DAC",
            "stt",
            "Spd",
        ]
        textObjectName: "_DGDrainPumpData"
    }

    DebugDataColumn { id: _DGPressuresColumn
        x: col(2)
        y: row(0)
        title: qsTr(" Prssr ")
        model: [
            vDGPressures.roInletPSI     .toFixed(2) ,
            vDGPressures.roOutletPSI    .toFixed(2) ,
            vDGPressures.drainInletPSI  .toFixed(2) ,
            vDGPressures.drainOutletPSI .toFixed(2)
        ]
        label: [
            "PPi",
            "PPo",
            "PRd",
            "PDr",
        ]
        textObjectName: "_DGPressuresData"
    }

    DebugDataColumn { id: _DGReservoirColumn
        x: col(3)
        y: row(0)
        title: qsTr(" Rsrvr ")
        model: [
            vDGReservoir.activeReservoir ,
            vDGReservoir.fillToVol       ,
            vDGReservoir.drainToVol      ,
        ]
        label: [
            "Act",
            "Fil",
            "Drn",
        ]
        textObjectName: "_DGReservoirData"
    }

    DebugDataColumn { id: _DGHeatersColumn
        x: col(4)
        y: row(0)
        title: qsTr(" Heatrs ")
        model: [
            vDGHeaters.mainPrimaryDC    .toFixed(2),
            vDGHeaters.smallPrimaryDC   .toFixed(2),
            vDGHeaters.trimmerDC        .toFixed(2),
        ]
        label: [
            "HP1",
            "HP2",
            "HR",
        ]
        textObjectName: "_DGHeatersData"
    }

    DebugDataColumn { id: _LoadCellReadingsColumn
        x: col(5)
        y: row(0)
        title: qsTr(" LoadCl ")
        model: [
            vDGLoadCellReadings.reservoir1Prim .toFixed(2) ,
            vDGLoadCellReadings.reservoir1Bkup .toFixed(2) ,
            vDGLoadCellReadings.reservoir2Prim .toFixed(2) ,
            vDGLoadCellReadings.reservoir2Bkup .toFixed(2)
        ]
        label: [
            "A1",
            "A2",
            "B1",
            "B2",
        ]
        textObjectName: "_DGLoadCellReadingsData"
    }

    DebugDataColumn { id: _DGTemperaturesColumn
        x: col(6)
        y: row(0)
        title: qsTr(" Tmprtr ")
        model: [
            vDGTemperatures.inletPrimaryHeater          .toFixed(2) ,
            vDGTemperatures.outletPrimaryHeater         .toFixed(2) ,
            vDGTemperatures.conductivitySensor1         .toFixed(2) ,
            vDGTemperatures.conductivitySensor2         .toFixed(2) ,
            vDGTemperatures.outletRedundancy            .toFixed(2) ,
            vDGTemperatures.inletDialysate              .toFixed(2) ,
            vDGTemperatures.primaryHeaterThermoCouple   .toFixed(2) ,
            vDGTemperatures.trimmerHeaterThermoCouple   .toFixed(2) ,
            vDGTemperatures.primaryHeaterColdJunction   .toFixed(2) ,
            vDGTemperatures.trimmerHeaterColdJunction   .toFixed(2) ,
            vDGTemperatures.primaryHeaterInternal       .toFixed(2) ,
            vDGTemperatures.trimmerHeaterInternal       .toFixed(2)
        ]
        label: [
            "TPi",
            "TPo",
            "TD1",
            "TD2",
            "TRo",
            "TDi",
            "HP.Trm",
            "HR.Trm",
            "HP.CJ",
            "HR.CJ",
            "HP.Int",
            "HR.Int",
        ]
        textObjectName: "_DGTemperaturesData"
    }

    // Next row
    DebugDataColumn { id: _DGValvesStatesColumn
        x: col(3.8)
        y: row(5.2)
        title: qsTr(" Valves ")
        model: [ vDGValvesStates.text ]
        textObjectName: "_DGValvesStatesData"
    }

    DebugDataColumn { id: _DGOperationModeColumn
        x: col(2.5)
        y: row(2)
        title: qsTr(" DG Mode ")
        model: [ vDGOperationMode.text ]
        textObjectName: "_DGOperationModeData"
    }

    // --- HD ---
    DebugDataColumn { id: _HDBloodFlowColumn
        x: col(0)
        y: row(2.8)
        title: qsTr(" BP ")
        model: [
            vTreatmentBloodFlow.bloodFlow_FlowSetPoint                 ,
            vTreatmentBloodFlow.bloodFlow_MeasuredFlow     .toFixed(2) ,
            vTreatmentBloodFlow.bloodFlow_RotorSpeed       .toFixed(2) ,
            vTreatmentBloodFlow.bloodFlow_MotorSpeed       .toFixed(2) ,
            vTreatmentBloodFlow.bloodFlow_MotorCtlSpeed    .toFixed(2) ,
            vTreatmentBloodFlow.bloodFlow_MotorCtlCurrent  .toFixed(2) ,
      "%" + vTreatmentBloodFlow.bloodFlow_PWMDutyCycle     .toFixed(2) ,
      "%" + vTreatmentBloodFlow.bloodFlow_SigStrength      .toFixed(2)
        ]
        label: [
            "Tgt.Flow",
            "Msr.Flow",
            "Rot.Spd",
            "Mot.Spd",
            "MC.Spd",
            "MC.Cur",
            "PWM",
            "SigS",
        ]
        textObjectName: "_HDBloodFlowData"
    }

    DebugDataColumn { id: _HDDialysateInletFlowColumn
        x: col(1.2)
        y: row(2.8)
        title: qsTr(" DPi ")
        model: [
            vTreatmentDialysateFlow.dialysateFlow_FlowSetPoint   .toFixed(2) ,
            vTreatmentDialysateFlow.dialysateFlow_MeasuredFlow   .toFixed(2) ,
            vTreatmentDialysateFlow.dialysateFlow_RotorSpeed     .toFixed(2) ,
            vTreatmentDialysateFlow.dialysateFlow_MotorSpeed     .toFixed(2) ,
            vTreatmentDialysateFlow.dialysateFlow_MotorCtlSpeed  .toFixed(2) ,
            vTreatmentDialysateFlow.dialysateFlow_MotorCtlCurrent.toFixed(2) ,
      "%" + vTreatmentDialysateFlow.dialysateFlow_PWMDutyCycle   .toFixed(2) ,
      "%" + vTreatmentDialysateFlow.dialysateFlow_SigStrength    .toFixed(2)
        ]
        label: [
            "Tgt.Flow",
            "Msr.Flow",
            "Rot.Spd",
            "Mot.Spd",
            "MC.Spd",
            "MC.Cur",
            "PWM",
            "SigS",
        ]
        textObjectName: "_HDInletFlowData"
    }

    DebugDataColumn { id: _HDDialysateOutletFlowColumn
        x: col(2.4)
        y: row(2.8)
        title: qsTr(" UF/DPo ")
        model: [
            vTreatmentUltrafiltration.ultrafiltration_RefUFVol       .toFixed(3) ,
            vTreatmentUltrafiltration.ultrafiltration_MeasUFVol      .toFixed(3) ,
            vTreatmentUltrafiltration.ultrafiltration_RotorSpeed     .toFixed(2) ,
            vTreatmentUltrafiltration.ultrafiltration_MotorSpeed     .toFixed(2) ,
            vTreatmentUltrafiltration.ultrafiltration_MotorCtlSpeed  .toFixed(2) ,
            vTreatmentUltrafiltration.ultrafiltration_MotorCtlCurrent.toFixed(2) ,
      "%" + vTreatmentUltrafiltration.ultrafiltration_PWMDtCycle     .toFixed(2)
        ]
        label: [
            "UFRef",
            "UFMeas",
            "Rot.Spd",
            "Mot.Spd",
            "MC.Spd",
            "MC.Cur",
            "PWM",
        ]
        textObjectName: "_HDOutletFlowData"
    }

    DebugDataColumn { id: _HDPressureOcclusionColumn
        x: col(3.6)
        y: row(2.8)
        title: qsTr(" Prsr Oc ")
        model: [
            vTreatmentPressureOcclusion.arterialPressure             .toFixed(2) ,
            vTreatmentPressureOcclusion.venousPressure               .toFixed(2) ,
            vTreatmentPressureOcclusion.bloodPumpOcclusion                       ,
            vTreatmentPressureOcclusion.dialysateInletPumpOcclusion              ,
            vTreatmentPressureOcclusion.dialysateOutletPumpOcclusion
        ]
        label: [
            "PBA" ,
            "PBo" ,
            "OB" ,
            "ODi" ,
            "ODo" ,
        ]

        textObjectName: "_PressureOcclusionData"
    }

    DebugDataColumn { id: _HDSyringePumpColumn
        x: col(4.6)
        y: row(2)
        title: qsTr(" Syringe ")
        model: [
            vHDSyringePump.syringePumpState                 ,
            vHDSyringePump.heparinState                     ,
            vHDSyringePump.setRate              .toFixed(2) ,
            vHDSyringePump.measuredRate         .toFixed(2) ,
            vHDSyringePump.syringePumpPosition              ,
            vHDSyringePump.volumeDelivered      .toFixed(2) ,
            vHDSyringePump.measuredHome         .toFixed(2) ,
            vHDSyringePump.measuredSwitch       .toFixed(2) ,
            vHDSyringePump.measuredForce        .toFixed(2) ,
            vHDSyringePump.safetyVol            .toFixed(2) ,
            vHDSyringePump.status                           ,
        ]
        label: [
            "srng.St",
            "hprn.St",
            "setRate",
            "msrRate",
            "srngPos",
            "VolumeD",
            "Msr.Hom",
            "Msr.Swt",
            "Msr.Frc",
            "safe.Vl",
            "status",
        ]

        textObjectName: "_HDSyringePumpColumn"
    }

    DebugDataColumn { id: _HDOperationModeColumn
        x: col(0.5)
        y: row(2)
        title: qsTr(" HD Mode ")
        model: [ vHDOperationMode.text ]
        textObjectName: "_HDOperationModeData"
    }

    Text { id : _canbusFaultCountText
        x: col(0)
        y: row(6)
        property int count: 0
        visible: true
        text: qsTr("CAN Faults: %1").arg(count)
        horizontalAlignment: Text.AlignLeft
        color: Colors.textMain
        font.pixelSize: 38
    }

    DebugDataColumn { id: _HDAccelerometerColumn
        x: col(7)
        y: row(3.2)
        title: qsTr(" HD Acc ")
        model: [
            vHDAccelerometer.x     .toFixed(3) ,
            vHDAccelerometer.y     .toFixed(3) ,
            vHDAccelerometer.z     .toFixed(3) ,
            vHDAccelerometer.xMax  .toFixed(3) ,
            vHDAccelerometer.yMax  .toFixed(3) ,
            vHDAccelerometer.zMax  .toFixed(3) ,
            vHDAccelerometer.xTilt .toFixed(3) ,
            vHDAccelerometer.yTilt .toFixed(3) ,
            vHDAccelerometer.zTilt .toFixed(3)
        ]
        label: [
            "x"     ,
            "y"     ,
            "z"     ,
            "xMax"  ,
            "yMax"  ,
            "zMax"  ,
            "x°"    ,
            "y°"    ,
            "z°"    ,
        ]
        textObjectName: "_HDAccelerometerData"
    }

    DebugDataColumn { id: _DGAccelerometerColumn
        x: col(7)
        y: row(0)
        title: qsTr(" DG Acc ")
        model: [
            vDGAccelerometer.x     .toFixed(3) ,
            vDGAccelerometer.y     .toFixed(3) ,
            vDGAccelerometer.z     .toFixed(3) ,
            vDGAccelerometer.xMax  .toFixed(3) ,
            vDGAccelerometer.yMax  .toFixed(3) ,
            vDGAccelerometer.zMax  .toFixed(3) ,
            vDGAccelerometer.xTilt .toFixed(3) ,
            vDGAccelerometer.yTilt .toFixed(3) ,
            vDGAccelerometer.zTilt .toFixed(3)
        ]
        label: [
            "x"     ,
            "y"     ,
            "z"     ,
            "xMax"  ,
            "yMax"  ,
            "zMax"  ,
            "x°"    ,
            "y°"    ,
            "z°"    ,
        ]
        textObjectName: "_DGAccelerometerData"
    }

    TouchRect {
        x           : col(6)
        y           : row(5.8)
        width       : 150
        height      : 35
        text.text   : "Get Version"
        onClicked   : vAdjustmentVersions.doAdjustment()
    }

    DebugDataColumn { id: _HDVersionsColumn
        x: col(6)
        y: row(6)
        title: ""
        model: [ "HD: v%1.%2.%3-%4 %5 v%6.%7 %8"
            .arg(vAdjustmentVersions.hdVerMajor)
            .arg(vAdjustmentVersions.hdVerMinor)
            .arg(vAdjustmentVersions.hdVerMicro)
            .arg(vAdjustmentVersions.hdVerBuild)
            .arg(vAdjustmentVersions.hdVerFPGAId)
            .arg(vAdjustmentVersions.hdVerFPGAMajor)
            .arg(vAdjustmentVersions.hdVerFPGAMinor)
            .arg(vAdjustmentVersions.hdVerFPGALab)
        ]
        textObjectName: "_HDVersions"
    }
    DebugDataColumn { id: _DGVersionsColumn
        x: col(6)
        y: row(6.5)
        title: ""
        model: [ "DG: v%1.%2.%3-%4 %5 v%6.%7 %8"
            .arg(vAdjustmentVersions.dgVerMajor)
            .arg(vAdjustmentVersions.dgVerMinor)
            .arg(vAdjustmentVersions.dgVerMicro)
            .arg(vAdjustmentVersions.dgVerBuild)
            .arg(vAdjustmentVersions.dgVerFPGAId)
            .arg(vAdjustmentVersions.dgVerFPGAMajor)
            .arg(vAdjustmentVersions.dgVerFPGAMinor)
            .arg(vAdjustmentVersions.dgVerFPGALab)
        ]
        textObjectName: "_DGVersions"
    }


    Text { id : _mouseEventCountText
        x: col(2)
        y: row(6)
        visible: true
        text: String("Touch Count: %1,%2")
        .arg(GuiEventSpy.touchCount   /*.toString().padStart(4,'0')*/ )
        .arg(GuiEventSpy.touchPoints  /*.toString().padStart(4,'0')*/ )
        horizontalAlignment: Text.AlignLeft
        color: Colors.textMain
        font.pixelSize: 38
        MouseArea {
            anchors.fill: _mouseEventCountText
            onClicked: GuiEventSpy.doTouchReset()
        }
    }

    DebugDataColumn { id: _PreTreatmentStates
        x: col(1)
        y: row(6.5)
        title: qsTr(" PreTx States ")
        model: [ "%1, %2, %3, %4, %5, %6, %7, %8"
            .arg(vPreTreatmentStates.subMode                 )
            .arg(vPreTreatmentStates.waterSampleState        )
            .arg(vPreTreatmentStates.selfTestNoCartridgeState)
            .arg(vPreTreatmentStates.installationState       )
            .arg(vPreTreatmentStates.selfTestDryState        )
            .arg(vPreTreatmentStates.primeState              )
            .arg(vPreTreatmentStates.recirculateState        )
            .arg(vPreTreatmentStates.patientConnectionState  )
        ]
        textObjectName: "_PreTreatmentStates"
    }

    Connections { target: _GuiView
        onDidActionReceive: {
            if (vAction === GuiActions.ID_CANBusFaultCount ) {
                _canbusFaultCountText.count = vData[0]
            }
        }
    }
}
