local S = core.get_translator(core.get_current_modname());
local modpath = core.get_modpath(core.get_current_modname());
local random, floor = math.random, math.floor;

-- SCHEMATICS
local apple_tree_schems = {
    { file = modpath.."/schems/trees/apple_tree_1.mts"},
    { file = modpath.."/schems/trees/apple_tree_2.mts"},
    { file = modpath.."/schems/trees/apple_tree_3.mts"},
    { file = modpath.."/schems/trees/apple_tree_4.mts"},
}

-- APPLE TREE
mcl_trees.register_wood("apple",{
    readable_name = "Apple",
    sign_color="#A1765D",
    tree_schems= apple_tree_schems,
    tree = { tiles = {"nbt_apple_tree_top.png", "nbt_apple_tree_top.png","nbt_apple_tree.png"} },
    leaves = {
        tiles = { "nbt_apple_leaves.png" },
        color = "#FFFFFF",
    },
    drop_apples = true,
    wood = { tiles = {"nbt_apple_wood.png"}},
    sapling = {
        tiles = {"nbt_apple_sapling.png"},
        inventory_image = "nbt_apple_sapling.png",
        wield_image = "nbt_apple_sapling.png",
        _after_grow=mcl_trees.sapling_add_bee_nest,
    },
    door = {
        inventory_image = "nbt_apple_door.png",
        tiles_bottom = {"nbt_apple_door_lower.png", "nbt_apple_door_side_lower.png"},
        tiles_top = {"nbt_apple_door_upper.png", "nbt_apple_door_side_upper.png"}
    },
    trapdoor = {
        tile_front = "nbt_apple_trapdoor.png",
        tile_side = "nbt_apple_trapdoor_side.png",
        wield_image = "nbt_apple_trapdoor.png",
    },
    potted_sapling = {
        image = "nbt_apple_sapling.png",
    },
})

-- APPLE FRUIT LEAF
local apple_leaves_def = core.registered_nodes["mcl_trees:leaves_apple"];

core.register_node("natural_habitat:leaves_with_fruit_apple", {
    description = apple_leaves_def["description"],
    _doc_items_longdesc = apple_leaves_def["_doc_items_longdesc"],
    tiles = {"nbt_apple_leaves.png", "default_apple.png"},
    drawtype = "mesh",
    mesh = "nbt_leaf_fruit.obj",
    paramtype = apple_leaves_def["paramtype"],
    paramtype2 = apple_leaves_def["paramtype2"],
    waving = apple_leaves_def["waving"],
    groups = apple_leaves_def["groups"],
    palette = apple_leaves_def["palette"],
    after_place_node = apple_leaves_def["after_place_node"],
    _mcl_leaves = apple_leaves_def["_mcl_leaves"],
    _mcl_orphan_leaves = apple_leaves_def["_mcl_orphan_leaves"],
    sounds = apple_leaves_def["sounds"],
        
    drop = "mcl_core:apple",
    on_rightclick = function(pos, _, clicker, itemstack)
        core.add_item(pos, "mcl_core:apple");
        core.swap_node(pos, {name = "mcl_trees:leaves_apple"});
    end,
        
    on_construct = apple_leaves_def["on_construct"],
    after_destruct = apple_leaves_def["after_destruct"],
        
    _doc_items_create_entry = apple_leaves_def["_doc_items_create_entry"],
    _mcl_shears_drop = apple_leaves_def["_mcl_shears_drop"],
    _mcl_silk_touch_drop = apple_leaves_def["_mcl_silk_touch_drop"],
});

-- ABM
core.register_abm({
    label = "Bring Fruits",
    nodenames = {"mcl_trees:leaves_apple"},
    interval = random(20, 200),
    chance = 5,
    action = function(pos, node)
        local tree_exist = core.find_node_near(pos, 3, "mcl_trees:tree_apple");
        local fruit_exist = core.find_node_near(pos, 1, "natural_habitat:leaves_with_fruit_apple");
        if (tree_exist ~= nil) and (fruit_exist == nil) then
            core.swap_node(pos, {name = "natural_habitat:leaves_with_fruit_apple"});
        end
    end
});

-- REGISTER DECORATION
for i = 1, #apple_tree_schems do
    core.register_decoration({
        deco_type = "schematic",
        place_on = {"group:grass_block_no_snow", "mcl_core:dirt"},
        sidelen = 16,
        noise_params = {
            offset = 0.0001,
            scale = 0.0013,
            spread = {x = 250, y = 250, z = 250},
            seed = 7913,
            octaves = 3,
            persist = 0.66,
        },
        biomes = {
            "Plains", "FlowerForest", "Forest", "ExtremeHills", "ExtremeHills+", 
            "SunflowerPlains", "MesaPlateauF_grasstop",
        },
        y_min = 1,
        y_max = mcl_vars.mg_overworld_max,
        schematic = apple_tree_schems[i].file,
        flags = "place_center_x, place_center_z",
        rotation = "random",
    })
end;

core.register_alias("craftoplus:leaves_with_fruit_apple", "natural_habitat:leaves_with_fruit_apple");