--   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/>. 
-- wibox.widget.textclock but with awmtk2 templates.
local awmtk2 = require("awmtk2")
local wibox = require("wibox")
local gears = require("gears")
local awful = require("awful")
local beautiful = require("beautiful")
local glib = require("lgi").GLib
local DateTime = glib.DateTime
local TimeZone = glib.TimeZone

return function(args)
    local style = awmtk2.create_style("clock",
        awmtk2.generic.status_widget,args.style)
    local templates = awmtk2.create_template_lib("clock",awmtk2.templates,args.templates)
    local t = awmtk2.build_templates(templates,style)
    -- Don't mind me just stealing default library code
    local format = args.format or "%a %b %d, %H:%M"
    local refresh = args.refresh or 60
    local tzid = args.tzid
    local timezone = (tzid and TimeZone.new(tzid)) or TimeZone.new_local()

    -- Seriously, was it so hard to add a font parameter for textclock?
    -- Do you actually expect of nobody to change the font to something like
    -- idk, 7-segment display? Goddamnit awesome. I trusted you.
    local widget = wibox.widget(t.container(t.textbox({
        id = "clocktext"
    })))
    local textbox = widget:get_children_by_id("clocktext")[1]
    widget.clocktimer = gears.timer {
        timeout = refresh,
        call_now = true,
        autostart = true,
        callback = function()
            local str = DateTime.new_now(timezone):format(format)
            if str == nil then
               require("gears.debug").print_warning("clock: "
                    .. "g_date_time_format() failed for format "
                    .. "'" .. format .. "'")
            end
            textbox:set_markup(str)
        end
    }
    return widget
end
