local awful = require("awful")
local beautiful = require("beautiful")
local gears = require("gears")
local spawn = require("awful.spawn")
local wibox = require("wibox")
local awmtk = require("awmtk")
local thumbnailer = require("thumbnail")
local widget = {}
local function ls(path)
    local ls_data = io.popen("ls -1 "..path)
    local output = {}
    if ls_data then
        ls_data:read("*a"):gsub("[^\n]+",function(capt) table.insert(output,capt) end)
        ls_data:close()
    else
        error("ls "..path.." failed.")
    end
    return output
end

local function worker(args)
    local args = args or {}
    assert(type(args["screen"]) ~= "nil","Screen not specified")
    assert(type(args["path"]) ~= "nil","Path to wallpapers folder not specified")
    --add an extra slash to path if there is none
    if not args["path"]:match("/$") then
        args["path"] = args["path"].."/"
    end
    --create local style
    local style = awmtk.style(awmtk.defaults,args.style or {},"wallpapers_")
    --set wallpaper
    local handler = io.open(global.config_dir.."/.wallpaper","r") 
    if handler then
        local wallpaper_path = handler:read("*a")
        gears.wallpaper.maximized(wallpaper_path,args["screen"])
    end
    local function update_last_wallpaper(s)
        local handler = io.open(global.config_dir.."/.wallpaper","w")
        handler:write(s)
        handler:close()
    end
    --read wallpapers from the wallpaper directory
    local image_list = ls(args["path"])
    for k,v in pairs(image_list) do
        if not (v:match("%.jpg$") or v:match("%.png$")) then
            image_list[k] = nil
        end
    end 
    --generate thumbnails to save up memory
    thumbnailer.generate(args["path"],args["path"]..".thumbnails",60)
    --style variables
    local button_bg = style.wallpapers_button_bg_focus
    local function new_wallpaper_button(image,s)
        local new_widget = style.button({
            image  = args["path"]..".thumbnails/"..image,
            resize = true,
            widget = wibox.widget.imagebox
        },{
            bg = button_bg,
            forced_height = style.wallpapers_button_height or 60,
            forced_width = style.wallpapers_button_width or 100
        },{
            function()
                gears.wallpaper.maximized(args["path"]..image,s)
                update_last_wallpaper(args["path"]..image)
            end
        })
        return new_widget
    end
    local copy_list = {
        widget = wibox.layout.grid,
        forced_num_cols = args.columns or 4,
        homogeneous     = true,
        expand = true,
        orientation = "vertical",
        spacing = 5
    }
    for k,v in pairs(image_list) do
        local new_widget = new_wallpaper_button(v,args["screen"])
        table.insert(copy_list,new_widget)
    end
    local popup = awful.popup(style.container(
        copy_list,
        {
            visible = false,
            ontop = true
        }
    ))
    local clip_widget = style.icon({
        image  = style["wallpapers_icon"],
        resize = true,
        widget = wibox.widget.imagebox
    },{},{
        function()
            if popup.visible then
                --rows = nil
                popup.visible = not popup.visible
            else
                --init_popup()
                popup:move_next_to(mouse.current_widget_geometry)
            end
        end
    })
    widget = clip_widget
    return clip_widget
end

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