local fdir_table = {
   {  1,  0 },
   {  0, -1 },
   { -1,  0 },
   {  0,  1 },
   {  1,  0 },
   {  0, -1 },
   { -1,  0 },
   {  0,  1 },
}

local function rack1_placement(pos)
   local node = minetest.get_node(pos)
   local nodename = node.name
   local fdir = node.param2
   local posr = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
   local posl = {x = pos.x - fdir_table[fdir+1][1], y=pos.y, z = pos.z - fdir_table[fdir+1][2]}
   local noder = minetest.get_node(posr)
   local nodel = minetest.get_node(posl)
   local nodername = noder.name
   local nodelname = nodel.name

   local style = string.sub(nodename, 26,27)
   local lpart = string.sub(nodelname, 29,29)
   local rpart = string.sub(nodername, 29,29)
   local lstyle = string.sub(nodelname, 26,27)
   local rstyle = string.sub(nodername, 26,27)
   local val = 0

   if lstyle == style then --placing to the right
      val = val + 1
      if lpart == 's' then
         minetest.set_node(posl, {name = 'furniture:pallet_racking_'..style..'_l', param2 = fdir})
      elseif lpart == 'r' then
         minetest.set_node(posl, {name = 'furniture:pallet_racking_'..style..'_m', param2 = fdir})
      end
   end
   if rstyle == style then --placing to the left
      val = val + 2
      if rpart == 's' then
         minetest.set_node(posr, {name = 'furniture:pallet_racking_'..style..'_r', param2 = fdir})
      elseif rpart == 'l' then
         minetest.set_node(posr, {name = 'furniture:pallet_racking_'..style..'_m', param2 = fdir})
      end
   end
   if val == 1 then
      minetest.set_node(pos, {name = 'furniture:pallet_racking_'..style..'_r', param2 = fdir})
   elseif val == 2 then
      minetest.set_node(pos, {name = 'furniture:pallet_racking_'..style..'_l', param2 = fdir})
   elseif val == 3 then
      minetest.set_node(pos, {name = 'furniture:pallet_racking_'..style..'_m', param2 = fdir})
   end
end

local function rack1_removal(pos)
   local node = minetest.get_node(pos)
   local meta = minetest.get_meta(pos)
   local nodename = node.name
   local fdir = node.param2
   local posr = {x = pos.x + fdir_table[fdir+1][1], y=pos.y, z = pos.z + fdir_table[fdir+1][2]}
   local posl = {x = pos.x - fdir_table[fdir+1][1], y=pos.y, z = pos.z - fdir_table[fdir+1][2]}
   local noder = minetest.get_node(posr)
   local nodel = minetest.get_node(posl)
   local nodername = noder.name
   local nodelname = nodel.name

   local style = string.sub(nodename, 26,27)
   local lpart = string.sub(nodelname, 29,29)
   local rpart = string.sub(nodername, 29,29)
   local lstyle = string.sub(nodelname, 26,27)
   local rstyle = string.sub(nodername, 26,27)

   meta:set_int('filled', 0)
   minetest.remove_node(pos)

   for _, obj in pairs (minetest.get_objects_inside_radius(pos, .5)) do
      obj:remove()
   end


   if lstyle == style then
      if lpart == 'm' then
         minetest.set_node(posl, {name = 'furniture:pallet_racking_'..style..'_r', param2 = fdir})
      elseif lpart == 'l' then
         minetest.set_node(posl, {name = 'furniture:pallet_racking_'..style..'_s', param2 = fdir})
      end
   end
   if rstyle == style then
      if rpart == 'm' then
         minetest.set_node(posr, {name = 'furniture:pallet_racking_'..style..'_l', param2 = fdir})
      elseif rpart == 'r' then
         minetest.set_node(posr, {name = 'furniture:pallet_racking_'..style..'_s', param2 = fdir})
      end
   end
end

