local this = class("cellKiBoDuelSkill", G_UIModuleBase)

function this.bind()
  return {
    txt_skillCd = "0",
    fill_maskSkill = 0,
    go_skillCd = false,
    img_skill = "",
    limitText = "",
    list_point = {
      moduleName = "pages/petDuel/cellKiboDuelPoint"
    }
  }
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, "movement.cellKiBoDuelSkill.update")
  L_PlayerManager:addListener(L_PlayerManager.event.setMainControl, self.setMainControl, self)
end

function this:initSkillType(skillType, isPetDuel)
  self.isFirstUse = true
  self.skillType = skillType
  self.isPetDuel = isPetDuel
  self:refreshSkillIcon()
end

function this:show()
end

function this:close()
  self.coolDown = nil
  C_UpdateSource.RemoveUpdateEventHandler(self.updateHandle)
  L_PlayerManager:removeListener(L_PlayerManager.event.setMainControl, self.setMainControl, self)
end

function this:setMainControl()
  self:refreshSkillIcon()
end

function this:refreshSkillIcon()
  if self.skillType == nil then
    return
  end
  local controlEntity = L_PlayerManager:getMainControlEntity()
  if not controlEntity then
    return
  end
  if controlEntity:getEntityType() ~= L_EcsConst.aliveType.pet and not self.isPetDuel then
    return
  end
  local tmp = {}
  for i = 1, 3 do
    table.insert(tmp, {
      index = i,
      isUse = false,
      toggle_condition = true
    })
  end
  self.bind.list_point:clear()
  self.bind.list_point:insert_array(tmp)
  local skill = controlEntity:getCsEntity().data.skillProperty:GetSkillBySlot(self.skillType)
  if skill then
    local skillId = skill.id
    local tpl = L_GameTpl:getSkillTpl():getTplById(skillId)
    if tpl ~= nil then
      local icon = L_GameTpl:getSkillTpl():getBattleSkillIcon(tpl)
      self.bind.img_skill = icon
    end
  end
end

function this:refresh()
end

function this:update()
  if self.skillType == nil then
    return
  end
  local controlEntity = L_PlayerManager:getMainControlEntity()
  if not controlEntity then
    return
  end
  if controlEntity:getEntityType() ~= L_EcsConst.aliveType.pet and not self.isPetDuel then
    return
  end
  local skillProperty = controlEntity:getCsEntity().data.skillProperty
  self.bind.go_skillCd = false
  local skill = skillProperty:GetSkillBySlot(self.skillType)
  if skill then
    local coolDown = skill.coolDown
    local ready = coolDown:Ready()
    if self.lastCurrentCharge ~= nil and self.lastCurrentCharge > coolDown.currentChargeCount then
      self:onKiBoSkillClicked(self.skillType)
    end
    if not ready and coolDown.currentChargeCount > 0 then
      self.canUseSkill = false
      self.bind.go_skillCd = true
      local coolTime = C_MyFloatUtility.GetFloat(coolDown.coolTime)
      local coolTimeNum = C_MyFloatUtility.GetFloat(coolDown.coolDown)
      self.bind.txt_skillCd = tostring(math.round(coolTime))
      self.bind.fill_maskSkill = coolTime / coolTimeNum
      if coolTime / coolTimeNum <= 0.015 then
        self.bindComponents.skillAnimator:Play("anim_petduel_skill_cd")
        self.canUseSkill = true
      end
    end
    if coolDown.coolDownType == C_ECoolDownType.KiBoVersusCommonSkill then
      for i = 1, coolDown.chargeMaxCount do
        if self.bind.list_point:getValue(i, "index") > coolDown.currentChargeCount and self.bind.list_point:getValue(i, "isUse") == false then
          self.bind.list_point:change(i, {isUse = true, toggle_condition = false})
        end
      end
      self.bind.limitText = string.format("%d/%d", coolDown.currentChargeCount, coolDown.chargeMaxCount)
    end
    if coolDown.currentChargeCount <= 0 then
      self.bind.go_skillCd = true
      self.bind.txt_skillCd = ""
      self.bind.fill_maskSkill = 1
      self.canUseSkill = false
    end
    self.coolDown = coolDown
    self.lastCurrentCharge = coolDown.currentChargeCount
  end
end

function this:onKiBoSkillClicked(type)
  if not self.isBind then
    return
  end
  if self.skillType == nil then
    return
  end
  if self.isFirstUse then
    self.canUseSkill = true
    self.isFirstUse = false
  end
  if self.skillType == type and self.coolDown ~= nil then
    print("\230\138\128\232\131\189\230\172\161\230\149\176\239\188\154", self.coolDown.currentChargeCount)
    if self.coolDown.currentChargeCount >= 0 and self.canUseSkill then
      L_TimerManager:newOrResetTimer(self, "wait", function()
        self.bindComponents.skillAnimator:Play("anim_petduel_skill_use")
        self.canUseSkill = false
        L_TimerManager:stopTimer(self, "wait")
      end, 0.015)
      return
    elseif self.coolDown.currentChargeCount <= 0 then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_kibo_duel_in_battle_03"))
      return
    end
    local ready = self.coolDown:Ready()
    if not ready then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_kibo_duel_in_battle_02"))
      return
    end
  end
end

return this
