local this = class("starHealingManager")

function this:ctor()
end

function this:initialize()
  L_UI:addListener(L_UI.pageEvent.closed, self.onUICloseEvent, self)
  L_BattleManager:addListener(L_BattleManager.event.exitBattle, self.onExitBattle, self)
end

function this:dispose()
  L_UI:removeListener(L_UI.pageEvent.closed, self.onUICloseEvent, self)
  L_BattleManager:removeListener(L_BattleManager.event.exitBattle, self.onExitBattle)
end

function this:onExitBattle(args)
  if args and args.finishType == C_EBattlerServerFinishType.ForceOut then
    L_StarHealingStore:triggerStarHealing()
  end
end

function this:onUICloseEvent(pageName)
  if pageName == "pageBattleSettle" or pageName == "pageBattleReward" or pageName == "pageBattleFailed" then
    L_StarHealingStore:triggerStarHealing()
  end
end

function this:checkIsCityValid()
  local worldCityTpl = L_GameTpl:getWorldCityTpl()
  local sceneId = AzurWorldInstance.CurWorldId
  if math.isEmpty(sceneId) then
    return false
  end
  local tpl = worldCityTpl:getTplById(sceneId)
  if tpl == nil then
    return false
  end
  return math.isEmpty(worldCityTpl:getCharge(tpl))
end

function this:checkHealingValid()
  local bFull = true
  local formationType = L_FormationManager:getCurFormationType()
  local posData = L_FormationStore:getFormationPosData(formationType)
  for _, heroId in pairs(posData) do
    if not math.isEmpty(heroId) then
      local maxHp = L_AttributeManager:getAttValueByHeroGuid(heroId, L_Const.battleAtt.MAXHP)
      local curHp = L_FormationStore:getFormationHeroHp(heroId)
      if maxHp > curHp then
        bFull = false
        break
      end
    end
  end
  return bFull
end

function this:getStarHealingFoods()
  local allItem = L_BagStore:getAllItem()
  local commonItemTpl = L_GameTpl:getCommonItemTpl()
  local foodTpl = L_GameTpl:getFoodTpl()
  local all = {}
  for _, v in pairs(allItem) do
    local tpl = commonItemTpl:getTplById(v.itemID)
    if not tpl then
      errorf("commonItem\232\161\168\230\178\161\230\156\137Id: " .. v.itemID)
    else
      local type = commonItemTpl:getType(tpl)
      local useType = commonItemTpl:getUseType(tpl)
      if type == L_BagConst.bagItemType.food and useType == L_BagConst.bagUseType.starHealing then
        local fTpl = foodTpl:getTplById(v.itemID)
        if not fTpl then
          errorf("itemId: " .. v.itemID .. " subId: " .. v.itemID .. " \229\156\168food\232\161\168\233\135\140\233\157\162\230\137\190\228\184\141\229\136\176")
        else
          table.insert(all, {
            itemId = v.itemID,
            itemNum = v.itemNum,
            itemType = L_Const.resType.commonItem,
            itemSubId = v.itemID,
            charge = foodTpl:getCharge(fTpl)
          })
        end
      end
    end
  end
  table.sort(all, function(a, b)
    return a.charge > b.charge
  end)
  return all
end

function this:checkTannsportRecoverValid()
  local heros = L_HeroStore:getAllHero()
  for _, hero in pairs(heros) do
    local guid = L_HeroStore:getHeroGuid(hero)
    local heroInfo = L_BattleStore:getObjBattleInfo(guid)
    if heroInfo == nil then
      return false
    end
    local hp_max = L_AttributeManager:getAttValueByHeroGuid(guid, L_Const.battleAtt.MAXHP)
    if hp_max > heroInfo.hp then
      return false
    end
  end
  return true
end

return this
