local this = class("cellKiBoDuelDodge", G_UIModuleBase)
local constEvadeBtnStatus = {
  idle = 0,
  fill = 1,
  cd = 2
}

function this.bind()
  return {
    fill_cdEvade = 0,
    fill_evade = 0,
    txt_evade = ""
  }
end

function this.methods()
  return {}
end

function this:created(...)
  this.super.created(self, ...)
end

function this:open()
  self.updateHandle = handler(self, self.update)
  C_UpdateSource.AddUpdateEventHandler(self.updateHandle, "cellKiBoDuelDodge.update")
  self.data = {
    evadeStatus = constEvadeBtnStatus.idle,
    oldEvadeCount = 3
  }
  self.dodgeContentList = {}
  for i = 0, self.bindComponents.cdEnergyRoot.transform.childCount - 1 do
    local item = {}
    item.go = self.bindComponents.cdEnergyRoot.transform:GetChild(i).gameObject
    item.anim = item.go:GetComponent(typeof(C_Animation))
    table.insert(self.dodgeContentList, item)
  end
end

function this:show()
  self:resetEffect()
end

function this:resetEffect()
  self.data.evadeStatus = constEvadeBtnStatus.idle
  self.data.oldEvadeCount = 3
end

function this:close()
  self.coolDown = nil
  C_UpdateSource.RemoveUpdateEventHandler(self.updateHandle)
end

function this:update()
  local hero = L_PlayerManager:getMainHeroEntity()
  if not hero then
    return
  end
  local skillProperty = hero:getCsEntity().data.skillProperty
  local evadeSkill = skillProperty:GetSkillBySlot(L_Const.C_ESkillSlotType.Evade)
  if evadeSkill == nil then
    return
  end
  local evadeCoolDown = evadeSkill.coolDown
  local nowCdTime = C_TimeManager.globalTime - evadeCoolDown.lastCastTime
  local coolDownHintTime = evadeCoolDown.coolDownHintTime
  local coolTime = C_MyFloatUtility.GetFloat(evadeCoolDown.coolTime)
  local coolDown = C_MyFloatUtility.GetFloat(evadeCoolDown.coolDown)
  if coolTime <= 0 and nowCdTime >= coolDownHintTime then
    self:refreshEvadeStatus(constEvadeBtnStatus.idle)
    self.data.oldEvadeCount = 3
  else
    if self.data.oldEvadeCount > evadeCoolDown.currentChargeCount then
      self.dodgeContentList[3 - evadeCoolDown.currentChargeCount].anim:Play("anim_battle_dodge_charge_use")
    end
    self.data.oldEvadeCount = evadeCoolDown.currentChargeCount
    if evadeCoolDown.currentChargeCount == 0 then
      self.bind.fill_evade = 0
      self:refreshEvadeStatus(constEvadeBtnStatus.cd, coolTime / coolDown)
    elseif nowCdTime < coolDownHintTime then
      self:refreshEvadeStatus(constEvadeBtnStatus.fill, 1 - nowCdTime / coolDownHintTime, evadeCoolDown.currentChargeCount)
    end
  end
end

function this:refreshEvadeStatus(evadeStatus, fill, count)
  if evadeStatus == constEvadeBtnStatus.idle then
    if evadeStatus == self.data.evadeStatus then
    else
      self.bind.fillevade = 0
      self.bind.fill__cdEvade = 0
      self.data.evadeStatus = constEvadeBtnStatus.idle
      self.bindComponents.dodgeAnim:SetBool("active", false)
      self.bindComponents.dodgeAnim:SetBool("cd", false)
      for i, v in ipairs(self.dodgeContentList) do
        v.go:ActiveTrans(true)
        v.anim:Play("anim_battle_dodge_charge_init")
      end
    end
    return
  end
  if evadeStatus == constEvadeBtnStatus.fill then
    if self.data.evadeStatus == constEvadeBtnStatus.idle then
      self.bind.fill_evade = 1
      self.bind.fill_cdEvade = 0
      self.bindComponents.dodgeAnim:SetBool("active", true)
    end
    self.data.evadeStatus = evadeStatus
    local getter = function()
      return self.bind.fill_evade
    end
    local setter = function(r)
      self.bind.fill_evade = r
    end
    self.tween = DOTween.To(getter, setter, fill, 0.1):SetEase(Tweening.Ease.Linear)
  elseif evadeStatus == constEvadeBtnStatus.cd then
    if self.data.evadeStatus == constEvadeBtnStatus.fill then
      self.bind.fill_evade = 0
      self.bind.fill_cdEvade = 1
      self.bindComponents.dodgeAnim:SetBool("active", false)
      self.bindComponents.dodgeAnim:SetBool("cd", true)
    end
    self.data.evadeStatus = evadeStatus
    local getter = function()
      return self.bind.fill_cdEvade
    end
    local setter = function(r)
      self.bind.fill_cdEvade = r
    end
    self.tween = DOTween.To(getter, setter, fill, 0.1):SetEase(Tweening.Ease.Linear)
  end
end

return this
