local function formspec_shovel(pos)
   local spos = pos.x .. "," .. pos.y .. "," .. pos.z
   local formspec =
   'formspec_version[3]'..
   'size[10.25,9]'..
   'textarea[1.5,.25;8.5,3.5;;;You need to dig for water. '..
   'Depending on where you are you may need to dig for a while to find water. '..
   'Put a shovel in the slot to start digging. '..
   '(The shovel will dig through all nodes until water is found.)]'..
   'list[nodemeta:'..spos..';bucket;.25,.25;1,1;]'..
   'list[current_player;main;.25,4;8,4;]'..
   'listring[]'
   return formspec
end

local function formspec_water(pos, depth)
   local spos = pos.x .. "," .. pos.y .. "," .. pos.z
   local formspec =
   'formspec_version[3]'..
   'size[10.25,9]'..
   'textarea[1.5,.25;8.5,3.5;;;Fantastic! water has been discovered, only '..depth..
   ' nodes below the well. This means it will take about '..(depth/2)..
   ' seconds to get a bucket of water! (The bucket will be filled automatically)]'..
   'list[nodemeta:'..spos..';bucket;.25,.25;1,1;]'..
   'list[current_player;main;.25,4;8,4;]'..
   'listring[]'
   return formspec
end

local biome_table = {
   ['rainforest'] = {min=5,max=15},
   ['grassland'] = {min=7,max=20},
   ['savanna'] = {min=15,max=45},
   ['deciduous_forest'] = {min=10,max=30},
   ['coniferous_forest'] = {min=12,max=25},
   ['coniferous_forest_dunes'] = {min=12,max=25},
   ['tundra'] = {min=15,max=20},
   ['icesheet'] = {min=21,max=25},
   ['taiga'] = {min=8,max=19},
   ['snowy_grassland'] = {min=7,max=20},
   ['grassland_dunes'] = {min=7,max=20},
}

minetest.register_craftitem('furniture:well_top', {
   description = 'Top of well',
   inventory_image = 'furniture_well_top.png'
})

minetest.register_craft({
   output = 'furniture:well',
   recipe = {
      {'default:stone', 'default:stone', 'default:stone'},
      {'default:stone', 'furniture:well_top', 'default:stone'},
      {'default:stone', 'default:stone', 'default:stone'}
   }
})

minetest.register_node('furniture:well', {
   description = 'Well',
   drawtype = 'mesh',
   mesh = 'furniture_well.obj',
   tiles = {'furniture_well.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = {
      type = 'fixed',
      fixed = {{-.6875, -.5, .5, .6875, .125, .6875},
               {.5, -.5, -.6875, .6875, .125, .6875},
               {-.6875, -.5, -.6875, -.5, .125, .6875},
               {-.6875, -.5, -.6875, .6875, .125, -.5},
               {-.6875, .75, -.9375, .6875, 1.375, .9375}}
      },
   collision_box = {
      type = 'fixed',
      fixed = {{-.6875, -.5, .5, .6875, .125, .6875},
               {.5, -.5, -.6875, .6875, .125, .6875},
               {-.6875, -.5, -.6875, -.5, .125, .6875},
               {-.6875, -.5, -.6875, .6875, .125, -.5},
               {-.6875, .75, -.9375, .6875, 1.375, .9375}}
      },
   groups = {cracky=2},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('bucket', 1)
      meta:set_int('water', 0)
      meta:set_string('working', 'false')
      meta:set_string('infotext', 'Dig for water')
      local biome_data = minetest.get_biome_data(pos)
      local biome_id = biome_data.biome
      local biome_name = minetest.get_biome_name(biome_id)
      meta:set_string('biome', biome_name)
   end,
   can_dig = function(pos,player)
      local inv = minetest.get_meta(pos):get_inventory()
      return inv:is_empty('bucket')
   end,
   allow_metadata_inventory_put = function(pos, listname, index, stack, player)
      local input = stack:get_name()
      local meta = minetest.get_meta(pos)
      local working = meta:get_string('working')
      if listname == 'bucket' then
         if input == 'bucket:bucket_empty' then
            if working == 'false' then
               return 0
            else
               return 1
            end
         elseif minetest.get_item_group(input, 'shovel') > 0 then
            if working == 'true' then
               return 0
            else
               return 1
            end
         else
            return 0
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local meta = minetest.get_meta(pos)
      local item = stack:get_name()
      local timer = minetest.get_node_timer(pos)
      if item == 'bucket:bucket_empty' then
         meta:set_string('infotext', 'Getting water...')
         local inv = meta:get_inventory()
         local bucket = inv:get_stack('bucket', 1)
         local water_depth = meta:get_int('water')
         timer:start(water_depth/2)
      else
         meta:set_string('infotext', 'Digging well...')
         timer:start(1)
      end
   end,
   on_rightclick = function(pos, node, clicker)
      local name = clicker:get_player_name()
      local meta = minetest.get_meta(pos)
      local working = meta:get_string('working')
      if working == 'false' then
         minetest.show_formspec(name, 'furniture:well', formspec_shovel(pos))
      else
         local depth = meta:get_int('water')
         minetest.show_formspec(name, 'furniture:well', formspec_water(pos, depth))
      end
   end,
   on_metadata_inventory_take = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      timer:stop()
      meta:set_string('infotext', 'Ready to use.')
   end,
   on_timer = function(pos)
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local working = meta:get_string('working')
      local biome = meta:get_string('biome')
      if working == 'true' then --get water
         local inv = meta:get_inventory()
         inv:set_stack('bucket', 1, 'bucket:bucket_river_water')
         meta:set_string('infotext', 'Ready to use.')
      else
         local inv = meta:get_inventory()
         local shovel = inv:get_stack('bucket', 1):get_name()
         if minetest.get_item_group(shovel, 'shovel') > 0 then
            if biome_table[biome] then
               local min = biome_table[biome].min or 50
               local max = biome_table[biome].max or 100
               local chance = math.random(min, max)
               local depth = meta:get_int('water')
               local below = depth + 1
               if below < chance then
                  local pos2 = {x=pos.x, y=pos.y-below, z=pos.z}
                  --pos:offset(0, -below, 0)
                  minetest.remove_node(pos2)
                  meta:set_int('water', below)
                  timer:start(1)
               else
                  meta:set_string('working', 'true')
                  meta:set_string('infotext', 'Ready to use.')
               end
            end
         end
      end
   end,
})
