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


local tiles = {{name = "aom_lights_lantern.png^[multiply:#eee",
    backface_culling = false,
}}

local lantern_nodes = {
    ["aom_lights:lantern"] = "aom_lights:lantern_off",
    ["aom_lights:lantern_off"] = "aom_lights:lantern",
    ["aom_lights:lantern_ceiling"] = "aom_lights:lantern_off_ceiling",
    ["aom_lights:lantern_off_ceiling"] = "aom_lights:lantern_ceiling",
}

local function switch_lantern(pos)
    local node = minetest.get_node(pos)
    local ret = lantern_nodes[node.name]
    if ret then node.name = ret end
    minetest.swap_node(pos, node)
    minetest.sound_play("aom_explosives_lighter", {
        gain = 0.5,
        pos = pos,
        max_hear_distance = 7,
    })
end

for i, def in ipairs({
    {name="lantern", light=14, state="on"},
    {name="lantern_off", light=6, state="off"}}) do

    local name = def.name
    local light = def.light
    local in_menu = i == 1
    minetest.register_node("aom_lights:"..name, {
        description = S("Lantern"),
        light_source = light,
        drawtype = "mesh",
        paramtype = "light",
        sunlight_propagates = true,
        mesh = "aom_lantern.b3d",
        use_texture_alpha = "clip",
        tiles = tiles,
        walkable = false,
        drop = "aom_lights:lantern",
        -- node_placement_prediction = "", -- turns off client predict node placement
        groups = { dig_immediate = 3, deco_block = 1, player_place_only = 1, furniture = 1,
            lantern = 1, not_in_creative_inventory = (in_menu and 0) or 1, ["wire_"..def.state] = 1, },
        sounds = aom_sounds.default_metal(),
        on_place = function(itemstack, placer, pointed_thing)
            local ret = aom_util.try_rightclick(itemstack, placer, pointed_thing, false)
            if ret then
                return ret, nil
            end
            if pointed_thing and (not aom_lights.has_floor(pointed_thing.above)) and (not aom_lights.has_ceiling(pointed_thing.above)) then
                return itemstack
            else
                return minetest.item_place_node(itemstack, placer, pointed_thing)
            end
        end,
        on_construct = function(pos)
            aom_lights.fix_wall_floor_placement(pos)
        end,
        selection_box = {
            type = "fixed",
            fixed = {-3/16, -8/16, -3/16, 3/16, 0/16, 3/16},
        },
        on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
            switch_lantern(pos)
            return itemstack
        end,
        _on_node_update = aom_lights.on_node_update,
        _ceiling = "aom_lights:"..name.."_ceiling",
        _floor = "aom_lights:"..name.."",
        _no_wall = true,
        _on_wire_input = switch_lantern,
    })

    minetest.register_node("aom_lights:"..name.."_ceiling", {
        description = S("Lantern"),
        light_source = light,
        drawtype = "mesh",
        paramtype = "light",
        sunlight_propagates = true,
        mesh = "aom_lantern_ceiling.b3d",
        use_texture_alpha = "clip",
        tiles = tiles,
        drop = "aom_lights:lantern",
        walkable = false,
        groups = { dig_immediate = 3, deco_block = 1, furniture = 1, lantern = 1,
            not_in_creative_inventory = 1, ["wire_"..def.state] = 1, },
        sounds = aom_sounds.default_metal(),
        selection_box = {
            type = "fixed",
            fixed = {-3/16, -8/16, -3/16, 3/16, 0/16, 3/16},
        },
        on_rightclick = function(pos, node, clicker, itemstack, pointed_thing)
            switch_lantern(pos)
            return itemstack
        end,
        _on_node_update = aom_lights.on_node_update,
        _ceiling = "aom_lights:"..name.."_ceiling",
        _floor = "aom_lights:"..name.."",
        _no_wall = true,
        _on_wire_input = switch_lantern,
    })
end

if minetest.get_modpath("aom_tcraft") then
    aom_tcraft.register_craft({
        output = "aom_lights:lantern 4",
        items = {
            ["aom_items:iron_sheet"] = 1,
            ["aom_lights:torch"] = 4,
        }
    })
end
