--This is provided as a super basic example of a task that only needs to be punched to gain XP.

minetest.register_node('tasks:example_0', { --This node is inactive, so somebody completed the task.
   description = 'example',
   tiles = {'task_1.png'},
   groups = {breakable=1, not_in_creative_inventory=1, tasks=1},
   light_source = 14,
   on_construct = function(pos)
      tasks.on_construct(pos, 'Sample', 'Please don\'t use me')
   end,
   on_timer = function(pos)
      local this_node = minetest.get_node(pos)
      minetest.swap_node(pos, {name = 'tasks:example_1', param2 = this_node.param2})
   end,
   on_punch = function(pos)
      local meta = minetest.get_meta(pos)
      local time_min = meta:get_int('time_min') or 60
      local time_max = meta:get_int('time_max') or 120
      local timer = minetest.get_node_timer(pos)
      local random_number = math.random(time_min, time_max)
      timer:start(random_number*2)
   end,
})

minetest.register_node('tasks:example_1', { --This node is waiting for somebody to come along and complete the task.
   description = 'example',
   tiles = {'task_1.png'},
   groups = {breakable=1, not_in_creative_inventory=1, tasks=1},
   light_source = 2,
   drop = 'tasks:example_0',
   on_punch = function(pos, node, puncher, pointed_thing)
      tasks.add_xp(pos, node, puncher, 'tasks:example_0') --Everything is pulled from the node meta expect for what node to swap to.
   end
})

local function math_clamp(val, lower, upper)
   return math.max(lower, math.min(upper, val))
end

tasks.test = {}

local function test_formspec(name, pos, x, y)
   x = math_clamp((x + math.random(-4,4)/10), 0, 9)
   y = math_clamp((y + math.random(-4,4)/10), 0, 6)
   --y = y - .2
   local formspec =
      'formspec_version[3]'..
      'size[10,7]'..
      'textarea[1,.5;8,1;;;Eww, something smells rotten! (Click the rotten food.)]'..
      'image[1,1;8,5;tasks_food_crate_apples.png]'..
      'image_button['..x..','..y..';1,1;tasks_food_crate_apple_rotten.png;rotten;;true;false;]'
   minetest.show_formspec(name, 'tasks:test', formspec)
   minetest.after(.2, function()
      if y < 0 then
         minetest.close_formspec(name, 'tasks:test')
         tasks.test[name] = 'false'
      end
      if tasks.test[name] == 'true' then
         test_formspec(name, pos, x, y)
      end
   end)
end

minetest.register_node('tasks:test', {
   description = 'testing',
   tiles = {'heart.png'},
   groups = {breakable=1, not_in_creative_inventory=1, tasks=1},
   on_rightclick = function(pos, node, clicker)
      local name = clicker:get_player_name()
      tasks.test[name] = 'true'
      test_formspec(name, pos, 5, 3.5)
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'tasks:test' then
      if fields.rotten then
         minetest.close_formspec(name, 'tasks:test')
         tasks.test[name] = 'false'
      elseif fields.quit then
         tasks.test[name] = 'false'
      end
   end
end)


--[[minetest.register_chatcommand('schem', {
   description = 'Places a block where player stands.',
   privs = {creative=true},
   func = function(name)
      local user = minetest.get_player_by_name(name)
      local pos = user:get_pos()
      minetest.place_schematic(pos, minetest.get_modpath('tasks')..'/schematics/test.mts')
   end
})
]]
