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

import Controls 1.0 as CustomControls
import Palette 1.0

import Tksi 1.0

ApplicationWindow {
    id: root
    width: 1920
    height: 1080
    visible: true
    visibility: Qt.platform.os === "linux" ? "FullScreen" : "Windowed"
    flags: Qt.platform.os === "linux" ? Qt.FramelessWindowHint | Qt.Window : Qt.Window
    title: "ТКСИ 310 монитор"

    ColumnLayout {
        anchors.leftMargin: 56
        anchors.rightMargin: 56
        anchors.topMargin: 80
        anchors.bottomMargin: 58
        anchors.fill: parent

        spacing: 72

        RowLayout {
            spacing: 96

            ColumnLayout {
                spacing: 16

                CustomControls.Label {
                    text: "Статус модулей"
                }

                RowLayout {
                    spacing: 16

                    CustomControls.IndicatorTextField {
                        text: DataController.firstStatus
                        horizontalAlignment: Text.AlignHCenter
                        readOnly: true
                        Layout.preferredWidth: 160
                    }

                    CustomControls.IndicatorTextField {
                        text: DataController.secondStatus
                        horizontalAlignment: Text.AlignHCenter
                        readOnly: true
                        Layout.preferredWidth: 160
                    }

                    CustomControls.IndicatorTextField {
                        visible: DataController.firstStatus === "0x07" && DataController.secondStatus === "0x07"
                        text: "Готов к работе"
                        backgroundColor: "#C4FFCA"
                        backgroundRadius: 16
                        font.pixelSize: 24
                        font.weight: Font.Normal
                        leftPadding: 16
                        rightPadding: 16
                        readOnly: true
                        Layout.preferredWidth: 200
                    }
                }

                Item {
                    Layout.fillHeight: true
                }

                Layout.fillHeight: true
            }

            Item {
                Layout.fillWidth: true
            }

            CustomControls.DescriptionTextField {
                // visible: modeSwitch.checkState === Qt.PartiallyChecked
                descriptionText: "Вход ПСН"
                indicatorText: "В"
                text: DataController.psnInputVoltage
                readonly: true
                Layout.alignment: Qt.AlignTop
                Layout.fillWidth: false
                Layout.preferredWidth: 320
            }

            CustomControls.DescriptionTextField {
                descriptionText: "Сеть"
                indicatorText: "В"
                text: DataController.mainVoltage
                readonly: true
                Layout.alignment: Qt.AlignTop
                Layout.fillWidth: false
                Layout.preferredWidth: 320
            }

            ColumnLayout {
                spacing: 16

                CustomControls.Label {
                    id: dateLabel
                    text: "Статус модулей"
                    Layout.alignment: Qt.AlignCenter
                }

                CustomControls.IndicatorTextField {
                    id: timeField
                    readOnly: true
                    Layout.alignment: Qt.AlignCenter
                    Layout.preferredWidth: 220
                }

                Item {
                    Layout.fillHeight: true
                }

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

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

                Layout.fillHeight: true
            }

            Layout.fillHeight: false
            Layout.fillWidth: true
        }

        CustomControls.TristateSwitch {
            id: modeSwitch
//            enabled: false // uncomment for non clickable
            firstStateDescription: "АИН 1,2,3"
            secondStateDescription: "АИН 4,5,6"
            checkState: DataController.ainMode === 2 ? Qt.Unchecked : DataController.ainMode === 1 ? Qt.Checked : Qt.PartiallyChecked
            Layout.fillHeight: false
            Layout.fillWidth: true
        }

        RowLayout {
            RowLayout {
                spacing: 56
                visible: modeSwitch.checkState === Qt.PartiallyChecked

                CustomControls.GroupBox {
                    title: "АИН 1"
                    subTitle: (DataController.ainReservedMask & 0x1) != 0 ? "Резервирование" : ""

                    ColumnLayout {
                        anchors.fill: parent
                        spacing: 32

                        CustomControls.DescriptionTextField {
                            descriptionText: "Напряжение"
                            indicatorText: "B"
                            text: DataController.ain1Voltage
                            readonly: true
                            Layout.fillWidth: true
                        }

                        CustomControls.DescriptionTextField {
                            descriptionText: "Ток"
                            indicatorText: "А"
                            text: DataController.ain1Current
                            readonly: true
                            Layout.fillWidth: true
                        }
                    }

                    Layout.preferredWidth: (parent.width - 2 * parent.spacing) / 3
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }

                CustomControls.GroupBox {
                    title: "АИН 2"
                    subTitle: (DataController.ainReservedMask & 0x2) != 0 ? "Резервирование" : ""

                    ColumnLayout {
                        anchors.fill: parent
                        spacing: 32

                        CustomControls.DescriptionTextField {
                            descriptionText: "Напряжение"
                            indicatorText: "B"
                            text: DataController.ain2Voltage
                            readonly: true
                            Layout.fillWidth: true
                        }

                        CustomControls.DescriptionTextField {
                            descriptionText: "Ток"
                            indicatorText: "А"
                            text: DataController.ain2Current
                            readonly: true
                            Layout.fillWidth: true
                        }
                    }

                    Layout.preferredWidth: (parent.width - 2 * parent.spacing) / 3
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }

                CustomControls.GroupBox {
                    title: "АИН 3"
                    subTitle: (DataController.ainReservedMask & 0x4) != 0 ? "Резервирование" : ""

                    ColumnLayout {
                        anchors.fill: parent
                        spacing: 32

                        CustomControls.DescriptionTextField {
                            descriptionText: "Напряжение"
                            indicatorText: "B"
                            text: DataController.ain3Voltage
                            readonly: true
                            Layout.fillWidth: true
                        }

                        CustomControls.DescriptionTextField {
                            descriptionText: "Ток"
                            indicatorText: "А"
                            text: DataController.ain3Current
                            readonly: true
                            Layout.fillWidth: true
                        }
                    }

                    Layout.preferredWidth: (parent.width - 2 * parent.spacing) / 3
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }

                Layout.fillWidth: true
                Layout.fillHeight: true
            }

            RowLayout {
                spacing: 56
                visible: modeSwitch.checkState === Qt.Checked

                CustomControls.GroupBox {
                    title: "АИН 4"
                    subTitle: (DataController.ainReservedMask & 0x8) != 0 ? "Резервирование" : ""

                    ColumnLayout {
                        anchors.fill: parent
                        spacing: 32

                        CustomControls.DescriptionTextField {
                            descriptionText: "Напряжение"
                            indicatorText: "B"
                            text: DataController.ain4Voltage
                            readonly: true
                            Layout.fillWidth: true
                        }

                        CustomControls.DescriptionTextField {
                            descriptionText: "Ток"
                            indicatorText: "А"
                            text: DataController.ain4Current
                            readonly: true
                            Layout.fillWidth: true
                        }
                    }

                    Layout.preferredWidth: (parent.width - 2 * parent.spacing) / 3
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }

                CustomControls.GroupBox {
                    title: "АИН 5"
                    subTitle: (DataController.ainReservedMask & 0x10) != 0 ? "Резервирование" : ""

                    ColumnLayout {
                        anchors.fill: parent
                        spacing: 32

                        CustomControls.DescriptionTextField {
                            descriptionText: "Напряжение"
                            indicatorText: "B"
                            text: DataController.ain5Voltage
                            readonly: true
                            Layout.fillWidth: true
                        }

                        CustomControls.DescriptionTextField {
                            descriptionText: "Ток"
                            indicatorText: "А"
                            text: DataController.ain5Current
                            readonly: true
                            Layout.fillWidth: true
                        }
                    }

                    Layout.preferredWidth: (parent.width - 2 * parent.spacing) / 3
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }

                CustomControls.GroupBox {
                    title: "АИН 6"
                    subTitle: (DataController.ainReservedMask & 0x20) != 0 ? "Резервирование" : ""

                    ColumnLayout {
                        anchors.fill: parent
                        spacing: 32

                        CustomControls.DescriptionTextField {
                            descriptionText: "Напряжение"
                            indicatorText: "B"
                            text: DataController.ain6Voltage
                            readonly: true
                            Layout.fillWidth: true
                        }

                        CustomControls.DescriptionTextField {
                            descriptionText: "Ток"
                            indicatorText: "А"
                            text: DataController.ain6Current
                            readonly: true
                            Layout.fillWidth: true
                        }
                    }

                    Layout.preferredWidth: (parent.width - 2 * parent.spacing) / 3
                    Layout.fillWidth: true
                    Layout.fillHeight: true
                }

                Layout.fillWidth: true
                Layout.fillHeight: true
            }

            Item {
                visible: modeSwitch.checkState === Qt.Unchecked
                Layout.fillWidth: true
                Layout.fillHeight: true
            }

            Layout.fillWidth: true
            Layout.fillHeight: true
        }
    }

    MouseArea {
        visible: Qt.platform.os === "linux"
        anchors.fill: parent
        enabled: false
        cursorShape: Qt.BlankCursor
    }

    background: Rectangle {
        color: Palette.backgroundColor
    }
}
