local this = class("cellKiBoOnDuel", G_UIModuleBase)
local petTpl = L_GameTpl:getPetTpl()

function this.bind()
  return {
    petIcon = "",
    healthAmount = 1,
    color_health = nil,
    deadMaskActive = false
  }
end

function this.methods()
  return {}
end

function this:open()
  L_TimerManager:newOrResetTimer(self, "cellKiBoOnDuelUpdate", handler(self, self.update), 0.1, -1)
end

function this:close()
  L_TimerManager:stopTimer(self, "cellKiBoOnDuelUpdate")
end

function this:refresh()
  if not self.isBind then
    return
  end
  local _, healthColor = C_ColorUtility.TryParseHtmlString(self.bind.isPlayerPet and "#4c9aff" or "#ff5b5c")
  self.bind.color_health = healthColor
  local tpl = petTpl:getTplById(self.bind.petConfigId)
  local special = 0
  if self.bind.guid ~= nil then
    local petItem = L_PetStore:getPetItem(self.bind.guid)
    if petItem ~= nil then
      special = petItem:isSpecialPet()
    end
  end
  self.bind.petIcon = petTpl:getPetIcon(tpl, special)
  self:update()
end

function this:update()
  if not self.isBind or self.bind.entityId == nil then
    return
  end
  local csEntity = C_EntityManager.GetEntity(self.bind.entityId)
  if csEntity == nil then
    self.bind.healthAmount = 1
    return
  end
  local aliveProperty = csEntity.data.aliveProperty
  local hp = aliveProperty.luaHp
  if hp <= 0 then
    self.bind.healthAmount = 0
    return
  end
  self.bind.healthAmount = hp / aliveProperty.luaMaxHp
end

return this
