local mod_name = minetest.get_current_modname()
local mod_path = minetest.get_modpath(mod_name)
local S = minetest.get_translator(mod_name)

aom_compost = {}

-- dofile(mod_path .. DIR_DELIM .. "api.lua")

local function get_random_time()
    return math.random(250,300)
end

local function on_timer(pos, time)
    local node = minetest.get_node(pos)
    if true then
        local i = minetest.get_item_group(node.name, "item_compost")
        minetest.swap_node(pos, {name="aom_compost:compost_"..(i)})
        minetest.get_node_timer(pos):start(get_random_time())
        return false
    else
        minetest.get_node_timer(pos):set(1,0)
    end
end

local function on_construct(pos)
    minetest.get_node_timer(pos):start(get_random_time())
end

local function _on_node_update(pos)
    local nt = minetest.get_node_timer(pos)
    if nt:is_started() then return false end
    nt:start(get_random_time())
end


local count = 3
for i = 0, count do
    minetest.register_node('aom_compost:compost_'..i, {
        description = S("Compost"),
        groups = {
            full_solid = 1, nature = 1, item_compost = i+1, oddly_breakable_by_hand = 3,
            snappy = 1, solid = 1, suffocates = 2 },
        tiles = {"aom_compost_"..i..".png"},
        drop = (i == count) and {
            max_items = 40,
            items = {
                {
                    items = {'pmb_items:nitrite 15'},
                    rarity = 1
                },
                {
                    items = {'pmb_items:sulphur 2'},
                    rarity = 1
                },
            },
        } or nil,
        sounds = pmb_sounds.default_snow(),
        on_timer = (i < count) and on_timer or nil,
        on_construct = (i < count) and on_construct or nil,
        _on_node_update = (i < count) and _on_node_update or nil,
    })
end

pmb_tcraft.register_craft({
    output = "aom_compost:compost_0",
    items = {
        ["pmb_items:plant_fibre"] = 1,
    },
})
