/*!
 *
 * Copyright (c) 2019-2020 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 ManagerHome.qml
 * \author (last) Behrouz NematiPour
 * \date (last) 22-Mar-2020
 * \author (original) Behrouz NematiPour
 * \date (original) 21-Oct-2019
 *
 */

// 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   ManagerHome is the screen
 * which is the default screen in the "Manager" stack
 */
ScreenItem { id: _root

    function col(idx, gap = 10) {
        return idx * 150 + gap
    }

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

    DebugDataColumn { id: _DGROPumpColumn
        x: col(0)
        y: row(0)
        title: qsTr(" ROPmp ")
        model: [
            vDGROPump.pressure             ,
            vDGROPump.flowRate .toFixed(2) ,
            vDGROPump.pwm      .toFixed(2)
        ]
        textObjectName: "_DGROPumpData"
    }

    DebugDataColumn { id: _DGDrainPumpColumn
        x: col(1)
        y: row(0)
        title: qsTr(" DRPmp ")
        model: [
            vDGDrainPump.rpm ,
            vDGDrainPump.dac ,
        ]
        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)
        ]
        textObjectName: "_DGPressuresData"
    }

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

    DebugDataColumn { id: _DGHeatersColumn
        x: col(4)
        y: row(0)
        title: qsTr(" Heatrs ")
        model: [
            vDGHeaters.mainPrimaryDC  ,
            vDGHeaters.smallPrimaryDC ,
            vDGHeaters.trimmerDC      ,
        ]
        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)
        ]
        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)
        ]
        textObjectName: "_DGTemperaturesData"
    }

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

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

    // --- HD ---
    DebugDataColumn { id: _HDBloodFlowColumn
        x: col(0)
        y: row(3)
        title: qsTr(" Blood ")
        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)
        ]
        textObjectName: "_HDBloodFlowData"
    }

    DebugDataColumn { id: _HDDialysateInletFlowColumn
        x: col(1)
        y: row(3)
        title: qsTr("  Dlyst I ")
        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)
        ]
        textObjectName: "_HDInletFlowData"
    }

    DebugDataColumn { id: _HDDialysateOutletFlowColumn
        x: col(2)
        y: row(3)
        title: qsTr("  Dlyst O ")
        model: [
                  vTreatmentUltrafiltration.ultrafiltration_RefUFVol       .toFixed(2) ,
                  vTreatmentUltrafiltration.ultrafiltration_MeasUFVol      .toFixed(2) ,
                  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)
        ]
        textObjectName: "_HDOutletFlowData"
    }

    DebugDataColumn { id: _HDPressureOcclusionColumn
        x: col(3)
        y: row(3)
        title: qsTr(" Prsr Oc ")
        model: [
            vTreatmentPressureOcclusion.pressureocclusion_ArterialPressure             .toFixed(2) ,
            vTreatmentPressureOcclusion.pressureocclusion_VenousPressure               .toFixed(2) ,
            vTreatmentPressureOcclusion.pressureocclusion_BloodPumpOcclusion           .toFixed(2) ,
            vTreatmentPressureOcclusion.pressureocclusion_DialysateInletPumpOcclusion  .toFixed(2) ,
            vTreatmentPressureOcclusion.pressureocclusion_DialysateOutletPumpOcclusion .toFixed(2)
        ]
        textObjectName: "_PressureOcclusionData"
    }

    DebugDataColumn { id: _HDOperationModeColumn
        x: col(3)
        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
    }

    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()
        }
    }

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