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 = "pmb_lights_lantern.png^[multiply:#eee",
    backface_culling = false,
}}

local lantern_nodes = {
    ["pmb_lights:lantern"] = "pmb_lights:lantern_off",
    ["pmb_lights:lantern_off"] = "pmb_lights:lantern",
    ["pmb_lights:lantern_ceiling"] = "pmb_lights:lantern_off_ceiling",
    ["pmb_lights:lantern_off_ceiling"] = "pmb_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("pmb_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 = "pmb_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 = pmb_sounds.default_metal(),
        on_place = function(itemstack, placer, pointed_thing)
            local ret = pmb_util.try_rightclick(itemstack, placer, pointed_thing, false)
            if ret then
                return ret, nil
            end
            if pointed_thing and (not pmb_lights.has_floor(pointed_thing.above)) and (not pmb_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)
            pmb_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 = pmb_lights.on_node_update,
        _ceiling = "pmb_lights:"..name.."_ceiling",
        _floor = "pmb_lights:"..name.."",
        _no_wall = true,
        _on_wire_input = switch_lantern,
    })

    minetest.register_node("pmb_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 = "pmb_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 = pmb_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 = pmb_lights.on_node_update,
        _ceiling = "pmb_lights:"..name.."_ceiling",
        _floor = "pmb_lights:"..name.."",
        _no_wall = true,
        _on_wire_input = switch_lantern,
    })
end

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