-- SPDX-License-Identifier: GPL-3.0-only
local modname = core.get_current_modname()
local S = core.get_translator(modname)
local setting_recipe = mcl_ruby_stuff.recipe

local valid_choices = {
    "emerald_and_redstone_dust",
    "eight_redstone_dust",
    "nine_redstone_blocks",
}

local function list_has_item(intable, value)
    if intable[value] ~= nil then return true end
    for _, v in pairs(intable) do
        if v == value then return true end
    end
    return false
end

core.log("warning","Will use setting " .. setting_recipe)
if not list_has_item(valid_choices,setting_recipe) then
    core.log("warning","Invalid choice for mcl_ruby_stuff.recipe. Please adjust settings in minetest.conf. Using default of emerald_and_redstone_dust.")
    setting_recipe = "emerald_and_redstone_dust"
end
if setting_recipe == "emerald_and_redstone_dust" then
    core.register_craft({
        output = modname .. ":ruby",
        type = "shapeless",
        recipe = { "mcl_core:emerald", "mcl_redstone:redstone" },
    })
elseif setting_recipe == "eight_redstone_dust" then
    local dust = "mcl_redstone:redstone"
    core.register_craft({
        output = modname .. ":ruby",
        type = "shapeless",
        recipe = {
            dust, dust, dust, dust,
            dust, dust, dust, dust,
        },
    })
elseif setting_recipe == "nine_redstone_blocks" then
    local block = "mcl_redstone_torch:redstoneblock"
    core.register_craft({
        output = modname .. ":ruby",
        recipe = {
            { block, block, block },
            { block, block, block },
            { block, block, block },
        },
    })
else
end

core.register_craftitem(modname .. ":ruby", {
    description = S("Ruby"),
    _doc_items_longdesc = S("Rubies can be used to craft armor, tools, and whatnot."),
    groups = { craftitem = 1 },
    inventory_image = modname .. "_ruby.png",
    _mcl_armor_trim_color = "#991b1b",
    _mcl_armor_trim_desc = S("Ruby Material"),
    _mcl_crafting_output = {square3 = {output = modname .. ":rubyblock"}}
})

core.register_node(modname .. ":rubyblock", {
    description = S("Block of Ruby"),
    _doc_items_longdesc = S("A block of ruby is mostly a shiny decorative block but also useful as a compact storage of rubies."),
    tiles = {modname .. "_rubyblock.png"},
    is_ground_content = false,
    groups = {pickaxey = 4, building_block = 1},
    sounds = mcl_sounds.node_sound_stone_defaults(),
    _mcl_blast_resistance = 6,
    _mcl_hardness = 5,
    _mcl_crafting_output = {single = {output = modname .. ":ruby 9"}}
})
