unified_inventory.register_craft_type('churn', {
   description = 'Butter Churn',
   icon = 'stations_churn_icon.png',
   width = 1,
   height = 1
})

unified_inventory.register_craft({
   type = 'churn',
   items = {'mobs:bucket_milk'},
   output = 'food:butter 4'
})

local function milk_formspec(pos)
   local meta = minetest.get_meta(pos)
   local milk_level = tonumber(meta:get_string('milk'))
   local formspec =
   'size[8,8.5]'..
   'list[current_name;input;.5,0;1,1;]'..
   'label[1.5,0.25;Input Milk]'..
   'list[current_name;output;.5,2.5;1,1;]'..
   'label[1.5,2.75;Take Butter]'..
   'list[current_player;main;0,4.5;8,4;]'..
   'image[5,.5;1,3;stations_stain_bg.png^[lowpart:'..(milk_level*12.5)..':stations_churn_fg.png]'..
   'listring[context;output]'..
   'listring[current_player;main]'..
   'listring[context;input]'
   return formspec
end


minetest.register_node('stations:churn', {
   description = 'Butter Churn',
   drawtype = 'mesh',
   mesh = 'stations_churn.obj',
   tiles = {'stations_churn.png'},
   use_texture_alpha = 'opaque',
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {{-.3, -.5, -.3, .3, .3, .3},
               {-.1, .3, -.1, .1, 1, .1}}},
   collision_box = {
      type = 'fixed',
      fixed = {{-.3, -.5, -.3, .3, .3, .3},
               {-.1, .3, -.1, .1, 1, .1}}},
   groups = {oddly_breakable_by_hand = 1, 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', 1)
      meta:set_string('infotext', 'Butter Churn')
      meta:set_string('milk', 0)
      meta:set_string('formspec', milk_formspec(pos))
   end,
   after_place_node = function(pos, placer, itemstack)
      if not epic.space_on_top(pos) then
         minetest.remove_node(pos)
         return itemstack
      end
   end,
   after_dig_node = function(pos, oldnode, oldmetadata, digger)
      epic.remove_top_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 input == 'mobs:bucket_milk' 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 input = stack:get_name()
      if listname == 'input' then
         if input == 'mobs:bucket_milk' then
            local meta = minetest.get_meta(pos)
            local inv = meta:get_inventory()
            local milk_level = tonumber(meta:get_string('milk'))
            if milk_level + 4 <= 8 then
               local timer = minetest.get_node_timer(pos)
               inv:set_stack('input', 1, 'bucket:bucket_empty')
               meta:set_string('milk', (milk_level + 4))
               timer:start(10)
               meta:set_string('formspec', milk_formspec(pos))
               meta:set_string('infotext', 'Making Butter')
            end
         end
      end
   end,
   on_timer = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local milk_level = tonumber(meta:get_string('milk'))
      if milk_level >= 1 then
         local timer = minetest.get_node_timer(pos)
         inv:add_item('output', 'food:butter')
         meta:set_string('milk', (milk_level - 1))
         timer:start(10)
         meta:set_string('formspec', milk_formspec(pos))
         meta:set_string('infotext', 'Making Butter')
      else
         meta:set_string('infotext', 'Butter Churn')
      end
   end,
})

minetest.register_node('stations:churn_locked', {
   description = 'Butter Churn (locked)',
   drawtype = 'mesh',
   mesh = 'stations_churn.obj',
   tiles = {'stations_churn.png'},
   use_texture_alpha = 'opaque',
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {{-.3, -.5, -.3, .3, .3, .3},
               {-.1, .3, -.1, .1, 1, .1}}},
   collision_box = {
      type = 'fixed',
      fixed = {{-.3, -.5, -.3, .3, .3, .3},
               {-.1, .3, -.1, .1, 1, .1}}},
   groups = {oddly_breakable_by_hand = 1, 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', 1)
      meta:set_string('infotext', 'Butter Churn (locked)')
      meta:set_string('milk', 0)
      meta:set_string('formspec', milk_formspec(pos))
   end,
   after_place_node = function(pos, placer, itemstack)
      if not epic.space_on_top(pos) then
         minetest.remove_node(pos)
         return itemstack
      end
   end,
   after_dig_node = function(pos, oldnode, oldmetadata, digger)
      epic.remove_top_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 input == 'mobs:bucket_milk' 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 input = stack:get_name()
      if listname == 'input' then
         if input == 'mobs:bucket_milk' then
            local meta = minetest.get_meta(pos)
            local inv = meta:get_inventory()
            local milk_level = tonumber(meta:get_string('milk'))
            if milk_level + 4 <= 8 then
               local timer = minetest.get_node_timer(pos)
               inv:set_stack('input', 1, 'bucket:bucket_empty')
               meta:set_string('milk', (milk_level + 4))
               timer:start(10)
               meta:set_string('formspec', milk_formspec(pos))
               meta:set_string('infotext', 'Making Butter')
            end
         end
      end
   end,
   on_timer = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local milk_level = tonumber(meta:get_string('milk'))
      if milk_level >= 1 then
         local timer = minetest.get_node_timer(pos)
         inv:add_item('output', 'food:butter')
         meta:set_string('milk', (milk_level - 1))
         timer:start(10)
         meta:set_string('formspec', milk_formspec(pos))
         meta:set_string('infotext', 'Making Butter')
      else
         meta:set_string('infotext', 'Butter Churn')
      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,
})
