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

return function(args)
    local style = awmtk.style(awmtk.defaults, args.style or {},"drawer_")
    local contained_widget = {
         id = "contents",
         widget = wibox.container.background,
         layout = (args.vertical and wibox.layout.fixed.vertical) or
                  wibox.layout.fixed.horizontal,
         visible = false
    }
    for k,v in ipairs(args) do
        table.insert(contained_widget,v)
    end
    local widget = style.icon {
        contained_widget,
        {
            widget = wibox.widget.imagebox,
            image = style["drawer_closed_icon"],
            id = "widget_toggle",
            resize = true
        },
        layout = (args.vertical and wibox.layout.fixed.vertical) or
                 wibox.layout.fixed.horizontal,
        spacing = style.drawer_container_spacing_horizontal
    }
    widget:get_children_by_id("widget_toggle")[1]:buttons(
        awful.util.table.join(
            awful.button({},1,function() 
                local subwidget = widget:get_children_by_id("contents")[1]
                local image = widget:get_children_by_id("widget_toggle")[1]
                if not subwidget.visible then
                    image.image = style["drawer_open_icon"]
                else
                    image.image = style["drawer_closed_icon"]
                end
                subwidget.visible = not subwidget.visible
                
            end)
        )
    )
    return widget
end
