local this = L_BevTree:registClass("guide.findTargetListItem", L_BevTree.action)
local guideStuckTime = L_Config:getConfig("game", "GUIDE_SKIP_BUTTON_DISPLAY_TIME")

function this:param()
  self.pageName = ""
  self.targetPath = ""
  self.targetId = 0
  self.listType = 0
  self.listIndex = 0
  self.inputActions = ""
  self.childPath = ""
end

function this:parse()
  self.pageName = self:getParam("pageName")
  self.targetPath = self:getParam("targetPath")
  self.targetId = self:getParam("targetId", true)
  self.listType = self:getParam("listType", true)
  self.listIndex = self:getParam("listIndex", true)
  self.inputActions = self:getParam("inputActions")
  self.childPath = self:getParam("childPath")
end

function this:onEnter()
  self.lastStuck = false
  self.curCheckTime = C_Time.time
  self:dealHighInputAction()
  self.result = L_BevTree.taskResult.Running
end

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

function this:dealHighInputAction()
  if not string.isEmpty(self.inputActions) then
    local actions = string.split(self.inputActions, "|")
    C_InputManager.EnableHighLevelControl(actions, nil)
    return true
  end
  return false
end

function this:dealDisableHighInputAction()
  if not string.isEmpty(self.inputActions) then
    C_InputManager.DisableHighLevelControl()
  end
end

function this:execute()
  if self.result == L_BevTree.taskResult.Running then
    local page = L_UI:getPage(self.pageName)
    if L_UI:checkPageShown(self.pageName) then
      local target = page.gameObject.transform:Find(self.targetPath)
      if not target then
        return L_BevTree.taskResult.Running
      end
      local targetList = target:GetComponent(typeof(C_LScrollRect))
      if targetList == nil then
        targetList = target:GetComponent(typeof(C_LList))
      end
      if targetList ~= nil then
        local targetItem
        if self.listIndex ~= 0 then
          local item = targetList:GetItemById(self.listIndex - 1)
          if not item or not item.uiBinding then
            return L_BevTree.taskResult.Running
          end
          self.tree.targetListItem = item.uiBinding
          if not string.isEmpty(self.childPath) then
            self.tree.targetListItem = self.tree.targetListItem.gameObject.transform:Find(self.childPath)
          end
          if L_CommonUtil.isValid(self.tree.targetListItem) then
            return L_BevTree.taskResult.Success
          end
        end
        for k, item in pairs(targetList.luaTable) do
          local itemBinding
          if self.listType == G_GuideClasses.listItemType.hero then
            if item.unitId ~= nil and item.unitId == self.targetId then
              itemBinding = targetList.luaTable:getItemCls(k).uiBinding
            end
            if item.id ~= nil and item.id == self.targetId then
              itemBinding = targetList.luaTable:getItemCls(k).uiBinding
            end
            if item.guid ~= nil then
              local hero = L_HeroStore:getHero(item.guid)
              if L_HeroStore:getHeroConfigId(hero) == self.targetId then
                itemBinding = targetList.luaTable:getItemCls(k).uiBinding
              end
            end
          end
          if self.listType == G_GuideClasses.listItemType.spiritron and item.serverData and item.serverData.spiritronId == self.targetId then
            itemBinding = targetList.luaTable:getItemCls(k).uiBinding
          end
          if self.listType == G_GuideClasses.listItemType.pet then
            if item.id and item.id == self.targetId then
              itemBinding = targetList.luaTable:getItemCls(k).uiBinding
            end
            if item.groupId and item.groupId == self.targetId then
              itemBinding = targetList.luaTable:getItemCls(k).uiBinding
            end
          end
          if self.listType == G_GuideClasses.listItemType.product and item.id and item.id == self.targetId then
            itemBinding = targetList.luaTable:getItemCls(k).uiBinding
          end
          if self.listType == G_GuideClasses.listItemType.mountPet and item.guid then
            local pet = L_PetStore:getPetItem(item.guid)
            if self.targetId == pet.petId then
              itemBinding = targetList.luaTable:getItemCls(k).uiBinding
            end
          end
          if itemBinding then
            targetItem = itemBinding
            if not string.isEmpty(self.childPath) then
              targetItem = targetItem.gameObject.transform:Find(self.childPath)
            end
            self.tree.targetListItem = targetItem
            if L_CommonUtil.isValid(targetItem) then
              return L_BevTree.taskResult.Success
            end
          end
        end
      end
    end
    self:checkUIShow()
  end
  return L_BevTree.taskResult.Running
end

function this:checkUIShow()
  if string.isEmpty(self.inputActions) then
    return
  end
  local isStuck = C_Time.time - self.curCheckTime >= guideStuckTime
  if isStuck ~= self.lastStuck then
    L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.ShowSkipGuide, {
      skipNotSave = self.skipNotSave,
      guideId = self.tree.param.guideId,
      isShow = isStuck
    })
    if not isStuck then
      self:dealHighInputAction()
    end
  end
  self.lastStuck = isStuck
  self.curCheckTime = C_Time.time
end

function this:onDestroy()
  self:dealDisableHighInputAction()
end

return this
