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 =
   'formspec_version[3]'..
   'size[8,10]'..
   'bgcolor[;neither]'..
   'background[0,0;8,10;tasks_card_table_bg.png]'..
   'hypertext[0,0;8,1;nil;<global halign=center valign=middle><style color=white size=20>Pick two matching cards.\n('..tries..' tries remaining.)<style>]'..
   'hypertext[1,8.5;1,1;nil;<global halign=center valign=middle><style color=white size=35>'..val1..'<style>]'..
   'hypertext[6,8.5;1,1;nil;<global halign=center valign=middle><style color=white size=35>'..val2..'<style>]'..
   'style_type[list;size=1.5,1.5]'..
   'listcolors[#10101020;#257C4020]'..
   'list[detached:match_card_grid_'..pname..';cards;.625,1.25;4,4;]'..
   'list[detached:match_card_slot_'..pname..';slots;2.375,8.25;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
})
