local awful = require("awful")
local wibox = require("wibox")
local theme = require("theme")

local tag_list = {}

tag_list.create = function(s)
    local filter = function(tag)
        local count = 0
        for _, t in ipairs(s.tags) do
            if #t:clients() > 0 or t.selected then
                count = count + 1
            end
        end
        return (#tag:clients() > 0 and count > 1) or (tag.selected and #tag:clients() == 0 and count > 1)
    end

    local taglist_buttons = awful.util.table.join(
        awful.button({}, 1,
            function(c)
                c.selected = true
                for _, tag in ipairs(s.tags) do
                    if tag.index ~= c.index then
                        tag.selected = false
                    end
                end
            end
        )
    )
    return awful.widget.taglist {
        screen  = s,
        -- filter  = awful.widget.taglist.filter.noempty,
        filter  = filter,
        widget_template = {
            {
                {
                    id     = 'index_role',
                    widget = wibox.widget.textbox,
                },
                left  = 10,
                right = 10,
                widget = wibox.container.margin
            },
            id     = 'background_role',
            widget = wibox.container.background,

            create_callback = function(self, c3, _, _)
                self:get_children_by_id('index_role')[1].markup = '<b> '..c3.name..' </b>'
                self:connect_signal('mouse::enter', function()
                    if self.bg ~= theme.top_panel_powerline then
                        self.backup     = self.bg
                        self.has_backup = true
                    end
                    self.bg = theme.top_panel_powerline
                end)
                self:connect_signal('mouse::leave', function()
                    if self.has_backup and not c3.selected then self.bg = self.backup end
                end)
            end,
        },
        buttons = taglist_buttons,
    }

end
return tag_list