local function racking_on_rightclick(pos, node, clicker, itemstack) --check protection
   local node = minetest.get_node(pos)
   local player_name = clicker:get_player_name()
   if not minetest.is_protected(pos, player_name) then
      local height = string.sub(node.name, 27,27)
      local meta = minetest.get_meta(pos)
      local filled = meta:get_int('filled')
      if filled == 0 then
         local item = itemstack:get_name()
         local pi = math.pi
         local pallet_height = minetest.get_item_group(item, 'pallet_height')
         if height == 's' then
            if pallet_height == 1 then
               local obj = minetest.add_entity(pos, item)
               obj:set_yaw(pi * 2 - node.param2 * pi / 2)
               meta:set_int('filled', 1)
            end
         elseif height == 't' then
            if pallet_height > 0 and pallet_height <= 2 then
               local obj = minetest.add_entity(pos, item)
               obj:set_yaw(pi * 2 - node.param2 * pi / 2)
               meta:set_int('filled', 1)
            end
         else
            minetest.chat_send_player(player_name, 'Sorry, you can\'t put that in a rack.')
      end
      else
         minetest.chat_send_player(player_name, 'This rack already has something in it.')
      end
   end
end

local s_cbox = {type = 'fixed', fixed = {{-.5, .4, -.5, .5, .5, .5},{-.5, -.5, -.5, -.4, .5, .5},{.4, -.5, -.5, .5, .5, .5}}}

