--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
--  Powerman X - second generation of the power management daemon
local sysctl = require("syscontrol")
local naughty = require("naughty")
local gears = require("gears")
local batteries = sysctl.power_supply.enumerate()
local state_tracking = {}
-- Configuration variables
local cfg = config.powerman or {}
local quality_min = cfg.battery_quality_min or 33
local capacity_min = cfg.battery_capacity_min or 15
-- Main loop
gears.timer({
    timeout = 2,
    autostart = true,
    callback = function()
        for _,v in pairs(batteries) do
            local data,_ = sysctl.power_supply.read_attribs(v)
            state_tracking[v] = state_tracking[v] or {}
            if data.type == "Battery" then
                if (tonumber(data.quality) < quality_min) and 
                   (not state_tracking[v].quality_notification) then
                    naughty.notify({
                        title = "Critical battery condition",
                        text = "Battery "..data.name.." has reached critically low condition, seek a suitable replacement"
                    })
                    state_tracking[v].quality_notification = true
                end
                if (tonumber(data.capacity) <= capacity_min) and 
                   (not data.charging) and
                   (not state_tracking[v].capacity_notification) then
                    naughty.notify({
                        title = "Battery capacity low",
                        text = "Battery "..data.name.." capacity is at "..tostring(data.capacity).."%"
                    })
                    state_tracking[v].capacity_notification = true
                end
                if (tonumber(data.capacity) > capacity_min) then
                    state_tracking[v].capacity_notification = false
                end
                if (data.capacity == "100") and 
                   (data.charging) and
                   (not state_tracking[v].charged_notification) then
                    naughty.notify({
                        title = "Battery is completely charged",
                        text = "Disconnect the charger from the power grid to avoid passive electricity usage."
                    })
                end
                if (not data.charging) then
                    state_tracking[v].charged_notification = false
                end
            end
        end
    end
})

