--   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/>. 
-- awful.widget.tasklist but it can change geometry based on "vertical" property
local awful = require("awful")
local wibox = require("wibox")
local gears = require("gears")
local awmtk2 = require("awmtk2")
local tasklist_buttons = gears.table.join(
    awful.button({}, 1, function(c)
        if c == client.focus then
            c.minimized = true
        else
            c:emit_signal(
                "request::activate",
                "tasklist",
                {raise = true}
            )
        end
    end),
    awful.button({}, 4, function()
        awful.client.focus.byidx(1)
    end),
    awful.button({}, 5, function()
        awful.client.focus.byidx(-1)
    end)
)

return function(args)
    local style = awmtk2.create_style("tasklist", awmtk2.default, args.style)
    local templates = awmtk2.create_template_lib("tasklist", awmtk2.templates, args.templates)
    local t = awmtk2.build_templates(templates,style)
    local button = t.button({
        {
            {
                id = "clienticon",
                widget = awful.widget.clienticon
            },
            (not args.vertical) and t.textbox({
                id = "text_role"
            }),
            layout = wibox.layout.fixed.horizontal,
            spacing = style.button.spacing
        },
        widget = wibox.container.constraint,
        strategy="exact",
        width = style.button.width,
        height = style.button.height,
        id = "constraint_task"
    },{
        create_callback = function(self, c, index, objects)
            self:get_children_by_id('clienticon')[1].client = c
            -- If for some ungodly reason you enabled the behaviour of the original awesomewm, this script will just stop after setting the client icon.
            if self.id == "background_role" then
                return
            end
            local textbox = self:get_children_by_id("text_role")[1] or {}
            -- Apparently the original system for changing bgimage is
            -- 1) broken
            -- 2) uses deprecated functions (nice code practices awesomewm)
            -- Solution: write my own. I blame material design for all this.
            -- (P.S: Not to bullshit you, check it yourself - replicatable
            -- by adding theme.tasklist_bg_image_normal or 
            -- theme.tasklist_bg_image_focus in current beautiful theme.)
            local onfocus = function()
                self.bgimage = style.button.bgimage_focus
                self.bg = style.button.bg_focus
                self.shape = style.button.shape_focus
                self.shape_border_width = style.button.shape_border_width_focus
                self.shape_border_color = style.button.shape_border_color_focus
                textbox.font = style.textbox.font_focus
            end
            local onunfocus = function()
                self.bgimage = style.button.bgimage_normal
                self.bg = style.button.bg_normal
                self.shape = style.button.shape_normal
                self.shape_border_width = style.button.shape_border_width_normal
                self.shape_border_color = style.button.shape_border_color_normal
                textbox.font = style.textbox.font_normal
            end
            local onurgent = function()
                if not c.urgent then return end
                self.bgimage = style.button.bgimage_urgent
                self.bg = style.button.bg_urgent
                self.shape = style.button.shape_urgent
                self.shape_border_width = style.button.shape_border_width_urgent
                self.shape_border_color = style.button.shape_border_color_urgent
                textbox.font = style.textbox.font_urgent
            end
            local onminimize = function()
                if not c.minimized then return end
                self.bgimage = style.button.bgimage_minimize
                self.bg = style.button.bg_minimize
                self.shape = style.button.shape_minimize
                self.shape_border_width = style.button.shape_border_width_minimize
                self.shape_border_color = style.button.shape_border_color_minimize
                textbox.font = style.textbox.font_minimize
            end
            c:connect_signal("focus",onfocus)
            c:connect_signal("unfocus",onunfocus)
            c:connect_signal("property::urgent",onurgent)
            c:connect_signal("property::minimized",onminimize)
            if client.focus == c then 
                onfocus()
            end
        end,
        -- Uncomment this only, and **ONLY** if you actually need it.
        --id = "background_role"
    })
    return awful.widget.tasklist {
        screen = args.screen,
        filter = awful.widget.tasklist.filter.currenttags,
        buttons = tasklist_buttons,
        layout = {
            -- basically we just map every property of this to beautiful.tasklist.base
            spacing = style.base.spacing,
            spacing_widget = style.base.spacing_widget,
            -- Now THIS is shit racing
            layout = (
                (args.vertical and style.base.layout_vertical) or
                style.base.layout_horizontal
            ) or ( 
                (args.vertical and wibox.layout.fixed.vertical) or
                wibox.layout.fixed.horizontal
            )
        },
        widget_template = button
    }
end
