gamer.temp_tone = {}
gamer.temp_eyes = {}
gamer.temp_mouth = {}

local function skin_selection(tone, eyes, mouth)
   local formspec =
      'formspec_version[3]'..
      'size[9,7]'..
      'textarea[4,.5;4.5,2;;;Use the arrows to adjust switch between tones/colors. Initial setup is free, changes thereafter cost 500XP.]'..
      'model[.5,.5;3,6;player model;gamer_model.b3d;gamer_skin_'..tone..'.png^gamer_eyes_'..eyes..'.png^gamer_mouth_'..mouth..'.png,clothing_briefs.png,blank.png,blank.png;0,180]'..
      'button[4,2.75;1,.5;skin_sub;<]'..
      'button[5,2.75;1,.5;skin_add;>]'..
      'label[6.5,3;Skin Tone]'..
      'button[4,3.75;1,.5;eyes_sub;<]'..
      'button[5,3.75;1,.5;eyes_add;>]'..
      'label[6.5,4;Eyes]'..
      'button[4,4.75;1,.5;mouth_sub;<]'..
      'button[5,4.75;1,.5;mouth_add;>]'..
      'label[6.5,5;Mouth]'..
      'button_exit[4,5.5;2,1;exit;Close]'..
      'button_exit[6.5,5.5;2,1;save;Save]'
   return formspec
end

minetest.register_chatcommand('skin', {
   description = 'Change your skin.',
   func = function(name)
      local player = minetest.get_player_by_name(name)
      local player_attributes = player:get_meta()
      local tone = player_attributes:get_int('tone')
      local eyes = player_attributes:get_int('eyes')
      local mouth = player_attributes:get_int('mouth')
      if tone == 0 then
         gamer.temp_tone[name] = 4
      else
         gamer.temp_tone[name] = tone
      end
      if eyes == 0 then
         gamer.temp_eyes[name] = 1
      else
         gamer.temp_eyes[name] = eyes
      end
      if mouth == 0 then
         gamer.temp_mouth[name] = 1
      else
         gamer.temp_mouth[name] = mouth
      end
      minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
   end
})

minetest.register_on_player_receive_fields(function(player, formname, fields)
   if formname == 'gamer:skin_selection' then
      local name = player:get_player_name()
      local player_attributes = player:get_meta()
      local tone = gamer.temp_tone[name]
      local eyes = gamer.temp_eyes[name]
      local mouth = gamer.temp_mouth[name]
      if fields.skin_sub then
         if tone > 1 then
            gamer.temp_tone[name] = gamer.temp_tone[name] - 1
            minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
         end
      elseif fields.skin_add then
         if tone < 7 then
            gamer.temp_tone[name] = gamer.temp_tone[name] + 1
            minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
         end
      elseif fields.eyes_sub then
         if eyes > 1 then
            gamer.temp_eyes[name] = gamer.temp_eyes[name] - 1
            minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
         end
      elseif fields.eyes_add then
         if eyes < 6 then
            gamer.temp_eyes[name] = gamer.temp_eyes[name] + 1
            minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
         end
      elseif fields.mouth_sub then
         if mouth > 1 then
            gamer.temp_mouth[name] = gamer.temp_mouth[name] - 1
            minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
         end
      elseif fields.mouth_add then
         if mouth < 8 then
            gamer.temp_mouth[name] = gamer.temp_mouth[name] + 1
            minetest.show_formspec(name, 'gamer:skin_selection', skin_selection(gamer.temp_tone[name], gamer.temp_eyes[name], gamer.temp_mouth[name]))
         end
      elseif fields.save then
         local old_tone = player_attributes:get_int('tone')
         if old_tone ~= 0 then
            if lobby.take_xp(player, 500) then
               local tone = gamer.temp_tone[name]
               local eyes = gamer.temp_eyes[name]
               local mouth = gamer.temp_mouth[name]
               local skin_col = 'gamer_skin_'..tone..'.png^gamer_eyes_'..eyes..'.png^gamer_mouth_'..mouth..'.png'
               player_attributes:set_int('tone', tone)
               player_attributes:set_int('eyes', eyes)
               player_attributes:set_int('mouth', mouth)
               gamer.player_textures[name] = skin_col
               gamer.get_clothes(player)
               minetest.chat_send_player(name, 'Skin tone updated!')
               gamer.save_skins()
            else
               minetest.chat_send_player(name, 'You need more XP!')
            end
         else
            local tone = gamer.temp_tone[name]
            local eyes = gamer.temp_eyes[name]
            local mouth = gamer.temp_mouth[name]
            local skin_col = 'gamer_skin_'..tone..'.png^gamer_eyes_'..eyes..'.png^gamer_mouth_'..mouth..'.png'
            player_attributes:set_int('tone', tone)
            player_attributes:set_int('eyes', eyes)
            player_attributes:set_int('mouth', mouth)
            gamer.player_textures[name] = skin_col
            gamer.get_clothes(player)
            minetest.chat_send_player(name, 'Skin tone updated!')
            gamer.save_skins()
         end
      end
   end
end)
