BINDING_NAME_FUNC_RELOADUI = "Reload UI";

local function snil(x)
  return x == nil and "" or x
end

if GetCritChance == nil then -- WoW 1.12
function GetCritChance()
  local _,_,_, numSpells = GetSpellTabInfo(1)
  for i = 1, numSpells do
   if GetSpellName(i, BOOKTYPE_SPELL) == "Attack" then
      GameTooltip:SetOwner(WorldFrame, "ANCHOR_NONE")
      GameTooltip:SetSpell(i, BOOKTYPE_SPELL)
      local tip = GameTooltipTextLeft2:GetText()
      GameTooltip:Hide()
      local _,_, capt = string.find(tip, "(%d[%d%. ]*)%%")
      if capt == nil then break; end
      return tonumber(capt)
   end
  end
  return 0
end
end

function AAL_GetBlockedAvoidance()
  local base, bonus = UnitDefense("player")
  return 5+0.04*(base+bonus - 5*UnitLevel("player")) + GetDodgeChance()+GetParryChance()+GetBlockChance()
  -- Dodge, Parry, Block "калиброваны" на уровень игрока, прибавки от Defense уже учтены
  -- из Miss, Dodge, Parry, Block вычитается по 0.6% за разницу уровней L60 vs L63 (суммарно 2.4%)
end


function AAL_UnitInfo()
  local t = "target"
  if not UnitExists(t) then t = "player" end

  SetMapToCurrentZone()
  local x,y=GetPlayerMapPosition(t)
  if x == 0 and y == 0 then x,y = GetPlayerMapPosition("player") end
  local subz = GetSubZoneText(); if subz ~= "" then subz = ", "..subz; end

  local pvp, isFFA, faction = GetZonePVPInfo()
  if pvp == "friendly" or pvp == "hostile" then
  elseif pvp == "sanctuary" then  pvp = "sanctuary"
  elseif (isFFA) then  pvp = "arena"
  else pvp = "contested"
  end

  local n = GetUnitName(t)
  local g = UnitSex(t)
  local gs = {"?g", "male", "female"}
  local lvl = UnitLevel(t); if lvl == -1 then lvl = "?" end
  local ctype = UnitCreatureType(t); if ctype == nil then ctype = "?ct" end
  local fam = UnitCreatureFamily(t); if fam == nil then fam = "" else fam = " ("..fam..")" end
  local fac = UnitFactionGroup(t); if fac == nil then fac = "--" end
  local baseArmor, armor = UnitArmor(t)
  local melMin, melMax = UnitDamage(t)
  local melSpeed = UnitAttackSpeed(t)
  local ranSpeed, ranMin, ranMax = UnitRangedDamage(t)
  local ur = UnitClassification(t)
  if UnitIsPlusMob ~= nil then
     if UnitIsPlusMob(t) then if ur == "normal" then ur=ur.."+"; end
     else if ur ~= "normal" then ur=ur.."-"; end
     end
  end

  local petInfo = ""
  if UnitIsUnit(t, "pet") then
     happiness, dmgPercentage, loyaltyRate = GetPetHappiness()
     if happiness ~= nil then
        petInfo = ", "..tostring(dmgPercentage).."% "..({"Unhappy", "Content", "Happy"})[happiness].." "..tostring(loyaltyRate)
     end
  end

  local playerData = ""
  if UnitIsUnit(t,"player") then
     local d = GetDodgeChance(); local p = GetParryChance(); local b = GetBlockChance()
     local m = AAL_GetBlockedAvoidance() - (d+p+b);
     playerData = format(", mi:%.2f do:%.2f pa:%.2f {%.2f} bl:%.2f {%.2f}, crit:%.2f", m, d, p, m+d+p, b, m+d+p+b, GetCritChance())
  end

  DEFAULT_CHAT_FRAME:AddMessage(format("%s%s, %s (%.1f, %.1f);  %s %s %s, %s %s%s, %s; %d HP, %d MP, %d ar, M %d-%d/%.1f R %d-%d/%.1f%s%s;  p:%s f:%s e:%s cc:%s ca:%s/%s C:%s P:%s c:%s",
    GetRealZoneText(), subz, pvp, x*100, y*100,
    ur, lvl, n, gs[g], ctype, fam, fac,
    UnitHealthMax(t), UnitManaMax(t), armor,
    melMin, melMax, melSpeed, ranMin, ranMax, ranSpeed,
    playerData, petInfo,
    snil(UnitInParty(t)),
    snil(UnitIsFriend(t,"player")),
    snil(UnitIsEnemy(t,"player")),
    snil(UnitCanCooperate(t,"player")),
    snil(UnitCanAttack(t,"player")), snil(UnitCanAttack("player",t)),
    snil(UnitIsCivilian and UnitIsCivilian(t) or nil),
    snil(UnitIsPVP(t)),
    snil(UnitIsConnected(t))
  ));
end

function AAL_SwitchCloakAndHelm()
  local state = (ShowingCloak() and 1 or 0) + (ShowingHelm() and 2 or 0)

  if state == 1 then ShowCloak(false); ShowHelm(true)
  elseif state ~= 3 then ShowCloak(true)
  else ShowCloak(false); ShowHelm(false)
  end
end


function AAL_ToggleChatTab(delta)
  for i=1,7 do
    local curr = getglobal("ChatFrame"..i)
    if curr == SELECTED_CHAT_FRAME then
       local k = i
       for count = 1,7 do
         k = k + delta
         if k < 1 then k = 7
         elseif k > 7 then k = 1
         end
         local new = getglobal("ChatFrame"..k)
         if new == curr then break end
         if new.isDocked ~= nil then
            curr:Hide()
            new:Show()
            SELECTED_CHAT_FRAME = new
            SELECTED_DOCK_FRAME = new
            return true
         end
       end
       break 
    end
  end
  return false
end


function AAL_Test()
  print( "=== moused:", GetMouseFocus():GetName() )
  local t = "target"
  if not UnitExists(t) then t = "player" end
  for i = 1, 100 do
    local name, rank, icon, count = UnitBuff(t, i)
    if name == nil then break end
    print("UnitBuff#", i, name, rank, icon, count)
  end
  for i = 1, 100 do
    local name, rank, icon, count = UnitDebuff(t, i)
    if name == nil then break end
    print("UnitDebuff#", i, name, rank, icon, count)
  end
end


function AAL_PetTaunt()
 for i=1, NUM_PET_ACTION_SLOTS do
   local name = GetPetActionInfo(i);
   if name == "Growl" or name == "Torment" then
      if GetPetActionCooldown(i) == 0 then CastPetAction(i); return true; end
   end
 end
 return false
end
