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

import Core 1.0
import Controls 1.0 as CustomControls
import Palette 1.0

Rectangle {
    radius: 14
    color: Palette.controlBackgroundColor

    property BatteryController battery: null
    property int batteryIndex: 0

    onBatteryIndexChanged: {
        if (battery) {
            battery.batteryIndex = batteryIndex
        }
    }

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 20
        spacing: 10

        RowLayout {
            CustomControls.Label {
                text: "АКБ %1".arg(batteryIndex + 1)
                font.pixelSize: 20
                font.weight: Font.DemiBold
                Layout.fillWidth: true
            }

            CustomControls.Button {
                text: chargeTypeToString(battery ? battery.chargeType : 0)
                backgroundColor: workingModeToColor(battery ? battery.workingMode : 0)
                Layout.preferredWidth: 180
                onClicked: battery ? battery.actionButtonClicked() : null
            }
        }

        GridLayout {
            columns: 2
            rowSpacing: 8
            columnSpacing: 16

            Layout.fillWidth: true

            CustomControls.Label {
                text: "КП"
                horizontalAlignment: Text.AlignHCenter
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.Label {
                text: "УКБП"
                horizontalAlignment: Text.AlignHCenter
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.controlCurrent) : "—"
                indicatorText: "А"
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.measuredCurrent) : "—"
                indicatorText: "А"
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.controlVoltage) : "—"
                indicatorText: "В"
                implicitWidth: 1
                Layout.fillWidth: true
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.measuredVoltage) : "—"
                indicatorText: "В"
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.controlTemperature) : "—"
                indicatorText: "°C"
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.measuredTemperature) : "—"
                indicatorText: "°C"
                implicitWidth: 1
                Layout.fillWidth: true
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.controlCapacity) : "—"
                indicatorText: "Ач"
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }

            CustomControls.TextField {
                text: battery ? convertDoubleToString(battery.measuredCapacity) : "—"
                indicatorText: "Ач"
                Layout.fillWidth: true
                Layout.preferredWidth: 1
            }
        }

        ListView {
            id: statusView

            clip: true
            spacing: 5
            boundsBehavior: Flickable.StopAtBounds
            flickableDirection: Flickable.HorizontalAndVerticalFlick

            model: battery ? battery.statues : null

            delegate: CustomControls.ColoredLabel {
                text: modelData.description
                backgroundColor: Palette.neutralColor
                font.pixelSize: 12
                width: parent.width
            }

            ScrollBar.vertical: ScrollBar {}

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

    function convertIntToString(value) {
        if (value === undefined || value === null) {
            return "—"
        }
        return parseInt(value).toString()
    }

    function convertDoubleToString(value) {
        if (value === undefined || value === null) {
            return "—"
        }
        return parseFloat(value).toFixed(1)
    }

    function chargeTypeToString(type) {
        switch (type) {
            case 1:
                return "Контрольный разряд"
            case 2:
                return "Повторный заряд"
            case 3:
                return "Завершение заряда"
            case 0:
            default:
                return "Первичный подзаряд"
        }
    }

    function workingModeToColor(mode) {
        switch (mode) {
            case 1:
                return Palette.greenButtonColor
            case 2:
                return Palette.redButtonColor
            case 3:
                return Palette.yellowButtonColor
            case 0:
            default:
                return Palette.blueButtonColor
        }
    }
}
