local this = L_BevTree:registClass("guide.castNextSkillAction", L_BevTree.action)
local tag = this.getName()

function this:param()
  self.configId = ""
  self.skillId = ""
  self.skillIndex = ""
  self.isPet = 0
  self.skillSlot = 0
end

function this:parse()
  self.configId = self:getParam("configId", true)
  self.skillId = self:getParam("skillId", true)
  self.skillIndex = self:getParam("skillIndex", true)
  self.isPet = self:getParam("isPet", true)
  self.skillSlot = self:getParam("skillSlot", true)
end

function this:onEnter()
end

function this:onFinish()
  self:resumeWorld()
end

function this:onReset()
  self:resumeWorld()
end

function this:execute()
  local mainControl = L_PlayerManager:getMainControlEntity()
  if mainControl == nil then
    return L_BevTree.taskResult.Success
  end
  if math.isEmpty(self.isPet) then
    if math.isEmpty(self.configId) or math.isEmpty(self.skillId) then
      return L_BevTree.taskResult.Failure
    end
    local heroId = mainControl:getConfigId()
    if self.configId ~= heroId then
      return L_BevTree.taskResult.Failure
    end
    self:castSkill(mainControl:getCsEntity(), self.skillId, self.skillIndex)
  else
    if not L_PlayerManager:getPlayerUnit():getMainPetEntity_cs() then
      return L_BevTree.taskResult.Success
    end
    local petEntity = L_PlayerManager:getPlayerUnit():getMainPetEntity_cs()
    if petEntity == nil then
      return L_BevTree.taskResult.Success
    end
    local skillProperty = petEntity.data.skillProperty
    local skill = skillProperty:GetSkillBySlot(self.skillSlot)
    local skillId = skill.id
    self:castSkill(petEntity, skillId, self.skillIndex)
  end
  return L_BevTree.taskResult.Success
end

function this:castSkill(entity, skillId, skillIndex)
  self:resumeWorld()
  CS.Lens.Gameplay.Modules.BigWorld.SkillUtility.CastNextSkill(entity, skillId, skillIndex)
end

function this:resumeWorld()
  if not self.resume then
    C_WorldProxy:GetInstance():Resume()
    L_WorldManager:translateUpdate(true)
    self.resume = true
  end
end

return this
