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

local pipe_box1 = nil
local pipe_box = nil
if true then
    local pw = 3/16 -- width
    local pl = 8/16 -- length
    pipe_box1 = {
        type = "fixed",
        fixed = {
            -pw, -pl, -pw,
             pw,  pw,  pw
        },
    }
    pipe_box = {
        type = "connected",
        fixed = {
            -- {
            --     -pw*2,  pw-2/16, -pw*2,
            --      pw*2,  pw,       pw*2
            -- },
            {
                -pw, -pw, -pw,
                 pw,  pw,  pw
            },
        },
        connect_top = {
            -pw,  -0, -pw,
             pw,  pl,  pw
        },
        connect_bottom = {
            -pw, -pl, -pw,
             pw,   0,  pw
        },
        connect_front = {
            -pw, -pw, -pl,
             pw,  pw,  0
        },
        connect_left = {
            -pl, -pw, -pw,
              0,  pw,  pw
        },
        connect_back = {
            -pw, -pw, -0,
             pw,  pw,  pl
        },
        connect_right = {
             -0, -pw, -pw,
             pl,  pw,  pw
        },
    }
end

local function fix_formspec(pos)
    local meta = minetest.get_meta(pos)
    local bgs = (pmb_inventory and pmb_inventory.get_itemslot_array(4.5, 0.5, 1, 1)) or ""
    meta:set_string("formspec",
        "size[24.00,12.0]"..
        ((pmb_inventory and pmb_inventory.get_itemslot_array(11.5, 5, 1, 1)) or "")..
        "list[context;main;11.5,5;1,1;]"..
        pmb_inventory.player.get_formspec(nil)
    )
    meta:set_string("infotext", "Pipe");
    local inv = meta:get_inventory()
    inv:set_size("main", 1)
end

minetest.register_node('pmb_pipes:pipe', {
    description = pmb_util.desc(S("Copper Pipe"), "blue"),
    _tt_long_desc = S("Moves items to the next pipe or any inventory with a \"main\" list it's facing."),
    groups = { dig_immediate = 3, choppy = 1, solid = 1, suffocates = 0, pipe = 1},
    tiles = {"pmb_pipes_siphon.png", "pmb_pipes_siphon.png", "pmb_pipes_pipe.png"},
    sounds = pmb_sounds.default_wood(),
    paramtype = "light",
    sunlight_propagates = true,
    paramtype2 = "facedir",
    drawtype = "nodebox",
    node_box = pipe_box,
    connects_to = { "group:pipe", "group:storage", "group:chest", "group:cooker" },
    collision_box = pipe_box1,
    selection_box = pipe_box1,
    after_place_node = function(pos, placer, itemstack, pointed_thing)
        pmb_util.rotate_and_place_against(pos, placer, itemstack, pointed_thing)
        -- tell the game what node it's pointing into
        local dir = vector.subtract(pos, pointed_thing.under)
        pmb_pipes.set_meta_vector(pos, dir)
    end,
    on_timer = function(pos, elapsed)
        local metavec = pmb_pipes.get_dir_of_node(pos)
        local to_pos = vector.multiply(metavec, -1)
        if not to_pos then return true end
        to_pos = vector.add(pos, to_pos)
        local ret = pmb_pipes.move_item_to(pos, to_pos, pmb_pipes.item_move_count)
        if ret then
            local behind_pos = vector.add(pos, metavec)
            local nt = minetest.get_node_timer(behind_pos)
            if not nt:is_started() then
                nt:start(1)
            end
        end
        return ret
    end,
    on_construct = function(pos)
        -- set up the formspec and stuff
        fix_formspec(pos)
        -- start the timer
        minetest.get_node_timer(pos):start(1.0)
    end,
        on_destruct = function(pos)
        pmb_inventory.drop_contents(pos)
    end,
    _on_node_update = function(pos)
        fix_formspec(pos)
        local nt = minetest.get_node_timer(pos)
        if not nt:is_started() then
            nt:start(1.0)
        end
    end,
    on_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
        pmb_pipes.start_timer(pos)
    end,
    on_metadata_inventory_put = function(pos, listname, index, stack, player)
        pmb_pipes.start_timer(pos)
    end,
    on_metadata_inventory_take = function(pos, listname, index, stack, player)
        pmb_pipes.start_timer(pos)
    end,
})

if true then
    local s = "pmb_items:copper_sheet"
    minetest.register_craft({
        output = "pmb_pipes:pipe 8",
        recipe = {
            {"", s, ""},
            { s, "",s},
            {"", s, ""},
        },
    })
end
