doors.player_picking_answer = {}
doors.lock_pick_timer = {}

minetest.register_craftitem('doors:lock_pick', {
   description = 'Lock Picking Tool',
   inventory_image = 'doors_lock_pick.png',
   stack_max = 1,
   groups = {not_in_creative_inventory=1, stashable=1},
   on_use = function(itemstack, user, pointed_thing)
      local pos = minetest.get_pointed_thing_position(pointed_thing)
      local meta = minetest.get_meta(pos)
      local status = meta:get_int('lock_s')
      if status >= 3 then
         local name = user:get_player_name()
         doors.player_config[name] = pos
         doors.pick_lock(user, status)
      end
   end,
   on_drop = lobby.no_drop
})

for i = 0,9 do
   minetest.register_craftitem('doors:key_wedge_'..i, {
      description = 'Key Wedge',
      inventory_image = 'doors_key_wedge_'..i..'.png',
      stack_max = 1,
      groups = {not_in_creative_inventory=1}
   })
end

local wedges = {'doors:key_wedge_0', 'doors:key_wedge_1', 'doors:key_wedge_2', 'doors:key_wedge_3', 'doors:key_wedge_4', 'doors:key_wedge_5', 'doors:key_wedge_6', 'doors:key_wedge_7', 'doors:key_wedge_8', 'doors:key_wedge_9'}

local function shuffle_table(table)
   for i = #table, 2 , -1 do
      local j = math.random(i)
      table[i], table[j] = table[j], table[i]
   end
   return table
end

function doors.pick_lock(player, difficulty)
   local pname = player:get_player_name()
   local player_attributes = player:get_meta()
   local luck = player_attributes:get_int('luck')
   local grid_inv = minetest.create_detached_inventory('lock_picking_wedges_'..pname, {
      allow_take = function()
         return -1
      end,
      allow_put = function()
         return -1
      end,
   })
   grid_inv:set_size('wedges', 10)
   grid_inv:set_list('wedges', shuffle_table(wedges))
   local solution_inv = minetest.create_detached_inventory('lock_picking_solution_'..pname, {})
   solution_inv:set_size('pattern', 5)
   local timer = 20-difficulty+luck
   local sequence = math.random(11111, 99999)
   doors.player_picking_answer[pname] = sequence
   doors.lock_pick_timer[pname] = timer
   doors.lock_picking_wrapper(player, timer)
end

function doors.lock_picking_wrapper(player, timer)
   local pname = player:get_player_name()
   local timer = doors.lock_pick_timer[pname] - 1
   doors.lock_pick_timer[pname] = timer
   if timer <= 0 then
      minetest.close_formspec(pname, 'doors:lock_picking')
   elseif timer > 0 then
      minetest.show_formspec(pname, 'doors:lock_picking', doors.lock_picking_formspec(player, timer))
      local looping = minetest.after(1, function()
         doors.lock_picking_wrapper(player, timer)
      end)
   end
end

function doors.lock_picking_formspec(player, timer)
   local pname = player:get_player_name()
   local sequence = doors.player_picking_answer[pname]
   local val1 = string.sub(sequence, 1, 1)
   local val2 = string.sub(sequence, 2, 2)
   local val3 = string.sub(sequence, 3, 3)
   local val4 = string.sub(sequence, 4, 4)
   local val5 = string.sub(sequence, 5, 5)
   local formspec =
      'formspec_version[3]'..
      'size[8,8]'..
      'textarea[.5,3;7,2;;;Is breaking and entering your thing? If you don\'t break the lock is it technically breaking and entering???]'..
      'textarea[.25,.1;8,1;;;The timer is running, '..timer..' seconds remain.]'..
      'list[detached:lock_picking_wedges_'..pname..';wedges;1,.5;5,2;]'..
      'image[0.5,4.5;1,1;doors_key_profile_'..val1..'.png]'..
      'image[1.5,4.5;1,1;doors_key_profile_'..val2..'.png]'..
      'image[2.5,4.5;1,1;doors_key_profile_'..val3..'.png]'..
      'image[3.5,4.5;1,1;doors_key_profile_'..val4..'.png]'..
      'image[4.5,4.5;1,1;doors_key_profile_'..val5..'.png]'..
      'button[5.75,4.5;2,1;check;Check]'..
      'button_exit[3,7.125;2,.75;exit;Exit]'..
      'list[detached:lock_picking_solution_'..pname..';pattern;1,6;5,1;]'..
      'listring[]'
   return formspec
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'doors:lock_picking' then
      if fields.exit then
         doors.lock_pick_timer[name] = 0
      end
      if fields.check then
         local inv = minetest.get_inventory({type = 'detached', name = 'lock_picking_solution_'..name})
         local slot1 = inv:get_stack('pattern', 1)
         local key1 = string.sub(slot1:get_name(), -1)
         local slot2 = inv:get_stack('pattern', 2)
         local key2 = string.sub(slot2:get_name(), -1)
         local slot3 = inv:get_stack('pattern', 3)
         local key3 = string.sub(slot3:get_name(), -1)
         local slot4 = inv:get_stack('pattern', 4)
         local key4 = string.sub(slot4:get_name(), -1)
         local slot5 = inv:get_stack('pattern', 5)
         local key5 = string.sub(slot5:get_name(), -1)
         local solution = tonumber(key1..key2..key3..key4..key5)
         local answer = doors.player_picking_answer[name]
         if solution == answer then
            local pos = doors.player_config[name]
            minetest.chat_send_player(name, 'nice work!')
            doors.lock_pick_timer[name] = 0
            minetest.close_formspec(name, 'doors:lock_picking')
            local node = minetest.get_node(pos)
            if minetest.get_item_group(node.name, 'door') == 1 then --Standard door
               doors.door_toggle(pos, nil, player, true)
               minetest.after(3, function()
                  doors.door_toggle(pos, nil, player, true)
               end)
            else --Trapdoor
               doors.trapdoor_toggle(pos, nil, player, true)
               minetest.after(3, function()
                  doors.trapdoor_toggle(pos, nil, player, true)
               end)
            end
         else
            minetest.chat_send_player(name, 'Try again!')
         end
      end
   end
end)
