minetest.register_node('clothing:hair_stylist', {
   description = 'Change your hair.',
   drawtype = 'mesh',
   mesh = 'clothing_barbers_chair.obj',
   tiles = {'clothing_barbers_chair.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   groups = {breakable=1},
   selection_box = {
      type = 'fixed',
      fixed = {-.375, -.5, -.375, .375, .625, .4375},
      },
   collision_box = {
      type = 'fixed',
      fixed = {-.375, -.5, -.375, .375, .625, .4375},
      },
   on_punch = function(pos, node, puncher)
      clothing.hair(puncher)
   end,
   on_rightclick = function(pos, node, clicker)
      clothing.hair(clicker)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Hair Styling')
   end
})

clothing.hair = function(player)
   local name = player:get_player_name()
   local wield = player:get_wielded_item()
   local wield_name = wield:get_name()
   if wield_name ~= 'creative:tool_breaking' then
      local player_attributes = player:get_meta()
      local hair = player_attributes:get_int('hair')
      clothing.temp_hair[name] = hair
      minetest.show_formspec(name, 'clothing:hair_style', clothing.hair_formspec(player, hair))
   end
end

clothing.hair_formspec = function(player, style)
   local name = player:get_player_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')
   local style = clothing.temp_hair[name]
   local skin = 'gamer_skin_'..tone..'.png^gamer_eyes_'..eyes..'.png^gamer_mouth_'..mouth..'.png'
   local formspec =
   'formspec_version[3]'..
   'size[9,9]'..
   'textarea[.5,.5;8,1;;;Express yourself with a fresh cut. Changing hair will cost 10 XP.]'..
   'model[.5,1;8,7;player model;clothing_hair_formspec.obj;'..skin..'^clothing_hair_'..style..'.png;0,0]'..
   'button_exit[8,.5;.5,.5;exit;X]'..
   'button_exit[4,8;1,.5;buy;Buy]'..
   'button[.5,8;.5,.5;prev;<]'..
   'button[8,8;.5,.5;next;>]'
   return formspec
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
   if formname == 'clothing:hair_style' then
      local name = player:get_player_name()
      local player_attributes = player:get_meta()
      local hair = clothing.temp_hair[name]
      if fields.prev then
         if hair > 1 then
            clothing.temp_hair[name] = clothing.temp_hair[name] - 1
            minetest.show_formspec(name, 'clothing:hair_style', clothing.hair_formspec(player, clothing.temp_hair[name]))
         end
      elseif fields.next then
         if hair < 3 then
            clothing.temp_hair[name] = clothing.temp_hair[name] + 1
            minetest.show_formspec(name, 'clothing:hair_style', clothing.hair_formspec(player, clothing.temp_hair[name]))
         end
      elseif fields.buy then
         if lobby.take_xp(player, 10) then
            local tone = player_attributes:get_int('tone')
            local eyes = player_attributes:get_int('eyes')
            local mouth = player_attributes:get_int('mouth')
            local skin_col = 'gamer_skin_'..tone..'.png^gamer_eyes_'..eyes..'.png^gamer_mouth_'..mouth..'.png^clothing_hair_'..hair..'.png'
            player_attributes:set_int('hair', hair)
            gamer.player_textures[name] = skin_col
            gamer.get_clothes(player)
            minetest.chat_send_player(name, 'Enjoy your new look!')
            gamer.save_skins()
         else
            minetest.chat_send_player(name, 'You need more XP!')
         end
      end
   end
end)
