/*
 *  SPDX-FileCopyrightText: 2024 Sebastian Kügler <sebas@kde.org>
 *  SPDX-FileCopyrightText: 2021 Devin Lin <espidev@gmail.com>
 *  SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

import QtQuick 2.6
import QtQuick.Layouts 1.4

import org.kde.kirigami as Kirigami
import org.kde.kitemmodels

import org.kde.plasma.components 3.0 as PlasmaComponents
import org.kde.plasma.private.mobileshell.shellsettingsplugin as ShellSettings
import org.kde.plasma.private.mobileshell as MobileShell
import org.kde.plasma.private.battery // needed for charging state

RowLayout {
    property real textPixelSize: Kirigami.Units.gridUnit * 0.6

    function batteryIconName(percent, charging) {
        let name;
        if (percent >= 95) {
            name = "battery-100";
        } else if (percent >= 85) {
            name = "battery-090";
        } else if (percent >= 75) {
            name = "battery-080";
        } else if (percent >= 65) {
            name = "battery-070";
        } else if (percent >= 55) {
            name = "battery-060";
        } else if (percent >= 45) {
            name = "battery-050";
        } else if (percent >= 35) {
            name = "battery-040";
        } else if (percent >= 25) {
            name = "battery-030";
        } else if (percent >= 15) {
            name = "battery-020";
        } else if (percent > 5) {
            name = "battery-010";
        } else {
            name = "battery-000";
        }

        return charging ? name + "-charging" : name;
    }

    visible: MobileShell.BatteryInfo.isVisible

    ListView {
        id: batteryRepeater

        spacing: 0
        model: filterModel
        orientation: ListView.Horizontal

        Layout.alignment: Qt.AlignVCenter
        Layout.preferredWidth: contentItem.childrenRect.width
        Layout.fillHeight: true
        Layout.fillWidth: false

        KSortFilterProxyModel {
            id: filterModel
            sourceModel: MobileShell.BatteryInfo.batteries

            filterRoleName: "Type"
            filterString: "Battery" // only show internal batteries
        }

        delegate: RowLayout {
            id: batteryBase

            Layout.fillHeight: false
            Layout.alignment: Qt.AlignVCenter

            height: batteryRepeater.height

            Kirigami.Icon {
                id: battery

                Layout.alignment: Qt.AlignVCenter
                Layout.fillHeight: true
                width: batteryLabel.height
                source: PluggedIn ? batteryIconName(Percent, ChargeState === BatteryControlModel.Charging) : "battery-missing"
                Kirigami.Theme.inherit: false
                Kirigami.Theme.colorSet: Kirigami.Theme.Window
                isMask: true
                color: Kirigami.Theme.textColor
            }

            PlasmaComponents.Label {
                id: batteryLabel
                text: i18n("%1%", Percent)
                Layout.alignment: Qt.AlignVCenter
                Layout.fillHeight: true

                color: Kirigami.Theme.textColor
                visible: ShellSettings.Settings.showBatteryPercentage
                font.pixelSize: textPixelSize
            }
        }
    }
}
