import QtQuick 2.12
import QtQuick.Layouts 1.12

import Controls 1.0 as Controls
import Cubo 1.0
import Utils 1.0

Item {
    ColumnLayout {
        spacing: 15
        anchors.fill: parent

        Controls.Frame {
            padding: 35
            implicitWidth: parent.width
            Layout.fillWidth: true

            RowLayout {
                spacing: 90
                anchors.fill: parent

                Controls.TitleLabel {
                    text: qsTr("Serial number")
                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.spacing) / 2
                }

                Controls.TextField {
                    id: serialNumberField
                    enabled: false
                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.spacing) / 2
                }
            }
        }

        Controls.Frame {
            padding: 35
            implicitWidth: parent.width
            Layout.fillWidth: true

            GridLayout {
                columns: 2
                columnSpacing: 90
                rowSpacing: 20
                anchors.fill: parent

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Battery charge level, V")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: batteryChargeLevelLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Number of modules")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: numberOfModulesLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Battery voltage, V")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: batteryVoltageLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Number of cells")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: numberOfCellsLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Nominal capacity, A/h")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: nominalCapacityLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Actual capacity, A/h")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: actualCapacityLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Battery temperature, °C")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: batteryTemperatureLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("BMS temperature, °C")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: bmsTemperatureLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }
            }
        }

        Controls.Frame {
            padding: 35
            implicitWidth: parent.width
            Layout.fillWidth: true

            GridLayout {
                columns: 2
                columnSpacing: 90
                rowSpacing: 20
                anchors.fill: parent

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Maximum cell voltage, V")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: maximumCellVoltageLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Minimum cell voltage, V")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: minimumCellVoltageLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }

                RowLayout {
                    spacing: 10
                    Controls.ContentLabel {
                        text: qsTr("Current")
                        Layout.fillWidth: true
                        Layout.maximumWidth: implicitWidth
                    }

                    Controls.DotSeparator {
                        Layout.fillWidth: true
                    }

                    Controls.SubtitleLabel {
                        id: currentLabel
                        text: "-"
                    }

                    Layout.fillWidth: true
                    Layout.maximumWidth: (parent.width - parent.columnSpacing) / 2
                }
            }
        }

        Item {
            Layout.fillHeight: true
        }
    }

    Connections {
        target: BmsInterface.commands()
        onValuesReceived: {
            batteryChargeLevelLabel.text = values.soC
            batteryVoltageLabel.text = MathHelper.roundDouble(values.packVoltage)

            batteryTemperatureLabel.text = MathHelper.roundDouble(values.tempBattHigh)
            bmsTemperatureLabel.text = MathHelper.roundDouble(values.tempBMSHigh)

            maximumCellVoltageLabel.text = MathHelper.roundDouble(values.cVHigh)
            minimumCellVoltageLabel.text = MathHelper.roundDouble(values.cVLow)

            currentLabel.text = MathHelper.roundDouble(values.packCurrent)
        }
    }

    Connections {
        target: BmsInterface.bmsConfig()
        onUpdated: {
            serialNumberField.text = MathHelper.roundDouble(BmsInterface.bmsConfig().getParamDouble("notUsedCurrentThreshold")) // TODO

            numberOfModulesLabel.text = BmsInterface.bmsConfig().getParamInt("cellMonitorICCount")
            numberOfCellsLabel.text = BmsInterface.bmsConfig().getParamInt("noOfCellsSeries")

            nominalCapacityLabel.text = MathHelper.roundDouble(BmsInterface.bmsConfig().getParamDouble("batteryCapacity"))
            actualCapacityLabel.text = nominalCapacityLabel.text // TODO
        }
    }

    Connections {
        target: BmsInterface
        onPortConnectedChanged: getValues()
    }

    onVisibleChanged: getValues()

    function getValues() {
        if (BmsInterface.isPortConnected() && visible) {
            BmsInterface.commands().getValues()
            BmsInterface.commands().getBMSconf()
        }
    }
}
