import QtQml 2.12
import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12 as QuickControls

import Controls 1.0
import Utils 1.0
import Vku 1.0

QuickControls.ApplicationWindow {
    id: mainWindow
    width: 1024
    height: 768
    visible: true
    title: qsTr("Vku Monitor")

    ColumnLayout {
        anchors.fill: parent
        anchors.topMargin: Ui.scale(32)
        anchors.bottomMargin: Ui.scale(32)
        anchors.leftMargin: Ui.scale(50)
        anchors.rightMargin: Ui.scale(50)

        spacing: Ui.scale(12)

        RowLayout {
            id: topLayout
            spacing: Ui.scale(40)
            Layout.fillWidth: true

            ColumnLayout {
                spacing: Ui.scale(12)

                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.preferredWidth: 100

                PrimaryLabel {
                    text: qsTr("Current state")
                }

                GridLayout {
                    columns: 2
                    columnSpacing: Ui.scale(28)
                    rowSpacing: Ui.scale(8)

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    Indicator {
                        label: qsTr("Input voltage")
                        indicator: qsTr("V")
                        value.text: CanController.inputVoltage
                        value.readOnly: true
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Radiator temperature")
                        indicator: qsTr("°С")
                        value.text: CanController.radiatorTemperature
                        value.readOnly: true
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Output voltage")
                        indicator: qsTr("V")
                        value.text: CanController.outputVoltage
                        value.readOnly: true
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Emergency counter")
                        indicator: ""
                        value.text: CanController.emergencyCounter
                        value.readOnly: true
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Input current")
                        indicator: qsTr("A")
                        value.text: CanController.inputCurrent
                        value.readOnly: true
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }
                }

                PrimaryButton {
                    text: CanController.isVkuClosed ? qsTr("VKU opening") : qsTr("VKU closing")
                    Layout.fillWidth: true
                    Layout.topMargin: Ui.scale(12)
                    onClicked: CanController.switchVkuClosure()
                }
            }

            Separator {
                id: separator
                orientation: Qt.Vertical
                Layout.fillHeight: true
            }

            ColumnLayout {
                spacing: Ui.scale(12)

                Layout.fillWidth: true
                Layout.fillHeight: true
                Layout.preferredWidth: 100

                PrimaryLabel {
                    text: qsTr("Parameters control")
                    Layout.fillWidth: true
                }

                GridLayout {
                    columns: 2
                    columnSpacing: Ui.scale(28)
                    rowSpacing: Ui.scale(8)

                    Layout.fillWidth: true
                    Layout.fillHeight: true

                    Indicator {
                        label: qsTr("Maximum current")
                        indicator: qsTr("A")
                        value.text: CanController.maximumCurrent
                        value.onEditingFinished: CanController.maximumCurrent = value.text
                        value.validator: IntValidator { bottom: 0; top: 255 }
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Duration of delay after emergency")
                        indicator: qsTr("s")
                        value.text: CanController.emergencyDelay
                        value.onEditingFinished: CanController.emergencyDelay = value.text
                        value.validator: DoubleValidator { bottom: 2; top: 15; decimals: 1; notation: DoubleValidator.StandardNotation }
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Breaking delay")
                        indicator: qsTr("ms")
                        value.text: CanController.breakingDelay
                        value.onEditingFinished: CanController.breakingDelay = value.text
                        value.validator: IntValidator { bottom: 0; top: 255 }
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Number of retries after emergency breaking")
                        indicator: ""
                        value.text: CanController.retriesAfterEmergencyBreak
                        value.onEditingFinished: CanController.retriesAfterEmergencyBreak = value.text
                        value.validator: IntValidator { bottom: 1; top: 7 }
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }

                    Indicator {
                        label: qsTr("Breaking current")
                        indicator: qsTr("A")
                        value.text: CanController.breakingCurrent
                        value.onEditingFinished: CanController.breakingCurrent = value.text
                        value.validator: IntValidator { bottom: 0; top: 255 }
                        Layout.fillWidth: true
                        Layout.preferredWidth: 100
                    }
                }

                SecondaryButton {
                    text: qsTr("Emergency reset")
                    Layout.fillWidth: true
                    Layout.topMargin: Ui.scale(12)
                    onClicked: CanController.emergencyReset()
                }
            }
        }

        PrimaryLabel {
            text: qsTr("Status")
            visible: CanController.statuses.length > 0
            Layout.topMargin: Ui.scale(20)
        }

        ListView {
            clip: true
            model: CanController.statuses

            // ????
            implicitHeight: mainWindow.height

            Layout.fillWidth: true
            Layout.fillHeight: true

            delegate: GridLayout {
                width: parent.width
                columns: 4

                columnSpacing: Ui.scale(20)
                rowSpacing: Ui.scale(8)

                Separator {
                    bottomPadding: Ui.scale(8)
                    orientation: Qt.Horizontal
                    Layout.fillWidth: true
                    Layout.columnSpan: 4
                }

                ColoredLabel {
                    text: modelData.time
                    backgroundColor: Palette.timeSectionBackgroundColor
                }

                PrimaryLabel {
                    text: modelData.status
                }

                ColoredLabel {
                    text: modelData.description
                    backgroundColor: Palette.statusSectionBackgroundColor
                }

                Item {
                    Layout.fillWidth: true
                }
            }

            QuickControls.ScrollBar.vertical: QuickControls.ScrollBar {
                policy: size > 0 && size < 1 ? QuickControls.ScrollBar.AlwaysOn : QuickControls.ScrollBar.AsNeeded
            }
        }
    }

    background: Rectangle {
        color: Palette.backgroundColor
    }

    Component.onCompleted: {
        Ui.currentWidth = Qt.binding(function(){ return width })
        Ui.currentHeight = Qt.binding(function(){ return height })
    }
}
