local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local beautiful = require("beautiful")
local awmtk = require("awmtk")
local tasklist_buttons = gears.table.join(
    awful.button({ }, 1, function (c)
            if c == client.focus then
                c.minimized = true
            else
                c:emit_signal(
                    "request::activate",
                    "tasklist",
                    {raise = true}
                )
            end
        end),
    awful.button({ }, 3, function()
            awful.menu.client_list({ theme = { width = 250 } })
        end),
    awful.button({ }, 4, function ()
            awful.client.focus.byidx(1)
        end),
    awful.button({ }, 5, function ()
            awful.client.focus.byidx(-1)
        end))


return function(options)
    local style = awmtk.style(awmtk.defaults,options.style or {},"tasklist_")
    -- Color definitions
    local constraint = options.constraint or style.tasklist_constraint
    local _style = awmtk._create_preprocess_style(style,"tasklist_")
    _style.button_shape = awmtk.preprocess.button_shape(_style)
    local container = style.tasklist_button_widget(_style)
    return function(s)  
        local tasklist = {
            screen  = s,
            filter  = awful.widget.tasklist.filter.currenttags,
            buttons = tasklist_buttons,
            style = {
                shape = container.shape,
                shape_border_width = container.shape_border_width,
                shape_border_color = container.shape_border_color
            },
            layout   = options.container or {
                spacing = style.tasklist_container_spacing,
                layout  = (options.vertical and 
                        wibox.layout.fixed.vertical
                    ) or ((not options.stretch) and 
                        wibox.layout.flex.horizontal
                    ) or
                        wibox.layout.fixed.horizontal

            },
            widget_template = container
        }
        if constraint then
            tasklist.widget_template = {
                container,
                widget = wibox.container.constraint,
                width = (type(constraint) == "number" and constraint) or 180,
                strategy = "exact"
            }
        end
        local top = awmtk.utils.get_child_by_id(
            tasklist.widget_template,"widget_container"
        )[1]
        top[1] = {
            {
                id = 'clienticon',
                widget = awful.widget.clienticon,
            },
            (options.names and {
                id     = 'text_role',
                widget = wibox.widget.textbox,
                ellipsize = "end",
            }),
            left  = style.tasklist_padding or ((not options.vertical) and 6),
            right = style.tasklist_padding or ((not options.vertical) and 6),
            layout = (options.vertical and wibox.layout.fixed.vertical) or 
                wibox.layout.fixed.horizontal,
            spacing = 3,
        }
        local top = awmtk.utils.get_child_by_id(
            tasklist.widget_template,"widget_background"
        )[1]
        top.id = "background_role"
        tasklist.widget_template.create_callback = function(self, c, index, objects)
            self:get_children_by_id('clienticon')[1].client = c
        end
        return awful.widget.tasklist(tasklist)
    end
end
