--Some way to return/remove clothes is still needed, or the player inventory can fill up.
--Shops should have infotexts set.

local hats = {}
local pants = {}
local shirts = {}
local shoes = {}
local underclothes = {}

minetest.register_on_mods_loaded(function()
   for name, def in pairs(minetest.registered_items) do
      local name = string.sub(name, 10, -1)
      local group = def.groups or {}
      local cost = def.cost or 1
      local desc = def.description or 'bug'
      if group.clothing_hats then
         hats[#hats+1] = name..'|'..cost..'|'..desc
      elseif group.clothing_pants then
         pants[#pants+1] = name..'|'..cost..'|'..desc
      elseif group.clothing_shirts then
         shirts[#shirts+1] = name..'|'..cost..'|'..desc
      elseif group.clothing_shoes then
         shoes[#shoes+1] = name..'|'..cost..'|'..desc
      elseif group.clothing_underclothes then
         underclothes[#underclothes+1] = name..'|'..cost..'|'..desc
      end
   end
end)

clothing.shop_formspec = function(type)
   local formspec =
   'formspec_version[3]'..
   'size[16,9]'..
   'textarea[1,.5;15,1;;;Browse our ever growing selection of fine clothes to adorn yourself with!]'..
   'button_exit[15,.5;.5,.5;exit;X]'..
   'button[.625,5.5;.5,.5;prev;<]'..
   'button[14.875,5.5;.5,.5;next;>]'..
   'list[current_player;clothing;.625,6.25;12,2;]'
   local count = 0
   for k,v in pairs(type) do
      count = count + 1
   end
   local offset = 0
   for i = 1, count do --Would be nice to set tooltips, or use something with descriptions.
      local name, price, desc = type[i]:match('([^|]+)|([^|]+)|([^|]+)')
      formspec = formspec..
      'item_image['..i+offset..',1;1,1;clothing:'..name..']'..
      'tooltip['..i+offset..',1;1,1;'..desc..']'..
      'button['..i+offset..',2;1,.5;'..name..';'..price..' XP]'
      offset = i*.25
   end

   return formspec
end

clothing.shop = function(player, type) -- Player object, clothing group to display
   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
      minetest.show_formspec(name, 'clothing:shop', clothing.shop_formspec(type))
   end
end

minetest.register_on_player_receive_fields(function(player, formname, fields)
   local name = player:get_player_name()
   if formname == 'clothing:shop' then
      for k,v in pairs(fields) do
         local item = 'clothing:'..k
         local price = tonumber(string.sub(v, 1, -3))
         if lobby.is_integer(price) then
            local player_inv = player:get_inventory()
            if player_inv:room_for_item('clothing', item) then
               if lobby.take_xp(player, price) then
                  player_inv:add_item('clothing', item)
               else
                  minetest.chat_send_player(name, 'Sorry, you don\'t have enough XP right now.')
               end
            end
         end
      end
   end
end)

local colbox_shop_hats_1 = {
   type = 'fixed',
   fixed = {{-.5, -.5, -.4, .5, .6125, .5}}
}

minetest.register_node('clothing:shop_hats_1', {
   description = 'Buy hats here!',
   drawtype = 'mesh',
   mesh = 'clothing_shop_hats_1.obj',
   tiles = {'clothing_shop_hats_1.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   groups = {breakable=1},
   collision_box = colbox_shop_hats_1,
   selection_box = colbox_shop_hats_1,
   on_punch = function(pos, node, puncher)
      clothing.shop(puncher, hats)
   end,
   on_rightclick = function(pos, node, clicker)
      clothing.shop(clicker, hats)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Hat Shop')
   end
})

local colbox_shop_4way = {
   type = 'fixed',
   fixed = {{-.5, -.5, -.5, .5, 1, .5}}
}

minetest.register_node('clothing:shop_shirts_1', {
   description = 'Buy shirts here!',
   drawtype = 'mesh',
   mesh = 'clothing_shop_4way.obj',
   tiles = {'clothing_shop_shirts_1.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   groups = {breakable=1},
   collision_box = colbox_shop_4way,
   selection_box = colbox_shop_4way,
   on_punch = function(pos, node, puncher)
      clothing.shop(puncher, shirts)
   end,
   on_rightclick = function(pos, node, clicker)
      clothing.shop(clicker, shirts)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Shirt Shop')
   end
})

minetest.register_node('clothing:shop_pants_1', {
   description = 'Buy pants here!',
   drawtype = 'mesh',
   mesh = 'clothing_shop_4way.obj',
   tiles = {'clothing_shop_pants_1.png'},
   use_texture_alpha = 'clip',
   paramtype = 'light',
   paramtype2 = 'facedir',
   groups = {breakable=1},
   collision_box = colbox_shop_4way,
   selection_box = colbox_shop_4way,
   on_punch = function(pos, node, puncher)
      clothing.shop(puncher, pants)
   end,
   on_rightclick = function(pos, node, clicker)
      clothing.shop(clicker, pants)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Pants Shop')
   end
})

local colbox_shop_shoes_1 = {
   type = 'fixed',
   fixed = {{-.4, -.5, -.4, .4, 1.375, .4}}
}

minetest.register_node('clothing:shop_shoes_1', {
   description = 'Buy shoes here!',
   drawtype = 'mesh',
   mesh = 'clothing_shop_shoes_1.obj',
   tiles = {'clothing_shop_shoes_1.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   groups = {breakable=1},
   collision_box = colbox_shop_shoes_1,
   selection_box = colbox_shop_shoes_1,
   on_punch = function(pos, node, puncher)
      clothing.shop(puncher, shoes)
   end,
   on_rightclick = function(pos, node, clicker)
      clothing.shop(clicker, shoes)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Shoe Shop')
   end
})

local colbox_shop_underclothes_1 = {
   type = 'fixed',
   fixed = {{-.5, -.5, -.375, .5, .75, .375}}
}

minetest.register_node('clothing:shop_underclothes_1', {
   description = 'Buy underclothes here!',
   drawtype = 'mesh',
   mesh = 'clothing_shop_underclothes_1.obj',
   tiles = {'clothing_shop_underclothes_1.png'},
   paramtype = 'light',
   paramtype2 = 'facedir',
   groups = {breakable=1},
   collision_box = colbox_shop_underclothes_1,
   selection_box = colbox_shop_underclothes_1,
   on_punch = function(pos, node, puncher)
      clothing.shop(puncher, underclothes)
   end,
   on_rightclick = function(pos, node, clicker)
      clothing.shop(clicker, underclothes)
   end,
   on_construct = function(pos)
      local meta = minetest.get_meta(pos)
      meta:set_string('infotext', 'Underclothes Shop')
   end
})
