local awful = require("awful")
local gears = require("gears")
local wibox = require("wibox")
local menu = require("widgets.polymenu")
local move_screentags = {}
local toggle_screentags = {}
awful.screen.connect_for_each_screen(function(s)
    local move_tags = {}
    local toggle_tags = {}
    for _,tag in pairs(s.tags) do
        table.insert(move_tags,{
            tag.name,
            function()
                if client.focus then
                    client.focus:tags({tag})
                end
            end
        })
        table.insert(toggle_tags,{
            tag.name,
            function()
                if client.focus then
                    local tags = client.focus:tags()
                    for k,v in pairs(tags) do
                        if v == tag then
                            table.remove(tags,k)
                            client.focus:tags(tags)
                            return
                        end
                    end
                    table.insert(tags,tag)
                    client.focus:tags(tags)
                end
            end
        })
    end
    table.insert(move_screentags,{
        "Screen "..tostring(s.index),
        move_tags
    })
    table.insert(toggle_screentags,{
        "Screen "..tostring(s.index),
        toggle_tags
    })
end)
return function(c)
    local buttons = gears.table.join(
        awful.button({ }, 1, function()
            c:emit_signal("request::activate", "titlebar", {raise = true})
            awful.mouse.client.move(c)
        end),
        awful.button({ }, 3, function()
            c:emit_signal("request::activate", "titlebar", {raise = true})
            awful.mouse.client.resize(c)
        end)
    )
    c.menu = menu({
        before = {
            {
                awful.titlebar.widget.floatingbutton (c),
                awful.titlebar.widget.stickybutton   (c),
                awful.titlebar.widget.ontopbutton    (c),
                forced_width = 72,
                forced_height = 24,
                layout = wibox.layout.fixed.horizontal,
            }
        },
        items = {
            { "Move to tag" , 
                move_screentags
            },
            { "Toggle on tag",
                toggle_screentags
            }
        },
        vertical = true
    })
    c.menu_button = awful.titlebar.widget.iconwidget(c)
    c.menu_button:connect_signal("button::press", function()
        c.menu.toggle()
        c:emit_signal("request::activate", "titlebar", {raise = true})
    end)
    c:connect_signal("unfocus",function()
        if c.menu.visible then
            c.menu.toggle(0,0)
        end
    end)
    return awful.titlebar(c) : setup {
        { -- Left 
            c.menu_button,
            { -- Title
                align  = "center",
                widget = awful.titlebar.widget.titlewidget(c),
                buttons = buttons
            },
            spacing = 10,
            layout  = wibox.layout.fixed.horizontal
        },
        { -- Middle
            buttons = buttons,
            layout  = wibox.layout.flex.horizontal
        },
        { -- Right
            {
                {
                    awful.titlebar.widget.maximizedbutton(c),
                    awful.titlebar.widget.minimizebutton (c),
                    awful.titlebar.widget.closebutton    (c),
                    layout = wibox.layout.fixed.horizontal(),
                    widget = wibox.container.margin,
                    margins = 3            
                },
                widget = wibox.container.background,
                shape = gears.shape.rounded_bar,
                bg = {
                    type = "linear",
                    from = { 0, 15 },
                    to = { 0, 0},
                    stops = { { 0, "#5d5d5955"} , {1, "39383555"} }
                },
            },
            widget = wibox.container.margin,
            margins = 2
        },
        layout = wibox.layout.align.horizontal,
    }
end
