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")
local widget = {}
local function worker(args)
    local args = args or {}
    -- Set up style variables
    style = awmtk.style(awmtk.defaults,args.style,"mailbox_")
    style.mailbox_container_width = style.mailbox_container_width or 260
    style.mailbox_button_height = style.mailbox_button_heihgt or 40
    args.mail_limit = args.mail_limit or 8
    -- Create a notifications list
    notifications_list = {
        spacing = style.mailbox_container_spacing_vertical,
        layout = wibox.layout.fixed.vertical,
    }
    -- Function to regenerate the popup
    local gen_popup = function()
         return awful.popup(style.container(
            {
                {
                    text = "This widget will store your notification",
                    widget = wibox.widget.textbox
                },
                notifications_list,
                layout = wibox.layout.fixed.vertical,
                forced_width = style.mailbox_container_width
            },{
                visible = false,
                ontop = true,
            }
        ))
    end
    
    -- Initialize the popup
    local notification_popup = gen_popup()

    -- 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
        }
    )
    local notify_widget = args.screen.notify_widget
    local update_notifications = function(update_args)
        if notification_popup.visible then notification_popup.visible = false end
        table.insert(notifications_list,1,style.button(
            {
                (update_args.icon and {
                    image = update_args.icon,
                    resize = true,
                    forced_height = style.mailbox_button_height,
                    widget = wibox.widget.imagebox
                }),
                {
                    markup = "<b>"..
                        (update_args.title or "")..
                        "</b>\n"..(update_args.text or ""),
                    widget = wibox.widget.textbox,
                    forced_height = style.mailbox_button_height
                },
                layout = wibox.layout.fixed.horizontal
            },{
                bg = style.mailbox_button_bg_focus,
                shape = style.mailbox_container_shape,
                border_width = style.mailbox_button_border_width
            },{
                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 >= args.mail_limit then
            table.remove(notifications_list,args.mail_limit)
        end
        notification_popup = gen_popup()
        for s in screen do
            s.notify_widget:emit_signal("notification::arrived")
        end
        return update_args
    end
    args.screen.notify_widget:connect_signal("notification::arrived",function()
        args.screen.notify_widget.bg = style.mailbox_button_bg_focus
    end)
    naughty.config.notify_callback = update_notifications
    widget = notify_widget
    return notify_widget
end

return setmetatable(widget, { __call = function(_, ...) return worker(...) end })
