import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

import "."
import Core 1.0
import Controls 1.0 as CustomControls
import Palette 1.0

ApplicationWindow {
    width: 1280
    height: 1024
    visible: true
    visibility: Qt.platform.os === "linux" ? "FullScreen" : "Windowed"
    flags: Qt.platform.os === "linux" ? Qt.FramelessWindowHint | Qt.Window : Qt.Window
    title: qsTr("Stand Battery View")
    color: Palette.backgroundColor

    font.pixelSize: 14

    GridLayout {
        anchors.fill: parent
        anchors.margins: 10

        columns: 4
        columnSpacing: 10
        rowSpacing: 10

        Frame {
            Layout.fillWidth: true
            Layout.columnSpan: 4
            Layout.preferredHeight: statusBarLayout.implicitHeight + 20

            leftPadding: 20
            rightPadding: 20
            topPadding: 10
            bottomPadding: 10

            background: Rectangle {
                color: Palette.controlBackgroundColor
                radius: 14
            }

            RowLayout {
                id: statusBarLayout
                spacing: 10
                width: parent.width

                CustomControls.ColoredLabel {
                    text: CanController.isConnected ? "Штатный режим" : "Отсутствует связь с БМФ2"
                    backgroundColor: CanController.isConnected ? Palette.goodColor : Palette.errorColor
                }

                Item {
                    Layout.fillWidth: true
                }

                CustomControls.Label {
                    id: dateLabel
                }

                CustomControls.ColoredLabel {
                    id: timeLabel
                    backgroundColor: "#EAEAEA"
                    backgroundRadius: 8
                }

                Timer {
                    interval: 1000
                    repeat: true
                    running: true
                    triggeredOnStart: true

                    onTriggered: {
                        let date =  new Date()
                        dateLabel.text = Qt.formatDate(date, "dd.MM.yyyy")
                        timeLabel.text = Qt.formatTime(date, "hh:mm:ss")
                    }
                }
            }
        }

        Repeater {
            model: CanController.batteries

            delegate: Battery {
                battery: modelData
                batteryIndex: index
                Layout.fillWidth: true
                Layout.fillHeight: true
            }
        }
    }
}
