local function set_wear(itemstack, level, max_level)
   local temp
   if level == 0 then
      temp = 0
   else
      temp = 65536 - math.floor(level / max_level * 65535)
      if temp > 65535 then temp = 65535 end
      if temp < 1 then temp = 1 end
   end
   itemstack:set_wear(temp)
end

local function get_wear(itemstack)
   if itemstack:get_metadata() == "" then
      return 30
   else
      return tonumber(itemstack:get_metadata())
   end
end

local stain_table = {
   {'Golden Oak', '1', 32, '#cc7431'},
   {'Country Pine', '2', 64, '#b76126'},
   {'Cinnamon', '3', 96, '#9c4a1b'},
   {'Cherry', '4', 128, '#873a14'},
   {'Mahogany', '5', 160, '#712b0d'},
   {'Walnut', '6', 192, '#5b1e07'},
   {'Black', '7', 224, '#461404'}
}

for i in ipairs (stain_table) do
   local desc = stain_table[i][1]
   local name = stain_table[i][2]
   local indx = stain_table[i][3]
   local colo = stain_table[i][4]

   minetest.register_tool('furniture:stain_brush'..name, {
      description = desc..' Stain brush',
      inventory_image = 'furniture_brush.png^(furniture_brush_overlay.png^[colorize:'..colo..':255)',
      stack_max = 1,
      wear_represents = 'content_level',
      groups = {stain_brush=1},
      on_use = function(itemstack, user, pointed_thing)
         if pointed_thing.type ~= 'node' then return end
         local player_name = user:get_player_name()
         local pos = minetest.get_pointed_thing_position(pointed_thing)
         if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(user, 'protection_bypass') then
            return
         end
         local node = minetest.get_node(pos)
         if minetest.get_item_group(node.name, 'stainable') > 0 then
         --local mod = string.sub(node.name, 1, 9)
         --if mod == 'furniture' then
            local fdir = node.param2 % 32
            local stain = get_wear(itemstack)
            if stain == 0 then
               itemstack:set_name('furniture:brush')
            return itemstack end
            if node.param2 == (fdir + indx) then
               local player = user:get_player_name()
               minetest.chat_send_player(player, "Already stained this color.")
               return
            elseif node.param2 >= (fdir + indx) then
               local player = user:get_player_name()
               minetest.chat_send_player(player, "You can only stain things darker.")
               return end
            minetest.swap_node(pos, {name = node.name, param2 = fdir + indx})
            stain = stain - 1
            itemstack:set_metadata(tostring(stain))
            set_wear(itemstack, stain, 30)
            return itemstack
         else
            local player = user:get_player_name()
            minetest.chat_send_player(player, "You can't stain that item.")
         end
      end,
   })
end

minetest.register_tool('furniture:sanding', {
   description = 'Sanding pad',
   inventory_image = 'furniture_sanding.png',
   stack_max = 1,
   wear_represents = 'content_level',
   on_use = function(itemstack, user, pointed_thing)
      if pointed_thing.type ~= 'node' then return end
      local player_name = user:get_player_name()
      local pos = minetest.get_pointed_thing_position(pointed_thing)
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(user, 'protection_bypass') then
         return
      end
      local node = minetest.get_node(pos)
      if minetest.get_item_group(node.name, 'stainable') > 0 then
      --local mod = string.sub(node.name, 1, 9)
      --if mod == 'furniture' then
         local fdir = node.param2 % 32
         if node.param2 == fdir then
            local player = user:get_player_name()
            minetest.chat_send_player(player, "Already sanded.")
            return end
         minetest.swap_node(pos, {name = node.name, param2 = fdir})
         itemstack:add_wear(65535 / 48)
         return itemstack
      else
         local player = user:get_player_name()
         minetest.chat_send_player(player, "You can't sand that item.")
      end
   end,
})
