local trash_bin_setup =
   'size[6,3]'..
   'label[.5,.25;How much XP should be given for picking up trash?]'..
   'field[1,1.5;5,1;input;;]'..
   'button_exit[2,2;2,1;save;Submit]'

local trash_bin_formspec =
   'size[8,6]'..
   'textarea[.5,;5,2;;;Put trash in its place]' ..
   'list[current_name;trash;6,1;1,1;]'..
   'list[current_player;main;0,3;8,3;]'..
   'listring[current_player;main]'..
   'listring[current_name;trash]'

minetest.register_node('tasks:trash_bin', { --This is the node that can be placed.
   description = 'Trash Bin',
   drawtype = 'mesh',
   mesh = 'tasks_trash_bin.obj',
   tiles = {'tasks_trash_bin.png'},
   paramtype2 = 'facedir',
   groups = {breakable=1, tasks=1},
   collision_box = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.3, .4, .35, .3}}
   },
   selection_box  = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.3, .4, .35, .3}}
   },
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      inv:set_size('trash', 1)
      meta:set_string('infotext', 'Trash bin')
      meta:set_string('formspec', trash_bin_formspec)
      meta:set_int('xp', 1)
   end,
   on_receive_fields = function(pos, forname, fields, sender)
      local meta = minetest.get_meta(pos)
      if fields ['save'] then
         if not fields.input or fields.input == "" then
            return
         end
         if tasks.is_integer(fields.input) then
            meta:set_int('xp', fields.input)
            meta:set_string('infotext', 'Trash Bin')
            meta:set_string('formspec', trash_bin_formspec)
         else
            minetest.chat_send_player(sender:get_player_name(), 'Double check your input please.')
         end
      end
   end,
   on_metadata_inventory_put = function(pos, listname, index, stack, player)
      local name = player:get_player_name()
      local meta = minetest.get_meta(pos)
      local inv = meta:get_inventory()
      local input_stack = inv:get_stack(listname, 1)
      local input = input_stack:get_name()
      local item = string.sub(stack:get_name(), -7, -3)
      if item == 'trash' then
         local xp = meta:get_int('xp') or 1
         tasks.only_add_xp(xp, name)
         minetest.chat_send_player(name, 'Thanks for doing your part!')
      end
      inv:set_stack(listname, index, nil)
   end,
})

minetest.register_node('tasks:trash_1', {
   description = 'Rubbish',
   drawtype = 'mesh',
   mesh = 'decals_mesh.obj',
   tiles = {'tasks_trash_1.png'},
   use_texture_alpha = 'clip',
   inventory_image = 'tasks_trash_1.png',
   wield_image = 'tasks_trash_1.png',
   paramtype = 'light',
   paramtype2 = 'facedir',
   walkable = false,
   sunlight_propagates = true,
   buildable_to = true,
   stack_max = 1,
   groups = {breakable=1},
   selection_box  = {
      type = 'fixed',
      fixed = {{-.4, -.5, -.4, .4, -.45, .4}}
   },
   on_drop = lobby.no_drop,
   after_place_node = function(pos, placer)
      if not minetest.check_player_privs(placer, {creative = true}) then
         minetest.chat_send_player(placer:get_player_name(), 'Don\'t be a litter bug!')
         minetest.remove_node(pos)
         return true
      else
         return false
      end
   end,
   on_punch = function(pos, node, puncher, pointed_thing)
      local player_inv = puncher:get_inventory()
      if not player_inv:contains_item('main', {name='tasks:trash_1', count = 1}) then
         local timer = minetest.get_node_timer(pos)
         player_inv:add_item('main', 'tasks:trash_1')
         minetest.set_node(pos, {name = 'tasks:trash', param2 = 1})
         local time = math.random(30,600)
         timer:start(time)
      else
         minetest.chat_send_player(puncher:get_player_name(), 'You can only carry one piece of trash at a time.')
      end
   end,
})

for i = 2,8 do

   minetest.register_node('tasks:trash_'..i, {
      description = 'Rubbish',
      drawtype = 'mesh',
      mesh = 'decals_mesh.obj',
      tiles = {'tasks_trash_'..i..'.png'},
      use_texture_alpha = 'clip',
      inventory_image = 'tasks_trash_'..i..'.png',
      wield_image = 'tasks_trash_'..i..'.png',
      paramtype = 'light',
      paramtype2 = 'facedir',
      walkable = false,
      sunlight_propagates = true,
      buildable_to = true,
      stack_max = 1,
      groups = {not_in_creative_inventory=1, breakable=1},
      selection_box  = {
         type = 'fixed',
         fixed = {{-.4, -.5, -.4, .4, -.45, .4}}
      },
      on_drop = lobby.no_drop,
      after_place_node = function(pos, placer)
         if not minetest.check_player_privs(placer, {creative = true}) then
            minetest.chat_send_player(placer:get_player_name(), 'Don\'t be a litter bug!')
            minetest.remove_node(pos)
            return true
         else
            return false
         end
      end,
      on_punch = function(pos, node, puncher, pointed_thing)
         local player_inv = puncher:get_inventory()
         if not player_inv:contains_item('main', {name='tasks:trash_'..i, count = 1}) then
            local timer = minetest.get_node_timer(pos)
            player_inv:add_item('main', 'tasks:trash_'..i)
            minetest.set_node(pos, {name = 'tasks:trash', param2 = 1})
            local time = math.random(30,600)
            timer:start(time)
         else
            minetest.chat_send_player(puncher:get_player_name(), 'You can only carry one piece of trash at a time.')
         end
      end,
   })
end

minetest.register_node('tasks:trash', {
   description = 'Invisible Rubbish',
   drawtype = 'airlike',
   paramtype = 'light',
   walkable = false,
   pointable = false,
   sunlight_propagates = true,
   buildable_to = true,
   groups = {not_in_creative_inventory=1, breakable=1},
   on_timer = function(pos)
      local ran_param2 = math.random(0,3)
      local trash_ran = math.random(1,8)
      minetest.set_node(pos, {name = 'tasks:trash_'..trash_ran, param2 = ran_param2})
   end,
})
