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

local formspec_good =
   'formspec_version[3]'..
   'size[12,6]'..
   'image[0,0;12,6;tasks_beehive_ui_bg.png]'

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

beehive = {}

local function beehive_formspec(name, wx, wy, b1x, b1y, b2x, b2y)
   wx = math_clamp((wx + math.random(-4,4)/10), 0, 11)
   wy = math_clamp((wy + math.random(-4,4)/10), 0, 5)
   b1x = math_clamp((b1x + math.random(-4,4)/10), 0, 11)
   b1y = math_clamp((b1y + math.random(-4,4)/10), 0, 5)
   b2x = math_clamp((b2x + math.random(-4,4)/10), 0, 11)
   b2y = math_clamp((b2y + math.random(-4,4)/10), 0, 5)
   local formspec =
      'formspec_version[3]'..
      'size[12,6]'..
      'image[0,0;12,6;tasks_beehive_ui_bg.png]'..
      'image_button['..wx..','..wy..';1,1;tasks_beehive_ui_wasp.png;thief;;true;false]'..
      'image_button['..b1x..','..b1y..';1,1;tasks_beehive_ui_bee.png;bee;;true;false]'..
      'image_button['..b2x..','..b2y..';1,1;tasks_beehive_ui_bee.png;bee;;true;false]'
   minetest.show_formspec(name, 'tasks:beehive_bad', formspec)
   minetest.after(.25, function()
      if beehive[name] == 'true' then
         beehive_formspec(name, wx, wy, b1x, b1y, b2x, b2y)
      end
   end)
end

minetest.register_node('tasks:beehive',{
   description = 'Beehive',
   drawtype = 'mesh',
   mesh = 'tasks_beehive.obj',
   tiles = {'tasks_beehive.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   selection_box = box,
   collision_box = box,
   groups = {breakable = 1, tasks=1},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Beehive')
      meta:set_string('info_working', 'Beehive')
      meta:set_string('info_repair', 'Distressed Beehive')
      meta:set_string('status', 'good')
      meta:set_int('time_min', 30)
      meta:set_int('time_max', 90)
      meta:set_int('xp', 5)
      meta:set_int('level', 0)
   end,
   on_rightclick = function(pos, node, clicker)
      local name = clicker:get_player_name()
      local timer = minetest.get_node_timer(pos)
      local meta = minetest.get_meta(pos)
      local min = meta:get_int('time_min') or 30
      local max = meta:get_int('time_max') or 60
      local status = meta:get_string('status')
      local random_number = math.random(min,max)
      timer:start(random_number)
      local map_id = lobby.game[name]
      local sabotage_level = lobby.sabotage_level[map_id] or 5
      local level = meta:get_int('level') or 0
      if level < sabotage_level then
         if status == 'good' then
            minetest.show_formspec(name, 'tasks:beehive_good', formspec_good)
         else
            tasks.player_config[name] = pos
            beehive[name] = 'true'
            beehive_formspec(name, 6, 3, 2, 4, 9, 1)
         end
      else
         minetest.chat_send_player(name, 'level is currently sabotaged, and you can\'t do this now.')
      end
   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)
      meta:set_string('status', 'bad')
   end,
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'tasks:beehive_bad'then
      if fields.thief then
         beehive[name] = 'false'
         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')
         local xp = meta:get_int('xp')
         meta:set_string('status', 'good')
         meta:set_string('infotext', infotext)
         tasks.only_add_xp(xp, name)
         minetest.close_formspec(name, 'tasks:beehive_bad')
         minetest.chat_send_player(name, 'Nice going, the queen thanks you!')
      elseif fields.bee then
         beehive[name] = 'false'
         minetest.close_formspec(name, 'tasks:beehive_bad')
         minetest.chat_send_player(name, 'Careful, you just killed a honeybee.')
      elseif fields.quit then
         beehive[name] = 'false'
      end
   end
end)
