for i = 1,8 do
   minetest.register_craftitem('tasks:card_'..i, {
      description = 'Playing Card',
      inventory_image = 'tasks_playing_card.png',
      stack_max = 1,
      groups = {not_in_creative_inventory=1}
   })
end

local function match_formspec(pname, tries, card1, card2)
   local val1 = string.sub(card1, -1)
   local val2 = string.sub(card2, -1)
   local formspec =
   'size[8,6]'..
   'textarea[4.5,.25;3,1;;;Pick two matching cards.\n('..tries..' tries remaining.)]'..
   'label[4.75,3;'..val1..']'..
   'label[5.75,3;'..val2..']'..
   'list[detached:match_card_grid_'..pname..';cards;0,0;4,4;]'..
   'list[detached:match_card_slot_'..pname..';slots;4.5,2;2,1;]'
   return formspec
end

local deck = {'tasks:card_1', 'tasks:card_2', 'tasks:card_3', 'tasks:card_4', 'tasks:card_5', 'tasks:card_6', 'tasks:card_7', 'tasks:card_8',
               'tasks:card_1', 'tasks:card_2', 'tasks:card_3', 'tasks:card_4', 'tasks:card_5', 'tasks:card_6', 'tasks:card_7', 'tasks:card_8'}

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

local function new_game(pos, player)
   local player_attributes = player:get_meta()
   local luck = player_attributes:get_int('luck')
   local tries = (luck) + 6
   local pname = player:get_player_name()
   local meta = minetest.get_meta(pos)
   local xp = meta:get_int('xp')
   local grid_inv = minetest.create_detached_inventory('match_card_grid_'..pname, {})
   grid_inv:set_size('cards', 16)
   grid_inv:set_list('cards', shuffle_deck(deck))
   local slot_inv = minetest.create_detached_inventory('match_card_slot_'..pname, {
      on_put = function(inv, listname, index, stack, player)
         tries = tries - 1
         if tries == 0 then
            new_game(pos, player)
         else
            local inv = minetest.get_inventory({type = 'detached', name = 'match_card_slot_'..pname})
            local slot1 = inv:get_stack('slots', 1)
            local slot2 = inv:get_stack('slots', 2)
            local card1 = slot1:get_name()
            local card2 = slot2:get_name()
            minetest.show_formspec(pname, 'tasks:card_matching', match_formspec(pname, tries, card1, card2))
            if card1 == card2 then
               minetest.chat_send_player(pname, 'Nice work')
               tasks.only_add_xp(xp, pname)
               new_game(pos, player)
            end
         end
      end,
   })
   slot_inv:set_size('slots', 2)
   minetest.show_formspec(pname, 'tasks:card_matching', match_formspec(pname, tries, '?', '?'))
end

minetest.register_node('tasks:match', {
   description = 'Matching Game',
   drawtype = 'mesh',
   mesh = 'tasks_card_table.obj',
   tiles = {'tasks_card_table.png'},
   paramtype2 = 'facedir',
   groups = {breakable=1, tasks=1},
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Matching')
      meta:set_int('xp', 5)
   end,
   on_rightclick = function(pos, node, clicker)
      new_game(pos, clicker)
   end
})
