Name = "twallpaper"
NamePretty = "Wallpaper switcher"
Icon = "applications-other"
Cache = true
Action = "swww img %VALUE%"
HideFromProviderlist = false
Description = "Wallpaper change menu"
SearchName = true

-- Конфигурация
local config = {
    wallpaper_dir = os.getenv("HOME") .. "/Pictures/Wallpapers",
    supported_formats = {
        jpg = true, jpeg = true, png = true,
        gif = true, bmp = true, webp = true
    }
}

function GetEntries()
    local entries = {}
    local lfs = require("lfs")

    -- Проверяем существование основной директории
    local test_handle = io.open(config.wallpaper_dir, "r")
    if not test_handle then
        return {{
            Text = "Error: Wallpaper directory not found!",
            Subtext = config.wallpaper_dir,
            Value = "",
            Preview = "",
            PreviewType = "text",
            Icon = "error"
        }}
    end
    test_handle:close()

    -- Получаем список поддиректорий
    local subdirs = {}
    for item in lfs.dir(config.wallpaper_dir) do
        if item ~= "." and item ~= ".." then
            local path = config.wallpaper_dir .. "/" .. item
            local attr = lfs.attributes(path)
            if attr and attr.mode == "directory" then
                table.insert(subdirs, item)
            end
        end
    end

    -- Создаём запись для основной директории (все обои без группировки)
    table.insert(entries, {
        Text = "All wallpapers",
        Subtext = "Show all available wallpapers",
        Value = "all",
        Preview = "",
        PreviewType = "text",
        Icon = "view-fullscreen"
    })

    -- Создаём записи для поддиректорий (подменю)
    for _, dirname in ipairs(subdirs) do
        table.insert(entries, {
            Text = dirname,
            Subtext = "Wallpapers from '" .. dirname .. "'",
            Value = dirname,
            Preview = "",
            PreviewType = "text",
            Icon = "folder"
        })
    end

    return entries
end

-- Функция для получения обоев из конкретной директории
function GetWallpapersFromDir(dirname)
    local wallpapers = {}
    local lfs = require("lfs")
    local target_dir

    if dirname == "all" then
        target_dir = config.wallpaper_dir
    else
        target_dir = config.wallpaper_dir .. "/" .. dirname
    end

    for filename in lfs.dir(target_dir) do
        if filename ~= "." and filename ~= ".." then
            local filepath = target_dir .. "/" .. filename
            local attr = lfs.attributes(filepath)
            if attr and attr.mode == "file" then
                local ext = filename:match("%.([^%.]+)$")
                if ext and config.supported_formats[ext:lower()] then
                    table.insert(wallpapers, {
                        Text = filename,
                        Subtext = "From '" .. (dirname == "all" and "All" or dirname) .. "'",
                        Value = filepath,
                        Preview = filepath,
                        PreviewType = "file",
                        Icon = filepath
                    })
                end
            end
        end
    end

    table.sort(wallpapers, function(a, b) return a.Text < b.Text end)
    return wallpapers
end

