local this = class("cellBattleHero", G_UIModuleBase)
local PetRedColor = C_Color(0.94, 0.37, 0.3)
local PetGreenColor = C_Color(0.16, 0.94, 0.71)
local DeadHeroColor = C_Color(0.5, 0.5, 0.5)

function this.bind()
  return {
    imgPlayer = nil,
    color_hero = C_Color.white,
    sliderHp = 0,
    sliderSp = 0,
    goDead = false,
    goPet = false,
    slider_petHp = 0,
    img_pet = "",
    goChange = false,
    color_petIcon = C_Color.white,
    color_petColor = PetRedColor,
    goPerReborn = false,
    moduleHeroHeadEffect = {
      moduleName = "pages/battle/moduleCellHeroHeadEffect"
    },
    moduleBuff = {
      moduleName = "pages/battle/moduleBattleBuff"
    },
    ani_spMax = "",
    active_spMax = false
  }
end

function this.methods()
  return {
    onClick_head = function(self)
      self:emit("onClick", self.bind)
    end
  }
end

function this:open()
  self.timer = Timer.repeated(0.1, self.update, self)
  self.bind.goDead = false
  self.modules.moduleHeroHeadEffect:initChoice()
  self:refresh()
  self:playSelectAnim()
  L_PlayerManager:addListener(L_PlayerManager.event.setMainControl, self.onEvent_setMainControl, self)
end

function this:close()
  L_PlayerManager:removeListener(L_PlayerManager.event.setMainControl, self.onEvent_setMainControl)
  if self.timer then
    Timer.remove(self.timer)
    self.timer = nil
  end
  self.bind.heroId = nil
  self.oldMainId = nil
end

function this:playSelectAnim()
  local id = self.bind.heroId
  local controlId = L_BattleTeamStore:getControlId()
  self.modules.moduleHeroHeadEffect:setChoiceEffect(controlId == id, true)
  self.oldMainId = controlId
end

function this:onEvent_setMainControl()
  if math.isEmpty(self.bind.heroId) then
    return
  end
  local mainHero = L_PlayerManager:getMainControlEntity()
  if L_BattleDataManager:getIsInBattle() and L_BattleDataManager:getIsInCD() then
    if mainHero:getId() == self.bind.heroId then
      self.modules.moduleHeroHeadEffect:setChoiceEffect(true)
    else
      if self.oldMainId == self.bind.heroId then
        self.modules.moduleHeroHeadEffect:setChoiceEffect(false)
      end
      self.modules.moduleHeroHeadEffect:doChangeCdAnim(true)
    end
  elseif mainHero:getId() == self.bind.heroId then
    self.modules.moduleHeroHeadEffect:setChoiceEffect(true)
  elseif self.oldMainId == self.bind.heroId then
    self.modules.moduleHeroHeadEffect:setChoiceEffect(false)
  end
  self.oldMainId = mainHero:getId()
end

function this:refresh()
  self.modules.moduleHeroHeadEffect:initHeroId(self.bind.heroId)
  if L_PlayerManager:getMainControlEntity() == nil then
    return
  end
  if math.isEmpty(self.bind.heroId) then
    return
  end
  local id = self.bind.heroId
  local hero = L_ItemManager:parseHeroItem(L_GameUtil.getConfigIdByGuid(self.bind.heroId))
  self.bind.imgPlayer = hero.iconHalf
  local hero = L_PlayerManager:getPlayerUnit():getHero(self.bind.heroId)
  if hero == nil then
    return
  end
  self.modules.moduleBuff:initBuff(hero:getCsEntity(), 5, 5, true)
  if hero == nil then
    self.bind.goDead = true
    self.modules.moduleHeroHeadEffect:refreshDead()
    self.bind.color_hero = DeadHeroColor
    return
  end
  local petId
  local dic = L_BattleTeamStore:getBattleTeamInfo()
  for i, v in pairs(dic) do
    if v.heroId == id then
      petId = v.petId
      break
    end
  end
  if petId then
    self.bind.goPet = true
    local tpl = L_GameTpl:getPetTpl():getTplById(petId)
    local icon = L_GameTpl:getPetTpl():getPetIcon(tpl)
    self.bind.img_pet = icon
  else
    self.bind.goPet = false
  end
end

function this:update()
  if math.isEmpty(self.bind.heroId) then
    return
  end
  local csHeroEntity = L_PlayerManager:getPlayerUnit():getHeroEntityByPosIndex_cs(self.bind.index - 1)
  if csHeroEntity == nil then
    self.bind.sliderSp = 0
    self.bind.sliderHp = 0
    self.bind.goDead = true
    self.modules.moduleHeroHeadEffect:refreshDead()
    self.bind.color_hero = DeadHeroColor
    self:checkSpEffect()
    return
  end
  self.modules.moduleHeroHeadEffect:refreshDead()
  self.bind.goDead = false
  self.bind.color_hero = C_Color.white
  self.bind.sliderSp = 0.05 + 0.95 * C_MyFloatUtility.GetFloat(csHeroEntity.data.aliveProperty.sp) / C_MyFloatUtility.GetFloat(csHeroEntity.data.aliveProperty.maxSp)
  self:checkSpEffect()
  self.bind.sliderHp = C_MyFloatUtility.GetFloat(csHeroEntity.data.aliveProperty.hp) / C_MyFloatUtility.GetFloat(csHeroEntity.data.aliveProperty.maxHp)
  local petCsEntity = L_PlayerManager:getPlayerUnit():getPetEntityByPosIndex_cs(self.bind.index - 1)
  if petCsEntity then
    local aliveProperty = petCsEntity.data.aliveProperty
    self.bind.slider_petHp = C_MyFloatUtility.GetFloat(aliveProperty.hp) / C_MyFloatUtility.GetFloat(aliveProperty.maxHp)
    if petCsEntity.data.aliveState == CS.Lens.Gameplay.Modules.BigWorld.EEntityAliveState.Reborn then
      self.bind.goPerReborn = true
      self.bind.color_petIcon = DeadHeroColor
      self.bind.color_petColor = PetGreenColor
    else
      self.bind.goPerReborn = false
      self.bind.color_petIcon = C_Color.white
      self.bind.color_petColor = PetRedColor
    end
  end
end

function this:isDead()
  return self.bind.goDead
end

function this:checkSpEffect()
  local isMax = self.bind.sliderSp >= 1
  if self._lastMax ~= isMax then
    self._lastMax = isMax
  else
    return
  end
  self.bind.active_spMax = isMax
  if isMax and not self._firstSpEffect then
    self._firstSpEffect = true
    self.bind.ani_spMax = "anim_hero_ultimate_init"
  else
    self.bind.ani_spMax = "anim_hero_ultimate_in"
  end
end

return this
