/*!
 *
 * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved.
 * \copyright                                                       \n
 *          THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM,  \n
 *          IN PART OR IN WHOLE,                                    \n
 *          WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. \n
 *
 * \file    SettingsHome.qml
 * \date    2019/10/21
 * \author  Behrouz NematiPour
 *
 */

// Qt
import QtQuick 2.12

// Project
import Gui.Actions 0.1;

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

/*!
 * \brief   SettingsHome is the screen
 * which is the default screen in the "Settings" stack
 */
ScreenItem { id: _root

    USBButton { id: _usbButton
        anchors {
            top         : parent.top
            right       : _exportButton.left
            topMargin   : (Variables.headerHeight - Variables.logoHeight) / 2
            rightMargin : (Variables.headerHeight - Variables.logoHeight) / 2
        }
    }

    ExportButton { id: _exportButton
        width: 150
        height: 50
        anchors {
            top         : parent.top
            right       : _poweroffButton.left
            topMargin   : (Variables.headerHeight - Variables.logoHeight) / 2
            rightMargin : (Variables.headerHeight - Variables.logoHeight) / 2
        }
    }

    TouchRect { id : _poweroffButton
        width: 150
        height: Variables.logoHeight
        animated: true
        anchors {
            top         : parent.top
            right       : parent.right
            topMargin   : (Variables.headerHeight - Variables.logoHeight) / 2
            rightMargin : (Variables.headerHeight - Variables.logoHeight) / 2
        }
        text.text: qsTr("Shutdown")
        button.onPressed: {
            _GuiView.doActionTransmit(GuiActions.PowerOff, GuiActions.NoData)
        }
    }

    Text { id : _canbusFaultCountText
        property int count: 0
        visible: true
        anchors {
            top         : parent.top
            left        : parent.left
            topMargin   : (Variables.headerHeight - Variables.logoHeight) / 2
            leftMargin  : (Variables.headerHeight - Variables.logoHeight) / 2
        }
        text: qsTr("CAN Faults : %1").arg(count)
        horizontalAlignment: Text.AlignLeft
        color: Colors.textMain
        font.pixelSize: Fonts.fontPixelTitle
    }

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

    Column { id: _BloodFlowColumn
        width: 150
        spacing: 20
        topPadding: 100
        leftPadding: 10
        Text {
            id: _BloodFlow_Title
            text: qsTr(" Blood ")
            width: _BloodFlowColumn.width
            horizontalAlignment: Text.AlignRight
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            font.underline: true
        }
        Repeater {
            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)
            ]
            Text {
                text: modelData
                width: _BloodFlowColumn.width
                horizontalAlignment: Text.AlignRight
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTitle
            }
        }
    }

    Column { id: _DialysateInletFlowColumn
        width       : 150
        spacing     : 20
        topPadding  : 100
        leftPadding : 200
        Text {
            id: _DialysateInletFlow_Title
            text: qsTr("  Dlyst I ")
            width: _DialysateInletFlowColumn.width
            horizontalAlignment: Text.AlignRight
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            font.underline: true
        }
        Repeater {
            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)
            ]
            Text {
                text: modelData
                width: _DialysateInletFlowColumn.width
                horizontalAlignment: Text.AlignRight
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTitle
            }
        }
    }

    Column { id: _DialysateOutletFlowColumn
        width       : 150
        spacing     : 20
        topPadding  : 100
        leftPadding : 400
        Text {
            id: _DialysateOutletFlow_Title
            text: qsTr("  Dlyst O ")
            width: _DialysateOutletFlowColumn.width
            horizontalAlignment: Text.AlignRight
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            font.underline: true
        }
        Repeater {
            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)
            ]
            Text {
                text: modelData
                width: _DialysateOutletFlowColumn.width
                horizontalAlignment: Text.AlignRight
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTitle
            }
        }
    }

    Column { id: _PressureOcclusionColumn
        width       : 150
        spacing     : 20
        topPadding  : 100
        leftPadding : 600
        Text {
            id: _PressureOcclusion_Title
            text: qsTr(" Prsr Oc ")
            width: _PressureOcclusionColumn.width
            horizontalAlignment: Text.AlignRight
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            font.underline: true
        }
        Repeater {
            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)
            ]
            Text {
                text: modelData
                width: _PressureOcclusionColumn.width
                horizontalAlignment: Text.AlignRight
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTitle
            }
        }
    }

    Column { id: _LoadCellReadingsColumn
        width       : 150
        spacing     : 20
        topPadding  : 100
        leftPadding : 800
        Text {
            id: _LoadCellReading_Title
            text: qsTr(" LoadCl ")
            width: _LoadCellReadingsColumn.width
            horizontalAlignment: Text.AlignRight
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            font.underline: true
        }
        Repeater {
            model: [
                vLoadCellReadings.reservoir1Prim .toFixed(2) ,
                vLoadCellReadings.reservoir1Bkup .toFixed(2) ,
                vLoadCellReadings.reservoir2Prim .toFixed(2) ,
                vLoadCellReadings.reservoir2Bkup .toFixed(2)
            ]
            Text {
                objectName: "_LoadCellReadings" + index
                text: modelData
                width: _LoadCellReadingsColumn.width
                horizontalAlignment: Text.AlignRight
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTitle
            }
        }
    }

    Column { id: _TemperatureSensorsColumn
        width       :  150
        spacing     :   -5
        topPadding  :  100
        leftPadding : 1000
        Text {
            id: _TemperatureSensors_Title
            text: qsTr(" Temp ")
            width: _TemperatureSensorsColumn.width
            horizontalAlignment: Text.AlignRight
            color: Colors.textMain
            font.pixelSize: Fonts.fontPixelTitle
            font.underline: true
        }
        Repeater {
            model: [
                vTemperatureSensors.inletPrimaryHeater          .toFixed(2) ,
                vTemperatureSensors.outletPrimaryHeater         .toFixed(2) ,
                vTemperatureSensors.conductivitySensor1         .toFixed(2) ,
                vTemperatureSensors.conductivitySensor2         .toFixed(2) ,
                vTemperatureSensors.outletRedundancy            .toFixed(2) ,
                vTemperatureSensors.inletDialysate              .toFixed(2) ,
                vTemperatureSensors.primaryHeaterThermoCouple   .toFixed(2) ,
                vTemperatureSensors.trimmerHeaterThermoCouple   .toFixed(2) ,
                vTemperatureSensors.primaryHeaterColdJunction   .toFixed(2) ,
                vTemperatureSensors.trimmerHeaterColdJunction   .toFixed(2) ,
                vTemperatureSensors.primaryHeaterInternal       .toFixed(2) ,
                vTemperatureSensors.trimmerHeaterInternal       .toFixed(2)
            ]
            Text {
                objectName: "_TemperatureSensors" + index
                text: modelData
                width: _TemperatureSensorsColumn.width
                horizontalAlignment: Text.AlignRight
                color: Colors.textMain
                font.pixelSize: Fonts.fontPixelTitle
            }
        }
    }
}
