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

    contentItem : Item { id: _content

        DebugDataColumn { id: _FPPressureColumn
            textObjectName  :"_FPPressureData"
            title           : qsTr("[0x34] Pressure ")
            anchors {
                top     : parent.top
                left    : parent.left
            }

            model: [
                vFPPressures.m3Pressure    .toFixed(2) ,
                vFPPressures.p8Pressure    .toFixed(2) ,
                vFPPressures.p13Pressure   .toFixed(2) ,
                vFPPressures.p17Pressure   .toFixed(2) ,
                vFPPressures.p46Pressure   .toFixed(2) ,
            ]
            label: [
                "M3     ",
                "P8     ",
                "P13    ",
                "P17    ",
                "P46    ",
            ]
        }

        DebugDataColumn { id: _FPROPumpColumn
            textObjectName  :"_FPROPumpData"
            title           : qsTr("[0x32] RO Pump ")
            anchors {
                top         : parent.top
                left        : _FPPressureColumn.right
                leftMargin  : spacing
            }
            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: _FPRoRejectionPercentageColumn
            textObjectName  :"_FPRoRejectionPercentageColumn"
            title           : qsTr("[0x71] RO Rejection Ratio ")
            anchors {
                top         : _FPROPumpColumn.bottom
                topMargin   : spacing
                left        : _FPROPumpColumn.left
            }
            model: [
                vFPRORejectionPercentage.rawRORejectionRatio              .toFixed(1) ,
                vFPRORejectionPercentage.rawRORejectionRatioTankFill      .toFixed(1) ,
                vFPRORejectionPercentage.avgRORejectionRatio              .toFixed(1) ,
                vFPRORejectionPercentage.avgRORejectionRatioTankFill      .toFixed(1) ,
                vFPRORejectionPercentage.genPermeateState                             ,
            ]
            label: [
                "raw                ",
                "raw Tank Fill      ",
                "avg                ",
                "avg Tank Fill      ",
                "genPermeateState   ",
            ]
        }

        DebugDataColumn { id: _FPBoostPumpColumn
            textObjectName  :"_FPBoostPumpData"
            title           : qsTr("[0x50] Boost Pump ")
            anchors {
                top         : parent.top
                left        : _FPROPumpColumn.right
                leftMargin  : spacing
            }
            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("[0x39] Temperature ")
            anchors {
                top         : parent.top
                left        : _FPBoostPumpColumn.right
                leftMargin  : spacing
            }
            model: [
                vFPTemperature.m3Temp           .toFixed(2) ,
                vFPTemperature.p8Temp           .toFixed(2) ,
                vFPTemperature.p13Temp          .toFixed(2) ,
                vFPTemperature.p17Temp          .toFixed(2) ,
                vFPTemperature.p46Temp          .toFixed(2) ,
                vFPTemperature.p10Temp          .toFixed(2) ,
                vFPTemperature.p19Temp          .toFixed(2) ,
                vFPTemperature.p7Temp           .toFixed(2) ,
                vFPTemperature.p16Temp          .toFixed(2) ,
                vFPTemperature.p7InternalTemp   .toFixed(2) ,
                vFPTemperature.p16InternalTemp  .toFixed(2) ,
            ]
            label: [
                "M3Temp            ",
                "P8Temp            ",
                "P13Temp           ",
                "P17Temp           ",
                "P46Temp           ",
                "P10Temp           ",
                "P19Temp           ",
                "P7Temp            ",
                "P16Temp           ",
                "P7InternalTemp    ",
                "P16InternalTemp   ",
            ]
        }

        DebugDataColumn { id: _FPFlowColumn
            textObjectName  :"_FPFlowData"
            title           : qsTr("[0x36] Flow ")
            anchors {
                top         : parent.top
                left        : _FPTemperatureColumn.right
                leftMargin  : spacing
            }
            model: [
                vFPFlow.p7Flow         .toFixed(2) ,
                vFPFlow.p16Flow        .toFixed(2) ,
            ]
            label: [
                "P7 FlowIn     ",
                "P16 FlowOut   ",
            ]
        }

        DebugDataColumn { id: _FPLevelColumn
            textObjectName  :"_FPLevelData"
            title           : qsTr("[0x35] Level ")
            anchors {
                top         : parent.top
                left        : _FPFlowColumn.right
                leftMargin  : spacing
            }
            model: [
                vFPLevel.P25FloaterLevel                ,
            ]
            label: [
                "P25 Floater    ",
            ]
        }

        DebugDataColumn { id: _FPConductivityColumn
            textObjectName  :"_FPConductivityData"
            title           : qsTr("[0x37] Conductivity ")
            anchors {
                top         : _FPPressureColumn.bottom
                topMargin   : spacing
                left        : _FPPressureColumn.left
            }
            model: [
                vFPConductivity.P9Conductivity           .toFixed(2) ,
                vFPConductivity.P18Conductivity          .toFixed(2) ,
            ]
            label: [
                "P9     ",
                "P18    ",
            ]
        }

        // TODO: The TouchRect should be unnecessary since the first communication message with UI/TD is the version.
        TouchRect {
            width       : _FPVersionsColumn.width
            height      : _FPVersionsColumn.height
            onClicked   : vAdjustmentVersions.doAdjustment()
            radius      : 5
            anchors {
                bottom          : parent.bottom
                right           : parent.right
            }

            DebugDataColumn { id: _FPVersionsColumn
                textObjectName  :"_FPVersionsData"
                title           : ""
                horizontalAlignmentText : Text.AlignLeft
                fontPixelSizeText       : Fonts.fontPixelDebugTitle
                isTouchRect             : true
                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]
            }
        }
    }
}
