stations.grains = {}
stations.grains['farming:barley'] = true
stations.grains['farming:corn'] = true
stations.grains['farming:oat'] = true
stations.grains['farming:rice'] = true
stations.grains['farming:rye'] = true
stations.grains['farming:wheat'] = true

local function threshing_formspec(pos)
   local meta = minetest.get_meta(pos)
   local speed = meta:get_string('speed')
   local formspec =
   'size[8,8.5]'..
   'list[current_name;input;.5,.5;1,1;]'..
   'list[current_name;output;6,1;2,3;]'..
   'list[current_name;straw;2.5,3;3,1]'..
   'list[current_player;main;0,4.5;8,4;]'..
   'label[2,.25;Break grain from the stems, your choice, slow or fast.]'..
   'label[2,.75;Currently running at '..speed..' speed.]'..
   'button[0,2.5;2,1;slow;Slow]'..
   'button[0,3.25;2,1;fast;Fast]'..
   'listring[context;output]'..
   'listring[current_player;main]'..
   'listring[context;input]'
   return formspec
end

minetest.register_node('stations:threshing', {
   description = 'Threshing Machine',
   drawtype = 'mesh',
   mesh = 'stations_threshing.obj',
   tiles = {'stations_threshing.png'},
   use_texture_alpha = 'opaque',
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
   groups = {oddly_breakable_by_hand=3, choppy=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('input', 1)
      inv:set_size('output', 6)
      inv:set_size('straw', 3)
      meta:set_string('speed', 'slow')
      meta:set_string('infotext', 'Threshing Machine')
      meta:set_string('formspec', threshing_formspec(pos))
   end,
   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,
   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 stations.grains[input] then
            return 99
         else
            return 0
         end
         return 0
      else
         return 0
      end
   end,
   on_receive_fields = function(pos, formname, fields, sender)
      local meta = minetest.get_meta(pos)
      local timer = minetest.get_node_timer(pos)
      if fields ['slow'] then
         meta:set_string('speed', 'slow')
         meta:set_string('formspec', threshing_formspec(pos))
         timer:start(10)
      elseif fields ['fast'] then
         meta:set_string('speed', 'fast')
         meta:set_string('formspec', threshing_formspec(pos))
         timer:start(3)
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local speed = meta:get_string('speed')
      if speed == 'slow' then
         timer:start(10)
      elseif speed == 'fast' then
         timer:start(3)
      end
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local input = inv:get_stack('input', 1)
      local input_count = input:get_count()
      if input_count >= 1 then
         local output = inv:get_stack('output', 1)
         local grain_name = input:get_name()
         local grain = string.sub(grain_name, 9,-1)
         local speed = meta:get_string('speed')
         if speed == 'slow' then
            local num = math.random(3, 7)
            inv:add_item('output', 'farming:seed_'..grain..' '..num)
            inv:add_item('straw', 'farming:hay')
            input:take_item(1)
            inv:set_stack('input',1,input)
            timer:start(10)
         elseif speed == 'fast' then
            local num = math.random(1, 3)
            inv:add_item('output', 'farming:seed_'..grain..' '..num)
            inv:add_item('straw', 'farming:hay')
            input:take_item(1)
            inv:set_stack('input',1,input)
            timer:start(3)
         end
      end
   end,
   on_rotate = function(pos, node)
      return false
   end,
})

minetest.register_node('stations:threshing_locked', {
   description = 'Threshing Machine (locked)',
   drawtype = 'mesh',
   mesh = 'stations_threshing.obj',
   tiles = {'stations_threshing.png'},
   use_texture_alpha = 'opaque',
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.45, -.5, -.5, 1.25, .5, .5}},
   groups = {oddly_breakable_by_hand=3, choppy=3},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('input', 1)
      inv:set_size('output', 6)
      inv:set_size('straw', 3)
      meta:set_string('speed', 'slow')
      meta:set_string('infotext', 'Threshing Machine (locked)')
      meta:set_string('formspec', threshing_formspec(pos))
   end,
   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,
   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 stations.grains[input] then
               return 99
            else
               return 0
            end
            return 0
         else
            return 0
         end
      end
   end,
   on_receive_fields = function(pos, formname, fields, sender)
      local meta = minetest.get_meta(pos)
      local timer = minetest.get_node_timer(pos)
      if fields ['slow'] then
         meta:set_string('speed', 'slow')
         meta:set_string('formspec', threshing_formspec(pos))
         timer:start(10)
      elseif fields ['fast'] then
         meta:set_string('speed', 'fast')
         meta:set_string('formspec', threshing_formspec(pos))
         timer:start(3)
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local speed = meta:get_string('speed')
      if speed == 'slow' then
         timer:start(10)
      elseif speed == 'fast' then
         timer:start(3)
      end
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local input = inv:get_stack('input', 1)
      local input_count = input:get_count()
      if input_count >= 1 then
         local output = inv:get_stack('output', 1)
         local grain_name = input:get_name()
         local grain = string.sub(grain_name, 9,-1)
         local speed = meta:get_string('speed')
         if speed == 'slow' then
            local num = math.random(3, 7)
            inv:add_item('output', 'farming:seed_'..grain..' '..num)
            inv:add_item('straw', 'farming:hay')
            input:take_item(1)
            inv:set_stack('input',1,input)
            timer:start(10)
         elseif speed == 'fast' then
            local num = math.random(1, 3)
            inv:add_item('output', 'farming:seed_'..grain..' '..num)
            inv:add_item('straw', 'farming:hay')
            input:take_item(1)
            inv:set_stack('input',1,input)
            timer:start(3)
         end
      end
   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_rotate = function(pos, node)
      return false
   end,
})
