local this = L_BevTree:registClass("guide.guideStepSequencer", L_BevTree.composite)
local tag = this.getName()

function this:param()
  self.guideStep = -1
  self.isAutoSaveOnFinish = 1
end

function this:parse()
  self.guideStep = self:getParam("guideStep", true)
  self.isAutoSaveOnFinish = self:getParam("isAutoSaveOnFinish", true)
  self._hasCompleted = false
end

function this:onEnter()
  self._hasCompleted = L_GuideStore:isGuideStepComplete(self.tree.param.guideId, self.guideStep)
  if self._hasCompleted == true then
    printf(tag, "\232\161\140\228\184\186\230\160\145id\239\188\154" .. self.tree.param.guideId .. " \230\173\165\233\170\164\239\188\154" .. self.guideStep .. " \229\174\140\230\136\144")
  elseif not L_GuideManager:isOnDebugMode() then
    local guideGroupTpl = L_GameTpl:getGuideGroupTpl()
    local tpl = guideGroupTpl:getTplById(self.tree.param.guideId)
    local guideType = guideGroupTpl:getGuideType(tpl)
    local eventData = {
      act_type = 1,
      guide_id = self.guideStep,
      guide_group_id = self.tree.param.guideId,
      guide_type = guideType
    }
    L_GameUtil.logEvent(L_Const.logEventName.guide, eventData)
  end
end

function this:canRunChildren()
  return self._hasCompleted == false
end

function this:execute(childResult)
  if self._hasCompleted == true then
    return L_BevTree.taskResult.Success
  end
  if childResult == L_BevTree.taskResult.Failure or childResult == L_BevTree.taskResult.Running then
    return childResult
  end
  return L_BevTree.taskResult.Success
end

function this:onFinish()
  if self._hasCompleted == false and self.isAutoSaveOnFinish == 1 and self.guideStep > 0 and self.lastResult == L_BevTree.taskResult.Success and not L_GuideManager:isOnDebugMode() then
    L_GuideManager:saveStepId(self.tree.param.guideId, self.guideStep)
  end
end

function this:moveNext()
  if self._hasCompleted == true or self.lastResult == L_BevTree.taskResult.Failure or self.lastResult == L_BevTree.taskResult.Running then
    return false
  end
  self.childIndex = self.childIndex + 1
  return self.childIndex <= #self.children
end

return this
