local awful = require("awful")
local beautiful = require("beautiful")
local gears = require("gears")
local spawn = require("awful.spawn")
local wibox = require("wibox")
local naughty = require("naughty")
local awmtk_status,awmtk = pcall(require,"awmtk")
local dbus_config = require("naughty.dbus")
return function(args)
    local args = args or {}
    -- Set up style variables
    style = awmtk.style(awmtk.defaults,args.style or {},"mailbox_")
    style.mailbox_container_width = style.mailbox_container_width or 260
    args.mail_limit = args.mail_limit or 8
    -- Create a notifications list
    local notifications_list = wibox.widget {
        spacing = style.mailbox_container_spacing_vertical,
        layout = wibox.layout.fixed.vertical,
    }
    local notification_popup = awful.popup(style.container(
        {
            {
                text = "Notifications: ",
                widget = wibox.widget.textbox
            },
            notifications_list,
            layout = wibox.layout.fixed.vertical,
            forced_width = style.mailbox_container_width
        },{
            visible = false,
            ontop = true,
        }
    ))

    -- Create a mailbox button
    args.screen.notify_widget = style.icon (
        {
            image  = style["mailbox_icon"],
            widget = wibox.widget.imagebox
        },{},{
            function() 
                if notification_popup.visible then
                    --rows = nil
                    notification_popup.visible = not notification_popup.visible
                else
                    --init_popup()
                    for s in screen do
                        s.notify_widget.bg = style.mailbox_button_bg
                    end
                    notification_popup:move_next_to(mouse.current_widget_geometry)
                end
            end
        }
    )
    naughty.config.notify_callback = function(update_args)
        notifications_list:insert(1,style.button(
            {
                (update_args.icon and {
                    image = update_args.icon,
                    resize = true,
                    widget = wibox.widget.imagebox
                }),
                {
                    markup = "<b>"..
                        (update_args.title or "")..
                        "</b>\n"..(update_args.text or ""),
                    widget = wibox.widget.textbox,
                },
                layout = wibox.layout.fixed.horizontal
            },{
                bg = style.mailbox_button_bg_focus,
                shape = style.mailbox_container_shape,
                border_width = style.mailbox_button_border_width,
                forced_height = style.mailbox_button_height
            },{
                function()
                    clip = io.open("/tmp/clip","w")
                    clip:write(update_args.text)
                    clip:close()
                    awful.spawn.with_shell("cat /tmp/clip | xclip -selection clipboard")
                    notification_popup.visible = false
                end
            }
        ))
        if #notifications_list.children > args.mail_limit then
            notifications_list:remove(#notifications_list.children)
        end
        for s in screen do
            s.notify_widget.bg = style.mailbox_button_bg_focus
        end
        return update_args
    end
    return args.screen.notify_widget
end
