-- Fabric Dying Bench

minetest.register_node("vox_fabric:fabric_dying_bench", {
    description = "Fabric Dying Bench",
    tiles = {"fabric_dying_bench_top.png", "fabric_dying_bench_side.png"},
    groups = {cracky = 2},
    on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
        -- Show the UI for pattern creation
        local formspec = "size[8,9]" ..
                         "label[0.5,0.5;Fabric Dying Bench]" ..
                         "dropdown[1,1;3;grid_size;16x16,32x32,64x64;2]" ..
                         "button[4,1;2,1;save;Save Pattern]"
        minetest.show_formspec(clicker:get_player_name(), "vox_fabric:dying_bench", formspec)
    end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
    if formname == "vox_fabric:dying_bench" then
        if fields.save then
            -- Save the pattern
            local player_name = player:get_player_name()
            local pattern_id = player_name .. "_pattern_" .. os.time()
            local pattern_file = minetest.get_worldpath() .. "/vox_fabric_generated/" .. pattern_id .. ".json"

            local pattern_data = {
                size = fields.grid_size,
                colors = {}, -- This would be populated based on the player's interactions
            }

            minetest.safe_file_write(pattern_file, minetest.serialize(pattern_data))
            player:get_inventory():add_item("main", "vox_fabric:pattern_template")
            minetest.chat_send_player(player_name, "Pattern saved as: " .. pattern_id)
        end
    end
end)
