local awmtk2 = require("awmtk2")
local wibox = require("wibox")
local gears = require("gears")
local awful = require("awful")

return function(args)
    local style = awmtk2.create_style("dismal",awmtk2.default,args.style)
    local rounded_shape = function(cr,width,height)
        return gears.shape.rounded_rect(cr,width,height,style.container.rounding)
    end
    local separator = wibox.widget({
        widget = wibox.widget.separator,
        forced_width = 220,
        orientation = "horizontal",
        forced_height = 10,
        thickness = 1,
        color = style.container.bg_focus,
        border_width = 0
    })
    local inputbox = wibox.widget {
        {
            markup = "",
            widget = wibox.widget.textbox
        },
        widget = wibox.container.margin,
        margins = style.container.inner_margin,
        id = "inputbox",
        bg = style.button.bg_focus
    }
    local launchpad = awful.popup({
        widget = {
            {
                {
                    inputbox,
                    {
                        {
                            {
                                {
                                    markup = " ",
                                    widget = wibox.widget.textbox,
                                    id = "result1"
                                },
                                separator,
                                {
                                    markup = " ",
                                    widget = wibox.widget.textbox,
                                    id = "result2"
                                },
                                separator,
                                {
                                    markup = " ",
                                    widget = wibox.widget.textbox,
                                    id = "result3"
                                },
                                separator,
                                {
                                    markup = " ",
                                    widget = wibox.widget.textbox,
                                    id = "result4"
                                },
                                layout = wibox.layout.fixed.vertical
                            },
                            widget = wibox.container.margin,
                            margins = style.container.inner_margin
                        },
                        widget = wibox.container.background,
                        bg = style.container.bg_normal,
                        shape_border_width = 1,
                        shape_border_color = style.container.bg_focus,
                        shape = rounded_shape
                    },
                    {
                        markup = "<b>Enter</b> - run; <b>Ctrl+Enter</b> - run in terminal",
                        widget = wibox.widget.textbox
                    },
                    layout = wibox.layout.fixed.vertical
                },
                widget = wibox.container.margin,
                margins = style.container.inner_margin
            },
            bg = style.container.bg_normal,
            widget = wibox.container.background
        },
        placement = awful.placement.no_overlap,
        shape = rounded_shape, 
        visible = false,
        ontop = true
    })
    local modifiers = {}
    root.keys(gears.table.join(
        root.keys(),
        awful.key({ global.modkey }, "0", function()
            launchpad.visible = true
            if launchpad.visible then
                awful.prompt.run {
                    prompt = "<b>Run: </b>",
                    textbox = inputbox.children[1],
                    hooks = {
                        { { "Control" }, "Return", function(cmd) 
                            awful.spawn({global.terminal, "-e", cmd})
                        end},
                        { { }, "Return", function(cmd) 
                            awful.spawn(cmd)
                        end},
                        { {}, 'Escape', function(cmd)
                            textbox.markup = ""
                        end}
                    },
                    done_callback = function()
                        launchpad.visible = false
                    end
                }
            end
        end)
    ))
    return launchpad
end
