local function loom_formspec(progress)
   formspec =
   'size[8,8.5]'..
   'list[current_name;input;.5,1;1,1;]'..
   'label[1.5,1.5;Input string]'..
   'list[current_name;output;.5,3;1,1;]'..
   'label[1.5,3.5;Take fabric]'..
   'image[5,1;2,2.375;furniture_fabric_bg.png^[lowpart:'..(progress*20)..':furniture_fabric_fg.png]'..
   'list[current_player;main;0,4.5;8,4;]'..
   'listring[current_player;main]'..
   'listring[context;input]'..
   'listring[context;output]'..
   'listring[current_player;main]'
   return formspec
end

minetest.register_node('stations:weaving_loom', {
   description = 'Weaving Loom',
   drawtype = 'mesh',
   mesh = 'stations_weaving_loom.obj',
   tiles = {'stations_weaving_loom.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {
         {-.5, -.5, -.15, 1.5, 1.5, .15},
         }
      },
   collision_box = {
      type = 'fixed',
      fixed = {
         {-.5, -.5, -.15, 1.5, 1.5, .15},
         }
      },
   groups = {oddly_breakable_by_hand = 1, choppy=3},
   after_place_node = function(pos, placer, itemstack)
      if not epic.space_to_side(pos) then
         minetest.remove_node(pos)
         return itemstack
      end
   end,
   after_dig_node = function(pos, oldnode, oldmetadata, digger)
      epic.remove_side_node(pos, oldnode)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('main', 8*4)
      inv:set_size('input', 1)
      inv:set_size('output', 1)
      meta:set_string('progress', 0)
      meta:set_string('infotext', 'Weaving Loom')
      meta:set_string('formspec', loom_formspec(0))
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('input') and inv:is_empty('output') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local input = stack:get_name()
      if listname == 'input' then
         if input == 'farming:string' then
            return 99
         else
            return 0
         end
      elseif listname == 'output' then
         return 0
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local timer = minetest.get_node_timer(pos)
      timer:start(5)
   end,
   on_metadata_inventory_take = function(pos, listname, index, stack, player)
      if listname == 'output' then
         local meta = minetest.get_meta(pos)
         local progress = tonumber(meta:get_string('progress'))
         if progress == 0 then
            meta:set_string('infotext', 'Weaving Loom')
            meta:set_string('formspec', loom_formspec(0))
         end
      end
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local progress = tonumber(meta:get_string('progress'))
      local inv = meta:get_inventory()
      local input = inv:get_stack('input', 1)
      if input:get_name() == 'farming:string' then
         input:take_item()
         inv:set_stack('input',1,input)
         meta:set_string('progress', (progress+1))
         meta:set_string('infotext', 'Weaving Loom ('..((progress+1)*20)..'%)')
         meta:set_string('formspec', loom_formspec(progress+1))
         timer:start(5)
         if progress >= 4 then
            inv:add_item('output', 'furniture:fabric_white')
            meta:set_string('progress', 0)
            meta:set_string('formspec', loom_formspec(0))
         end
      end
   end,
   on_rotate = function(pos, node)
      return false
   end,
})

minetest.register_node('stations:weaving_loom_locked', {
   description = 'Weaving Loom (locked)',
   drawtype = 'mesh',
   mesh = 'stations_weaving_loom.obj',
   tiles = {'stations_weaving_loom.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {
         {-.5, -.5, -.15, 1.5, 1.5, .15},
         }
      },
   collision_box = {
      type = 'fixed',
      fixed = {
         {-.5, -.5, -.15, 1.5, 1.5, .15},
         }
      },
   groups = {oddly_breakable_by_hand = 1, choppy=3},
   after_place_node = function(pos, placer, itemstack)
      if not epic.space_to_side(pos) then
         minetest.remove_node(pos)
         return itemstack
      end
   end,
   after_dig_node = function(pos, oldnode, oldmetadata, digger)
      epic.remove_side_node(pos, oldnode)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('main', 8*4)
      inv:set_size('input', 1)
      inv:set_size('output', 1)
      meta:set_string('progress', 0)
      meta:set_string('infotext', 'Weaving Loom (locked)')
      meta:set_string('formspec', loom_formspec(0))
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('input') and inv:is_empty('output') then
         return true
      else
         return false
      end
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local player_name = player:get_player_name()
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
         return 0
      else
         local input = stack:get_name()
         if listname == 'input' then
            if input == 'farming:string' then
               return 99
            else
               return 0
            end
         elseif listname == 'output' then
            return 0
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local timer = minetest.get_node_timer(pos)
      timer:start(5)
   end,
   allow_metadata_inventory_take = function(pos, listname, index, stack, player)
      local player_name = player:get_player_name()
      if minetest.is_protected(pos, player_name) and not minetest.check_player_privs(player, 'protection_bypass') then
         return 0
      else
         return 99
      end
   end,
   on_metadata_inventory_take = function(pos, listname, index, stack, player)
      if listname == 'output' then
         local meta = minetest.get_meta(pos)
         local progress = tonumber(meta:get_string('progress'))
         if progress == 0 then
            meta:set_string('infotext', 'Weaving Loom')
            meta:set_string('formspec', loom_formspec(0))
         end
      end
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local progress = tonumber(meta:get_string('progress'))
      local inv = meta:get_inventory()
      local input = inv:get_stack('input', 1)
      if input:get_name() == 'farming:string' then
         input:take_item()
         inv:set_stack('input',1,input)
         meta:set_string('progress', (progress+1))
         meta:set_string('infotext', 'Weaving Loom ('..((progress+1)*20)..'%)')
         meta:set_string('formspec', loom_formspec(progress+1))
         timer:start(5)
         if progress >= 4 then
            inv:add_item('output', 'furniture:fabric_white')
            meta:set_string('progress', 0)
            meta:set_string('formspec', loom_formspec(0))
         end
      end
   end,
   on_rotate = function(pos, node)
      return false
   end,
})
