local function dying_formspec(water_level)
   formspec =
   'size[8,8.5]'..
   'list[current_name;dye;.5,0;1,1;]'..
   'label[1.5,0.25;Input dye]'..
   'list[current_name;salt;.5,1;1,1;]'..
   'label[1.5,1.25;Input salt]'..
   'list[current_name;fabric;.5,2;1,1;]'..
   'label[1.5,2.25;Input fabric/thread]'..
   'list[current_name;water;.5,3;1,1;]'..
   'label[1.5,3.25;Input water]'..
   'list[current_name;output;6.5,0;1,4;]'..
   'image[4.5,.5;1,3;station_dye_bg.png^[lowpart:'..(water_level*10)..':station_dye_fg.png]'..
   'list[current_player;main;0,4.5;8,4;]'..
   'listring[context;output]'..
   'listring[current_player;main]'
   return formspec
end

minetest.register_node('stations:dying', {
   description = 'Dying Station',
   drawtype = 'mesh',
   mesh = 'stations_dying.obj',
   tiles = {'stations_dying.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1.5, .3, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1.5, .3, .5}},
   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('dye', 1)
      inv:set_size('salt', 1)
      inv:set_size('fabric', 1)
      inv:set_size('water', 1)
      inv:set_size('output', 4)
      meta:set_string('water_level', 0)
      meta:set_string('infotext', 'Dying Station')
      meta:set_string('formspec', dying_formspec(0))
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('dye') and inv:is_empty('salt') and inv:is_empty('fabric')
      and inv:is_empty('water') 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 == 'dye' then
         if minetest.get_item_group(input, 'dye') > 0  and input ~= 'dye:white' then
            return 99
         else
            return 0
         end
      elseif listname == 'salt' then
         if input == 'epic:salt' then
            return 99
         else
            return 0
         end
      elseif listname == 'fabric' then
         if input == 'furniture:fabric_white' or input == 'furniture:thread_white' then
            return 99
         else
            return 0
         end
      elseif listname == 'water' then
         if input == 'bucket:bucket_river_water' or input == 'bucket:bucket_water'
         or input == 'earthbuild:clay_pot_river_water' or input == 'earthbuild:clay_pot_water' then
            return 1
         else
            return 0
         end
      elseif listname == 'output' then
         return 0
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local meta = minetest.get_meta(pos)
      local input = stack:get_name()
      local inv = meta:get_inventory()
      local timer = minetest.get_node_timer(pos)
      timer:start(3)
      if listname == 'water' then
         meta:set_string('formspec', dying_formspec(10))
         meta:set_string('water_level', 10)
         local vessel = string.sub(input, 1,3)
         if vessel == 'ear' then
            inv:set_stack('water', 1, 'earthbuild:clay_pot')
         elseif vessel == 'buc' then
            inv:set_stack('water', 1, 'bucket:bucket_empty')
         end
      end
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local water_level = tonumber(meta:get_string('water_level'))
      if water_level > 0 then
         local inv = meta:get_inventory()
         local input_dye = inv:get_stack('dye', 1)
         local input_salt = inv:get_stack('salt', 1)
         local input_fabric = inv:get_stack('fabric', 1)
         local dye = input_dye:get_count()
         local salt = input_salt:get_count()
         local fabric = input_fabric:get_count()
         if dye > 0 and salt > 0 and fabric > 0 then --Let's dye things
            local dye_name = input_dye:get_name()
            local color = string.sub(dye_name, 5,-1)
            local fabric_name = input_fabric:get_name()
            local material = string.sub(fabric_name, 11,-7)
            inv:add_item('output', 'furniture:'..material..'_'..color)
            meta:set_string('water_level', (water_level-1))
            meta:set_string('formspec', dying_formspec(water_level-1))
            input_dye:take_item(1)
            input_salt:take_item(1)
            input_fabric:take_item(1)
            inv:set_stack('dye',1,input_dye)
            inv:set_stack('salt',1,input_salt)
            inv:set_stack('fabric',1,input_fabric)
            timer:start(3)
         end
      end
   end,
   on_rotate = function(pos, node)
      return false
   end,
})

minetest.register_node('stations:dying_locked', {
   description = 'Dying Station (locked)',
   drawtype = 'mesh',
   mesh = 'stations_dying.obj',
   tiles = {'stations_dying.png'},
   sounds = default.node_sound_wood_defaults(),
   paramtype2 = 'facedir',
   paramtype = 'light',
   selection_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1.5, .3, .5}},
   collision_box = {
      type = 'fixed',
      fixed = {-.5, -.5, -.5, 1.5, .3, .5}},
   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('dye', 1)
      inv:set_size('salt', 1)
      inv:set_size('fabric', 1)
      inv:set_size('water', 1)
      inv:set_size('output', 4)
      meta:set_string('water_level', 0)
      meta:set_string('infotext', 'Dying Station (locked)')
      meta:set_string('formspec', dying_formspec(0))
   end,
   can_dig = function(pos,player)
      local meta = minetest.get_meta(pos);
      local inv = meta:get_inventory()
      if inv:is_empty('dye') and inv:is_empty('salt') and inv:is_empty('fabric')
      and inv:is_empty('water') 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 == 'dye' then
            if minetest.get_item_group(input, 'dye') > 0  and input ~= 'dye:white' then
               return 99
            else
               return 0
            end
         elseif listname == 'salt' then
            if input == 'epic:salt' then
               return 99
            else
               return 0
            end
         elseif listname == 'fabric' then
            if input == 'furniture:fabric_white' or input == 'furniture:thread_white' then
               return 99
            else
               return 0
            end
         elseif listname == 'water' then
            if input == 'bucket:bucket_river_water' or input == 'bucket:bucket_water'
            or input == 'earthbuild:clay_pot_river_water' or input == 'earthbuild:clay_pot_water' then
               return 1
            else
               return 0
            end
         elseif listname == 'output' then
            return 0
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local meta = minetest.get_meta(pos)
      local input = stack:get_name()
      local inv = meta:get_inventory()
      local timer = minetest.get_node_timer(pos)
      timer:start(3)
      if listname == 'water' then
         meta:set_string('formspec', dying_formspec(10))
         meta:set_string('water_level', 10)
         local vessel = string.sub(input, 1,3)
         if vessel == 'ear' then
            inv:set_stack('water', 1, 'earthbuild:clay_pot')
         elseif vessel == 'buc' then
            inv:set_stack('water', 1, 'bucket:bucket_empty')
         end
      end
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local water_level = tonumber(meta:get_string('water_level'))
      if water_level > 0 then
         local inv = meta:get_inventory()
         local input_dye = inv:get_stack('dye', 1)
         local input_salt = inv:get_stack('salt', 1)
         local input_fabric = inv:get_stack('fabric', 1)
         local dye = input_dye:get_count()
         local salt = input_salt:get_count()
         local fabric = input_fabric:get_count()
         if dye > 0 and salt > 0 and fabric > 0 then --Let's dye things
            local dye_name = input_dye:get_name()
            local color = string.sub(dye_name, 5,-1)
            local fabric_name = input_fabric:get_name()
            local material = string.sub(fabric_name, 11,-7)
            inv:add_item('output', 'furniture:'..material..'_'..color)
            meta:set_string('water_level', (water_level-1))
            meta:set_string('formspec', dying_formspec(water_level-1))
            input_dye:take_item(1)
            input_salt:take_item(1)
            input_fabric:take_item(1)
            inv:set_stack('dye',1,input_dye)
            inv:set_stack('salt',1,input_salt)
            inv:set_stack('fabric',1,input_fabric)
            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,
})
