--Variations on this task could include;
--Oranges, Turnips, Beets, Carrots, Cabbage, Pears, Bananas

local formspec_apple_good =
   'formspec_version[3]'..
   'size[10,7]'..
   'image[0,0;10,7;tasks_food_crate_apples.png]'

local function formspec_apple_bad()
   local x = math.random(10,80)/10
   local y = math.random(10,50)/10
   local formspec =
      'formspec_version[3]'..
      'size[10,7]'..
      'image[0,0;10,7;tasks_food_crate_apples.png]'..
      'image_button['..x..','..y..';1,1;tasks_food_crate_apple_rotten.png;rotten;;true;false;]'..
      'image[0,0;10,7;tasks_food_crate_apples_overlay.png]'
   return formspec
end

local formspec_carrot_good =
   'formspec_version[3]'..
   'size[10,7]'..
   'image[0,0;10,7;tasks_food_crate_carrots.png]'

local function formspec_carrot_bad()
   local x = math.random(10,80)/10
   local y = math.random(10,50)/10
   local formspec =
      'formspec_version[3]'..
      'size[10,7]'..
      'image[0,0;10,7;tasks_food_crate_carrots.png]'..
      'image_button['..x..','..y..';1,1;tasks_food_crate_carrot_rotten.png;rotten;;true;false;]'..
      'image[0,0;10,7;tasks_food_crate_carrots_overlay.png]'
   return formspec
end

local box = {
   type = 'fixed',
   fixed = {
      {-.5, -.5, -.4375, .5, .4375, .4375}}}

minetest.register_node('tasks:food_crate_apple_idle',{
   description = 'Apple Crate',
   drawtype = 'mesh',
   mesh = 'tasks_food_crate.obj',
   tiles = {'tasks_food_crate.png', 'tasks_food_crate_apples_node.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = box,
   collision_box = box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      tasks.on_construct(pos, 'Apple Crate', 'Apple Crate with Rotten Fruit')
   end,
   on_rightclick = function(pos, node, clicker)
      tasks.right_click_on(pos, node, clicker, formspec_apple_good)
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      local meta = minetest.get_meta(pos)
      local infotext = meta:get_string('info_repair')
      meta:set_string('infotext', infotext)
      minetest.swap_node(pos, {name = 'tasks:food_crate_apple_active', param2 = node.param2})
   end,
})

minetest.register_node('tasks:food_crate_apple_active',{
   description = 'Apple Crate',
   drawtype = 'mesh',
   mesh = 'tasks_food_crate.obj',
   tiles = {'tasks_food_crate.png', 'tasks_food_crate_apples_node_spoiled.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = box,
   collision_box = box,
   drop = 'tasks:food_crate_apple_idle',
   groups = {breakable = 1, tasks=1, not_in_creative_inventory=1},
   on_rightclick = function(pos, node, clicker)
      local name = clicker:get_player_name()
      local map_id = lobby.game[name]
      local sabotage_level = lobby.sabotage_level[map_id] or 5
      local meta = minetest.get_meta(pos)
      local level = meta:get_int('level') or 0
      if level < sabotage_level then
         tasks.player_config[name] = pos
         minetest.show_formspec(name, 'tasks:food_crate_apple', formspec_apple_bad())
      else
         minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
      end
   end,
})

minetest.register_node('tasks:food_crate_carrot_idle',{
   description = 'Carrot Crate',
   drawtype = 'mesh',
   mesh = 'tasks_food_crate.obj',
   tiles = {'tasks_food_crate.png', 'tasks_food_crate_carrots_node.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = box,
   collision_box = box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      tasks.on_construct(pos, 'Carrot Crate', 'Carrot Crate with Rotten Carrot')
   end,
   on_rightclick = function(pos, node, clicker)
      tasks.right_click_on(pos, node, clicker, formspec_carrot_good)
   end,
   on_timer = function(pos)
      local node = minetest.get_node(pos)
      local meta = minetest.get_meta(pos)
      local infotext = meta:get_string('info_repair')
      meta:set_string('infotext', infotext)
      minetest.swap_node(pos, {name = 'tasks:food_crate_carrot_active', param2 = node.param2})
   end,
})

minetest.register_node('tasks:food_crate_carrot_active',{
   description = 'Carrot Crate',
   drawtype = 'mesh',
   mesh = 'tasks_food_crate.obj',
   tiles = {'tasks_food_crate.png', 'tasks_food_crate_carrots_node_spoiled.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = box,
   collision_box = box,
   drop = 'tasks:food_crate_carrot_idle',
   groups = {breakable = 1, tasks=1, not_in_creative_inventory=1},
   on_rightclick = function(pos, node, clicker)
      local name = clicker:get_player_name()
      local map_id = lobby.game[name]
      local sabotage_level = lobby.sabotage_level[map_id] or 5
      local meta = minetest.get_meta(pos)
      local level = meta:get_int('level') or 0
      if level < sabotage_level then
         tasks.player_config[name] = pos
         minetest.show_formspec(name, 'tasks:food_crate_carrot', formspec_carrot_bad())
      else
         minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
      end
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'tasks:food_crate_apple'then
      if fields.rotten then
         local pos = tasks.player_config[name]
         local node = minetest.get_node(pos)
         local meta = minetest.get_meta(pos)
         local infotext = meta:get_string('info_working')
         meta:set_string('infotext', infotext)
         tasks.add_xp(pos, node, player, 'tasks:food_crate_apple_idle')
         minetest.close_formspec(name, 'tasks:food_crate_apple')
      end
   elseif formname == 'tasks:food_crate_carrot'then
      if fields.rotten then
         local pos = tasks.player_config[name]
         local node = minetest.get_node(pos)
         local meta = minetest.get_meta(pos)
         local infotext = meta:get_string('info_working')
         meta:set_string('infotext', infotext)
         tasks.add_xp(pos, node, player, 'tasks:food_crate_carrot_idle')
         minetest.close_formspec(name, 'tasks:food_crate_carrot')
      end
   end
end)
