--   This file is part of Reno desktop.
--
--  Reno desktop is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
--
--  Reno desktop is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along with Reno desktop. If not, see <https://www.gnu.org/licenses/>. 
-- Notifications pager
local awful = require("awful")
local pager = require("pager")
local beautiful = require("beautiful")
local gears = require("gears")
local wibox = require("wibox")
local awmtk2 = require("awmtk2")
local naughty = require("naughty")

return function(args)
    local style = awmtk2.create_style("notifications",
        awmtk2.generic.iconified_widget,args.style)
    local templates = awmtk2.create_template_lib("notifications",awmtk2.templates,args.templates)
    local t = awmtk2.build_templates(templates,style)
    local layout = wibox.widget({
        layout = wibox.layout.fixed.vertical,
        spacing = style.base.spacing
    })
    local pager = pager(layout,{},args.max_notifications or 8)
    local test_xclip = os.execute("xclip -version")
    local result = test_xclip
    if _VERSION:match("5.1") then
        result = (test_xclip == 0)
    end
    local count = 0
    local popup = awful.popup(t.popup({
        t.textbox({
            markup = "Click to copy text"
        }),
        ((not result) and t.textbox({
            markup = "(xclip is not installed)"
        })),
        t.container(layout),
        t.textbox({
            id = "page_id",
            markup = "Page 0/0"
        }),
        layout = wibox.layout.fixed.vertical
    },{
        visible = false
    }))
    naughty.config.notify_callback = function(update_args)
        count = count + 1
        local w = wibox.widget(t.button(t.article({
            icon = update_args.icon,
            title = update_args.title or "(No title)",
            description = update_args.text
        }),{
            forced_height = style.button.height,
            forced_width = style.button.width
        }))
        local page_index = popup.widget:get_children_by_id("page_id")[1]
        page_index:set_markup("Page "..tostring(pager.index+1).."/"..
            tostring(math.ceil(count/(args.max_notifications or 8))))
        w:connect_signal("button::press",style.button.onpress)
        w:connect_signal("button::release",style.button.onrelease)
        w:connect_signal("button::press",function(self,x,y,button)
            if button == 1 then
                clip = io.open("/tmp/clip","w")
                clip:write(update_args.text)
                clip:close()
                awful.spawn.with_shell("cat /tmp/clip | xclip -selection clipboard")
            elseif button == 4 then
                pager:prev()
                page_index:set_markup("Page "..tostring(pager.index+1).."/"..
                tostring(math.ceil(count/(args.max_notifications or 8))))
            elseif button == 5 then
                pager:next()
                page_index:set_markup("Page "..tostring(pager.index+1).."/"..
                tostring(math.ceil(count/(args.max_notifications or 8))))
            end
        end)
        table.insert(pager.list,1,w)
        pager:update()
        return update_args
    end
    -- create popup button
    local clip_widget = wibox.widget(t.button({
        image = beautiful["notifications-area-symbolic"],
        resize = true,
        widget = wibox.widget.imagebox,
    }))
    clip_widget:connect_signal("button::press",style.button.onpress)
    clip_widget:connect_signal("button::release",style.button.onrelease)
    clip_widget:connect_signal("button::press",function(self,x,y,button)
        if button == 1 then
            popup.visible = (not popup.visible)
            if popup.visible then
                popup:move_next_to(mouse.current_widget_geometry)
            end
        end
    end)
    return clip_widget
end
