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

    title: qsTr("Filtration & Purification")

    DebugDataColumn { id: _FPPressureColumn
            textObjectName  :"_FPPressureData"
            title           : qsTr(" Pressure ")
            x: col(0)
            y: row(0)
            model: [
                vFPPressures.beforePresureRegulator      .toFixed(2) ,
                vFPPressures.afterPressureRegulator      .toFixed(2) ,
                vFPPressures.beforeInletConductivity     .toFixed(2) ,
                vFPPressures.beforeROFilter              .toFixed(2) ,
                vFPPressures.afterROFilter               .toFixed(2) ,
                vFPPressures.beforeROPump                .toFixed(2) ,
                vFPPressures.ROConcentrate               .toFixed(2) ,
                vFPPressures.ROConcentrateDrop           .toFixed(2) ,
                vFPPressures.drainDrop                   .toFixed(2) ,
            ]
            label: [
                "M1 bRegPSI         ",
                "M3 aRegPSI         ",
                "P8 bInCond         ",
                "P13 bROFilter      ",
                "P17 aROFilter      ",
                "X1 bROPump         ",
                "X2 ROConc          ",
                "X3 dROConc         ",
                "X4 dDrain          ",
            ]
        }

    DebugDataColumn { id: _FPValvesColumn
            textObjectName  :"_FPValvesData"
            title           : qsTr(" Valves ")
            x: col(1.5)
            y: row(0)
            model: [
                Number(vFPValvesStates. M4Valve )    ,
                Number(vFPValvesStates. P39Valve)    ,
                Number(vFPValvesStates. P6Valve )    ,
                Number(vFPValvesStates. P11Valve)    ,
                Number(vFPValvesStates. P33Valve)    ,
                Number(vFPValvesStates. P34Valve)    ,
                Number(vFPValvesStates. P37Valve)    ,
                Number(vFPValvesStates. M7Valve )    ,
                Number(vFPValvesStates. P20Valve)    ,
                Number(vFPValvesStates. P43Valve)    ,
            ]
            label: [
                "M4     ",
                "P39    ",
                "P6     ",
                "P11    ",
                "P33    ",
                "P34    ",
                "P37    ",
                "M7     ",
                "P20    ",
                "P43    ",
            ]
        }

    DebugDataColumn { id: _FPROPumpColumn
            textObjectName  :"_FPROPumpData"
            title           : qsTr(" RO Pump ")
            x: col(3)
            y: row(0)
            model: [
                vFPROPump.pumpState                         ,
                vFPROPump.pumpDutyCycle                     ,
                vFPROPump.pumpFBDutyCycle                   ,
                vFPROPump.pumpSpeed             .toFixed(2) ,
                vFPROPump.targetPressure        .toFixed(2) ,
                vFPROPump.targetFlow            .toFixed(2) ,
                vFPROPump.targetDutyCycle       .toFixed(2) ,
                vFPROPump.pumpDutyCyclePct      .toFixed(2) ,
                vFPROPump.pumpFBDutyCyclePct    .toFixed(2) ,
            ]
            label: [
                "P12 State      ",
                "P12 DC         ",
                "P12 FBDC       ",
                "P12 Spd        ",
                "P12 tPSI       ",
                "P12 tFlow      ",
                "P12 tDC        ",
                "P12 DCPct      ",
                "P12 FBDCPct    ",
            ]
        }

    DebugDataColumn { id: _FPBoostPumpColumn
            textObjectName  :"_FPBoostPumpData"
            title           : qsTr(" Boost Pump ")
            x: col(4.5)
            y: row(0)
            model: [
                vFPBoostPump.pumpState                         ,
                vFPBoostPump.pumpDutyCycle                     ,
                vFPBoostPump.pumpFBDutyCycle                   ,
                vFPBoostPump.pumpSpeed             .toFixed(2) ,
                vFPBoostPump.targetPressure        .toFixed(2) ,
                vFPBoostPump.targetFlow            .toFixed(2) ,
                vFPBoostPump.targetDutyCycle       .toFixed(2) ,
                vFPBoostPump.pumpDutyCyclePct      .toFixed(2) ,
                vFPBoostPump.pumpFBDutyCyclePct    .toFixed(2) ,
            ]
            label: [
                "P40 State      ",
                "P40 DC         ",
                "P40 FBDC       ",
                "P40 Spd        ",
                "P40 tPSI       ",
                "P40 tFlow      ",
                "P40 tDC        ",
                "P40 DCPct      ",
                "P40 FBDCPct    ",
            ]
        }

    DebugDataColumn { id: _FPTemperatureColumn
            textObjectName  :"_FPTemperatureData"
            title           : qsTr(" Temperature ")
            x: col(6)
            y: row(0)
            model: [
                vFPTemperature.inletConductivityTemp        .toFixed(2) ,
                vFPTemperature.outletConductivityTemp       .toFixed(2) ,
                vFPTemperature.beforePressureRegulatorTemp  .toFixed(2) ,
                vFPTemperature.afterPressureRegulatorTemp   .toFixed(2) ,
                vFPTemperature.beforeInletConductivityTemp  .toFixed(2) ,
                vFPTemperature.beforeROFilterTemp           .toFixed(2) ,
                vFPTemperature.afterROFilterTemp            .toFixed(2) ,
                vFPTemperature.beforeROPumpTemp             .toFixed(2) ,
                vFPTemperature.ROConcentrateTemp            .toFixed(2) ,
                vFPTemperature.ROConcentratePumpTemp        .toFixed(2) ,
                vFPTemperature.drainDropTemp                .toFixed(2) ,
                vFPTemperature.ROInletTemp                  .toFixed(2) ,
                vFPTemperature.ROOutletTemp                 .toFixed(2) ,
            ]
            label: [
                "P10 inCond     ",
                "P19 outCond    ",
                "M1 bReg        ",
                "M3 aReg        ",
                "P8 bInCond     ",
                "P13 bRO        ",
                "P17 aRO        ",
                "X1 bROPump     ",
                "X2 ROConc      ",
                "X3 ROConcPump  ",
                "X4 Drain       ",
                "P7 ROIn        ",
                "P16 ROOut      ",
            ]
        }

    DebugDataColumn { id: _FPFlowColumn
            textObjectName  :"_FPFlowData"
            title           : qsTr(" Flow ")
            x: col(7.5)
            y: row(0)
            model: [
                vFPFlow.waterInletFlow       .toFixed(2) ,
                vFPFlow.waterInletTemp       .toFixed(2) ,
                vFPFlow.rOFilterOutletFlow   .toFixed(2) ,
                vFPFlow.rOFilterOutletTemp   .toFixed(2) ,
            ]
            label: [
                "P7 FlowIn     ",
                "P7 TempIn     ",
                "P16 FlowOut   ",
                "P16 TempOut   ",
            ]
        }

    DebugDataColumn { id: _FPConductivityColumn
            textObjectName  :"_FPConductivityData"
            title           : qsTr(" Conductivity ")
            x: col(9)
            y: row(0)
            model: [
                vFPConductivity.P9Conductivity           .toFixed(2) ,
                vFPConductivity.P18Conductivity          .toFixed(2) ,
            ]
            label: [
                "P9     ",
                "P18    ",
            ]
        }

    DebugDataColumn { id: _FPLevelColumn
            textObjectName  :"_FPLevelData"
            title           : qsTr(" Level ")
            x: col(9)
            y: row(1)
            model: [
                vFPLevel.P25FloaterLevel                ,
            ]
            label: [
                "P25 Floater    ",
            ]
        }

    // 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: _FPVersionsColumn
                textObjectName  :"_FPVersionsData"
                title           : ""
                horizontalAlignmentText: Text.AlignLeft
                fontPixelSizeText: Fonts.fontPixelDebugTitle
                model: [ "FP: v%1.%2.%3-%4 v%5.%6.%7-%8 c%9"
                    .arg(vAdjustmentVersions.fpVerMajor             )
                    .arg(vAdjustmentVersions.fpVerMinor             )
                    .arg(vAdjustmentVersions.fpVerMicro             )
                    .arg(vAdjustmentVersions.fpVerBuild             )
                    .arg(vAdjustmentVersions.fpVerFPGAMajor         )
                    .arg(vAdjustmentVersions.fpVerFPGAMinor         )
                    .arg(vAdjustmentVersions.fpVerFPGALab           )
                    .arg(vAdjustmentVersions.fpVerFPGAId            )
                    .arg(vAdjustmentVersions.fpVerCompatibilityRev  )
                ]
            }
        }
    }


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