local this = class("cellDuelCostItem", G_UIModuleBase)
local maxRatio = 0.96

function this.bind()
  return {
    highLightFillAmont = 0,
    normalActive = true,
    feverActive = false
  }
end

function this:open()
  if not self.isBind then
    return
  end
  if not self.bindComponents.cellAnimator then
    return
  end
  L_KiBoDuelManager:addListener(L_KiBoDuelManager.event.startRapidState, self.onStartRapidState, self)
  self.bind.highLightFillAmont = 0
  self.bindComponents.cellAnimator:SetBool("Show", false)
  self.bindComponents.cellAnimator:SetBool("Selected", false)
end

function this:onStartRapidState(isRapid)
  self:setPointStatus(isRapid)
end

function this:setPointStatus(isRapid)
  if not self.isBind then
    return
  end
  self.bind.normalActive = not isRapid
  self.bind.feverActive = isRapid
end

function this:updatePercent()
  self.recoverTimer = Timer.repeated(0.01, function()
    if self.bind.index == self.bind.currentCost + 1 then
      local percent, status = L_KiBoDuelManager:getCostData()
      if status == L_Const.kiBoDuelCostStatus.RecoverCost then
        self.bind.highLightFillAmont = percent * maxRatio
      else
        self.bind.highLightFillAmont = 0
      end
    end
  end, self)
end

function this:refresh()
  if self.bind.isEmpty then
    return
  end
  self:disposeRecoverTimer()
  self:updatePercent()
  if not self.isBind then
    return
  end
  self:setPointStatus(L_KiBoDuelManager:getRapidState())
  self.bindComponents.cellAnimator:SetBool("Filled", self.bind.index <= self.bind.currentCost)
  if self.bind.index <= self.bind.currentCost then
    self.bind.highLightFillAmont = maxRatio
  else
    self.bind.highLightFillAmont = 0
  end
  self:refreshUrD(self.bind.index % 2 == 0)
end

function this:refreshUrD(isShow)
  if not self.isBind then
    return
  end
  self.bindComponents.cellAnimator:SetBool("UorD", isShow)
end

function this:close()
  self:disposeRecoverTimer()
  L_KiBoDuelManager:removeListener(L_KiBoDuelManager.event.startRapidState, self.onStartRapidState, self)
end

function this:disposeRecoverTimer()
  if self.recoverTimer ~= nil then
    Timer.remove(self.recoverTimer)
  end
  self.recoverTimer = nil
end

function this:playInitAnimation(isShow)
  if not self.isBind then
    return
  end
  Timer.once((self.bind.index - 1) * self.bind.index * 0.0085, function()
    self:setShowAnimator(isShow)
  end, self)
end

function this:setShowAnimator(isShow)
  if not self.bindComponents.cellAnimator then
    return
  end
  self.bindComponents.cellAnimator:SetBool("Show", isShow)
end

function this:playSelectedAnimator(isSelected, lastCost)
  if self.isSelected == isSelected then
    return
  end
  self.isSelected = isSelected
  self:disposeSelectedTimer()
  local delayTime = (self.bind.index - lastCost - 1) * 0.02
  if delayTime < 0 then
    delayTime = 0
  end
  self.selectTimer = Timer.once(delayTime, function()
    self:setSetelected(isSelected)
  end, self)
end

function this:setSetelected(isSelected)
  if not self.bindComponents.cellAnimator then
    return
  end
  self.isSelected = isSelected
  self.bindComponents.cellAnimator:SetBool("Selected", isSelected)
end

function this:disposeSelectedTimer()
  if self.selectTimer ~= nil then
    Timer.remove(self.selectTimer)
    self.selectTimer = nil
  end
end

function this:triggerCast()
  if not self.isBind then
    return
  end
  Timer.once((self.bind.index - 1) * 0.02, function()
    if not self.bindComponents.cellAnimator then
      return
    end
    self.bindComponents.cellAnimator:Play("anim_petduel_energy_select_cast", 0)
  end, self)
end

function this:playForceInit()
  if not self.bindComponents.cellAnimator then
    return
  end
  self.bindComponents.cellAnimator:SetTrigger("ForceInit")
end

function this:getIndex()
  return self.bind.index
end

return this
