-- support for i18n
local S = armor_i18n.gettext
local F = minetest.formspec_escape
local has_technic = minetest.get_modpath("technic") ~= nil

if not minetest.global_exists("unified_inventory") then
   minetest.log("warning", S("3d_armor_ui: Mod loaded but unused."))
   return
end

if unified_inventory.sfinv_compat_layer then
   return
end

armor:register_on_update(function(player)
   local name = player:get_player_name()
   if unified_inventory.current_page[name] == "armor" then
      unified_inventory.set_inventory_formspec(player, "armor")
   end
end)

unified_inventory.register_button("armor", {
   type = "image",
   image = "inventory_plus_armor.png",
   tooltip = S("3d Armor")
})

unified_inventory.register_page("armor", {
   get_formspec = function(player, perplayer_formspec)
      local fy = perplayer_formspec.formspec_y
      local name = player:get_player_name()
      if armor.def[name].init_time == 0 then
         return {formspec="label[0,0;"..F(S("Armor not initialized!")).."]"}
      end
      local formspec = "background[0.06,"..(fy-1)..";7.92,8.52;3d_armor_ui_form.png]"..
         "label[2.15,0;"..F(S("Armor")).."]"..
         "list[detached:"..name.."_armor;armor;0,"..(fy-.75)..";1,4;]"..
         "list[detached:"..name.."_armor;armor;4,"..(fy-.75)..";1,4;4]"..
         "image[1.6,"..(fy - 0.25)..";2,4;"..armor.textures[name].preview.."]"..
         "label[5.25,"..(fy + 0.00)..";"..F(S("Heal"))..":  "..(math.floor(armor.def[name].heal))..' (Actual: '..(armor.def[name].heal)..')]'..
         "label[5.25,"..(fy + 0.25)..";"..F(S("Block"))..":  "..armor.def[name].block.."]"..
         "label[5.25,"..(fy + 0.50)..";"..F(S("DMG Resistance"))..":  "..(math.floor(armor.def[name].dmg_resist))..' (Actual: '..(armor.def[name].dmg_resist)..')]'..
         "label[5.25,"..(fy + 0.75)..";"..F(S("Fire"))..":  "..(math.floor(armor.def[name].fire))..' (Actual: '..(armor.def[name].fire)..')]'..
         "label[5.25,"..(fy + 1.00)..";"..F(S("Oxygen Regen"))..":  "..(math.floor(armor.def[name].water))..' (Actual: '..(armor.def[name].water)..')]'..
         "label[5.25,"..(fy + 1.25)..";"..F(S("Speed Multiplier"))..":  "..armor.def[name].speed.."]"..
         "label[5.25,"..(fy + 1.50)..";"..F(S("Jump Multiplier"))..":  "..armor.def[name].jump.."]"..
         "label[5.25,"..(fy + 1.75)..";"..F(S("Gravity Multiplier"))..":  "..armor.def[name].gravity.."]"..
         "listring[current_player;main]"..
         "listring[detached:"..name.."_armor;armor]"
      return {formspec=formspec}
   end,
})
