-- vox_core/trees.lua

-- support for MT game translation.
local S = minetest.get_translator("vox_core")

-- Placeholder for vox_core sound functions
local function vox_core_sound_wood_defaults()
    return {
        footstep = {name = "vox_core_wood_footstep", gain = 0.3},
        dug = {name = "vox_core_wood_dug", gain = 1.0},
        place = {name = "vox_core_wood_place", gain = 1.0},
    }
end

local function vox_core_sound_leaves_defaults()
    return {
        footstep = {name = "vox_core_leaves_footstep", gain = 0.45},
        dug = {name = "vox_core_leaves_dug", gain = 0.7},
        place = {name = "vox_core_leaves_place", gain = 1.0},
    }
end

local function vox_core_sound_sapling_defaults()
    return {
        footstep = {name = "vox_core_sapling_footstep", gain = 0.2},
        dug = {name = "vox_core_sapling_dug", gain = 0.25},
        place = {name = "vox_core_sapling_place", gain = 1.0},
    }
end

-- Tree nodes
minetest.register_node("vox_core:tree", {
    description = S("Apple Tree"),
    tiles = {"default_tree_top.png", "default_tree_top.png", "default_tree.png"},
    paramtype2 = "facedir",
    is_ground_content = false,
    groups = {tree = 1, choppy = 2, oddly_breakable_by_hand = 1, flammable = 2},
    sounds = vox_core_sound_wood_defaults(),
    on_place = minetest.rotate_node
})

minetest.register_node("vox_core:wood", {
    description = S("Apple Wood Planks"),
    paramtype2 = "facedir",
    place_param2 = 0,
    tiles = {"default_wood.png"},
    is_ground_content = false,
    groups = {choppy = 2, oddly_breakable_by_hand = 2, flammable = 2, wood = 1},
    sounds = vox_core_sound_wood_defaults(),
})

minetest.register_node("vox_core:sapling", {
    description = S("Apple Tree Sapling"),
    drawtype = "plantlike",
    tiles = {"default_sapling.png"},
    inventory_image = "default_sapling.png",
    wield_image = "default_sapling.png",
    paramtype = "light",
    sunlight_propagates = true,
    walkable = false,
    on_timer = grow_sapling,
    selection_box = {
        type = "fixed",
        fixed = {-4 / 16, -0.5, -4 / 16, 4 / 16, 7 / 16, 4 / 16}
    },
    groups = {snappy = 2, dig_immediate = 3, flammable = 2, attached_node = 1, sapling = 1},
    sounds = vox_core_sound_sapling_defaults(),
    on_construct = function(pos)
        minetest.get_node_timer(pos):start(math.random(300, 1500))
    end,
    on_place = function(itemstack, placer, pointed_thing)
        return itemstack
    end,
})

minetest.register_node("vox_core:leaves", {
    description = S("Apple Tree Leaves"),
    drawtype = "allfaces_optional",
    waving = 1,
    tiles = {"default_leaves.png"},
    special_tiles = {"default_leaves_simple.png"},
    paramtype = "light",
    is_ground_content = false,
    groups = {snappy = 3, leafdecay = 3, flammable = 2, leaves = 1},
    drop = {
        max_items = 1,
        items = {
            {items = {"vox_core:sapling"}, rarity = 20},
            {items = {"vox_core:leaves"}}
        }
    },
    sounds = vox_core_sound_leaves_defaults(),
    after_place_node = after_place_leaves,
})

minetest.register_node("vox_core:apple", {
    description = S("Apple"),
    drawtype = "plantlike",
    tiles = {"default_apple.png"},
    inventory_image = "default_apple.png",
    paramtype = "light",
    sunlight_propagates = true,
    walkable = false,
    is_ground_content = false,
    selection_box = {
        type = "fixed",
        fixed = {-3 / 16, -7 / 16, -3 / 16, 3 / 16, 4 / 16, 3 / 16}
    },
    groups = {fleshy = 3, dig_immediate = 3, flammable = 2, leafdecay = 3, leafdecay_drop = 1, food_apple = 1},
    on_use = minetest.item_eat(2),
    sounds = vox_core_sound_leaves_defaults(),
    after_place_node = function(pos, placer, itemstack)
        minetest.set_node(pos, {name = "vox_core:apple", param2 = 1})
    end,
    after_dig_node = function(pos, oldnode, oldmetadata, digger)
        if oldnode.param2 == 0 then
            minetest.set_node(pos, {name = "vox_core:apple_mark"})
        end
    end,
})

minetest.register_node("vox_core:apple_mark", {
    description = S("Apple Marker"),
    inventory_image = "default_apple.png^default_invisible_node_overlay.png",
    wield_image = "default_apple.png^default_invisible_node_overlay.png",
    drawtype = "airlike",
    paramtype = "light",
    sunlight_propagates = true,
    walkable = false,
    pointable = false,
    diggable = false,
    buildable_to = true,
    drop = "",
    groups = {not_in_creative_inventory = 1},
    on_timer = function(pos, elapsed)
        minetest.set_node(pos, {name = "vox_core:apple"})
    end,
})