minetest.register_node('furniture:pallet_racking_1s_s', {
   description = 'Pallet Racking (Single)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1s_s.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = s_cbox,
   collision_box = s_cbox,
   groups = {breakable=1},
   after_place_node = function(pos)
      rack1_placement(pos)
   end,
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

minetest.register_node('furniture:pallet_racking_1s_l', {
   description = 'Pallet Racking (left)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1s_l.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = s_cbox,
   collision_box = s_cbox,
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'furniture:pallet_racking_1s_s',
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

minetest.register_node('furniture:pallet_racking_1s_m', {
   description = 'Pallet Racking (middle)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1s_m.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = s_cbox,
   collision_box = s_cbox,
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'furniture:pallet_racking_1s_s',
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

minetest.register_node('furniture:pallet_racking_1s_r', {
   description = 'Pallet Racking (right)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1s_r.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = s_cbox,
   collision_box = s_cbox,
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'furniture:pallet_racking_1s_s',
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

local t_cbox = {type = 'fixed', fixed = {{-.5, 1.4, -.5, .5, 1.5, .5},{-.5, -.5, -.5, -.4, 1.5, .5},{.4, -.5, -.5, .5, 1.5, .5}}}

minetest.register_node('furniture:pallet_racking_1t_s', {
   description = 'Pallet Racking (Single)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1t_s.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = t_cbox,
   collision_box = t_cbox,
   groups = {breakable=1},
   after_place_node = function(pos)
      rack1_placement(pos)
   end,
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

minetest.register_node('furniture:pallet_racking_1t_l', {
   description = 'Pallet Racking (left)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1t_l.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = t_cbox,
   collision_box = t_cbox,
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'furniture:pallet_racking_1t_s',
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

minetest.register_node('furniture:pallet_racking_1t_m', {
   description = 'Pallet Racking (middle)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1t_m.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = t_cbox,
   collision_box = t_cbox,
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'furniture:pallet_racking_1t_s',
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

minetest.register_node('furniture:pallet_racking_1t_r', {
   description = 'Pallet Racking (right)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_racking_1t_r.obj',
   tiles = {'furniture_pallet_racking.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = t_cbox,
   collision_box = t_cbox,
   groups = {breakable=1, not_in_creative_inventory=1},
   drop = 'furniture:pallet_racking_1t_s',
   on_dig = function(pos)
      rack1_removal(pos)
   end,
   on_rightclick = function(pos, node, clicker, itemstack)
      racking_on_rightclick(pos, node, clicker, itemstack)
   end
})

--Entities--

local function removal(self,clicker)
   local tool = clicker:get_wielded_item():to_string()
   if tool == 'creative:tool_breaking' then
      local pos = self.object:get_pos()
      local player_name = clicker:get_player_name()
      if not minetest.is_protected(pos, player_name) then
         --check protection
         local meta = minetest.get_meta(pos)
         meta:set_int('filled', 0)
         self.object:remove()
      end
   end
end

minetest.register_entity('furniture:pallet_empty', {
   visual = 'mesh',
   mesh = 'furniture_pallet_empty.obj',
   textures = {'furniture_pallet_empty.png'},
   visual_size = {x=10, y=10},
   physical = true,
   collisionbox = {-.4375, -.5, -.4375, .4375, -.34375, .4375},
   on_rightclick = function(self,clicker)
      removal(self,clicker)
   end,
   on_punch = function(self, puncher)
      removal(self,puncher)
   end
})

minetest.register_entity('furniture:pallet_gaylord_1', {
   visual = 'mesh',
   mesh = 'furniture_pallet_gaylord_1.obj',
   textures = {'furniture_pallet_gaylord_1.png'},
   visual_size = {x=10, y=10},
   physical = true,
   collisionbox = {-.4375, -.5, -.4375, .4375, 0, .4375},
   on_rightclick = function(self,clicker)
      removal(self,clicker)
   end,
   on_punch = function(self, puncher)
      removal(self,puncher)
   end
})

minetest.register_entity('furniture:pallet_gaylord_2', {
   visual = 'mesh',
   mesh = 'furniture_pallet_gaylord_2.obj',
   textures = {'furniture_pallet_gaylord_2.png'},
   visual_size = {x=10, y=10},
   physical = true,
   collisionbox = {-.4375, -.5, -.4375, .4375, .3125, .4375},
   on_rightclick = function(self,clicker)
      removal(self,clicker)
   end,
   on_punch = function(self, puncher)
      removal(self,puncher)
   end
})

minetest.register_entity('furniture:pallet_gaylord_3', {
   visual = 'mesh',
   mesh = 'furniture_pallet_gaylord_3.obj',
   textures = {'furniture_pallet_gaylord_3.png'},
   visual_size = {x=10, y=10},
   physical = true,
   collisionbox = {-.4375, -.5, -.4375, .4375, 1.25, .4375},
   on_rightclick = function(self,clicker)
      removal(self,clicker)
   end,
   on_punch = function(self, puncher)
      removal(self,puncher)
   end
})


--Pallets--

minetest.register_node('furniture:pallet_empty', {
   description = 'Pallet (Empty)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_empty.obj',
   tiles = {'furniture_pallet_empty.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {-.4375, -.5, -.40625, .4375, -.34375, .5},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.4375, -.5, -.40625, .4375, -.34375, .5},
      },
   groups = {breakable=1, pallet_height=1}
})

minetest.register_node('furniture:pallet_gaylord_1', {
   description = 'Pallet (Short Gaylord)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_gaylord_1.obj',
   tiles = {'furniture_pallet_gaylord_1.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {{-.4375, -.5, -.40625, .4375, -.34375, .5},
               {-.40625, -.34375, -.375, .40625, 0, .46875}},
      },
   collision_box = {
      type = 'fixed',
      fixed = {{-.4375, -.5, -.40625, .4375, -.34375, .5},
               {-.40625, -.34375, -.375, .40625, 0, .46875}},
      },
   groups = {breakable=1, pallet_height=1},
})

minetest.register_node('furniture:pallet_gaylord_2', {
   description = 'Pallet (Standard Gaylord)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_gaylord_2.obj',
   tiles = {'furniture_pallet_gaylord_2.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {{-.4375, -.5, -.40625, .4375, -.34375, .5},
               {-.40625, -.34375, -.375, .40625, .3125, .46875}},
      },
   collision_box = {
      type = 'fixed',
      fixed = {{-.4375, -.5, -.40625, .4375, -.34375, .5},
               {-.40625, -.34375, -.375, .40625, .3125, .46875}},
      },
   groups = {breakable=1, pallet_height=1}
})

minetest.register_node('furniture:pallet_gaylord_3', {
   description = 'Pallet (Tall Gaylord)',
   drawtype = 'mesh',
   mesh = 'furniture_pallet_gaylord_3.obj',
   tiles = {'furniture_pallet_gaylord_3.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {{-.4375, -.5, -.40625, .4375, -.34375, .5},
               {-.40625, -.34375, -.375, .40625, 1.25, .46875}},
      },
   collision_box = {
      type = 'fixed',
      fixed = {{-.4375, -.5, -.40625, .4375, -.34375, .5},
               {-.40625, -.34375, -.375, .40625, 1.25, .46875}},
      },
   groups = {breakable=1, pallet_height=2}
})
