-- Fabric Patterns Management

local modpath = minetest.get_modpath("vox_fabric")
local patterns_file = modpath .. "/textures/patterns.json"
local patterns = {}

-- Load predefined patterns
local file = io.open(patterns_file, "r")
if file then
    local content = file:read("*a")
    file:close()
    patterns = minetest.parse_json(content) or {}
end

-- Register predefined patterns as Pattern Template items
for _, pattern in ipairs(patterns.patterns or {}) do
    minetest.register_craftitem("vox_fabric:pattern_" .. pattern.id, {
        description = "Pattern: " .. (pattern.title or pattern.id),
        inventory_image = "patterns/" .. pattern.id .. ".png",
        groups = {pattern = 1},
    })
end

-- Pattern Template Item
minetest.register_craftitem("vox_fabric:pattern_template", {
    description = "Pattern Template",
    inventory_image = "pattern_template.png",
    groups = {template = 1},
    on_use = function(itemstack, user, pointed_thing)
        -- Interaction with Pattern Template
        local meta = itemstack:get_meta()
        local pattern_id = meta:get_string("pattern_id")
        if pattern_id and pattern_id ~= "" then
            minetest.chat_send_player(user:get_player_name(), "Pattern ID: " .. pattern_id)
        else
            minetest.chat_send_player(user:get_player_name(), "This template has no pattern.")
        end
    end,
})
