local this = {}

function this:init()
  this.super.init(self)
end

function this:getFormationDataByType(fType)
  return AzurWorld.formationMgr:GetFormationGroupByType(fType)
end

function this:getFormationTeam(fType, formationIndex)
  formationIndex = formationIndex or self:getUsingTeamIndex(fType)
  return AzurWorld.formationMgr:GetFormation(fType, formationIndex)
end

function this:getUsingTeamIndex(fType)
  return AzurWorld.formationMgr:GetFormation(fType).index
end

function this:getControl(fType)
  return AzurWorld.formationMgr:GetFormation(fType).mainControl
end

function this:getFormationPosData(fType, formationIndex)
  formationIndex = formationIndex or self:getUsingTeamIndex(fType)
  local formation = self:getFormationTeam(fType, formationIndex)
  if not formation then
    return {
      0,
      0,
      0
    }
  end
  local posData = formation.posData
  local res = {}
  for i = 0, posData.Count - 1 do
    if i < 3 or i == 3 and posData[3] ~= 0 then
      table.insert(res, posData[i])
    end
  end
  return res
end

function this:getTeamHeroCount(formationType, formationIndex)
  formationIndex = formationIndex or self:getUsingTeamIndex(formationType)
  return AzurWorld.formationMgr:GetFormationHeroCount(formationType, formationIndex)
end

function this:getFormationName(formationType, formationIndex)
  formationIndex = formationIndex or self:getUsingTeamIndex(formationType)
  local formation = self:getFormationTeam(formationType, formationIndex)
  if not formation or formation and string.isEmpty(formation.formationName) then
    return L_WordsTpl:getValue(string.format("ui_formaiton_default_name_%s", formationIndex))
  end
  return formation.formationName
end

function this:getHeroPlaceInTeam(formationType, formationIndex, heorId)
  formationIndex = formationIndex or self:getUsingTeamIndex(formationType)
  return AzurWorld.formationMgr:GetHeroPlaceInFormation(formationType, formationIndex, heorId) + 1
end

function this:getPetPlaceInTeam(formationType, formationIndex, petGuid)
  formationIndex = formationIndex or self:getUsingTeamIndex(formationType)
  return AzurWorld.formationMgr:GetPetPlaceInFormation(formationType, formationIndex, petGuid) + 1
end

function this:getCurTeamValidHeroes(formationType)
  local posData = self:getFormationPosData(formationType)
  local res = {}
  for i, guid in ipairs(posData) do
    if not math.isEmpty(guid) then
      table.insert(res, guid)
    end
  end
  return res
end

function this:getFormationHeroHp(heroId)
  local hp_max = L_AttributeManager:getAttValueByHeroGuid(heroId, L_Const.battleAtt.MAXHP)
  local heroInfo = L_BattleStore:getObjBattleInfo(heroId)
  return heroInfo and heroInfo.hp or hp_max
end

function this:getFormationPosPetGuid(formationType, formationIndex, pos)
  return AzurWorld.formationMgr:GetPetByFormationPosIndex(formationType, formationIndex, pos - 1)
end

function this:getIsTrailFormation(formationType)
  return self:getUsingTeamIndex(formationType) == 0
end

function this:checkIsFullFormation(formationType)
  formationType = formationType or L_FormationManager:getCurFormationType()
  local heroGuidList = self:getCurTeamValidHeroes(formationType)
  return 3 <= #heroGuidList
end

function this:getAverageHeroLevelOfCurFormation(formationType)
  formationType = formationType or L_FormationManager:getCurFormationType()
  local heroGuidList = self:getCurTeamValidHeroes(formationType)
  local averageHeroLevel = 0
  local sumOfHeroLevel = 0
  for _, heroGuid in pairs(heroGuidList) do
    local hero = L_HeroStore:getHero(heroGuid)
    local heroLevel = L_HeroStore:getHeroLevel(hero)
    sumOfHeroLevel = sumOfHeroLevel + heroLevel
  end
  averageHeroLevel = sumOfHeroLevel / #heroGuidList
  return averageHeroLevel
end

return this
