local this = L_BevTree:registClass("guide.forceClickAction", L_BevTree.action)

function this:param()
  self.pageName = ""
  self.targetPath = ""
  self.effectPath = ""
  self.effectScale = ""
  self.dimension = ""
  self.offset = ""
  self.isListItem = 0
  self.useEventTransfer = 0
  self.delayPlayTime = 0
  self.isHideMask = 0
end

function this:parse()
  self.pageName = self:getParam("pageName")
  self.targetPath = self:getParam("targetPath")
  self.effectPath = self:getParam("effectPath")
  self.effectScale = self:getParam("effectScale")
  self.dimension = self:getParam("dimension")
  self.offset = self:getParam("offset")
  self.isListItem = self:getParam("isListItem", true)
  self.dimension = self:getParam("dimension")
  self.isUseListItem = self:getParam("isListItem", true) ~= 0
  self.isUseEventTransfer = self:getParam("useEventTransfer", true) == 0
  self.delayPlayTime = self:getParam("delayPlayTime", true)
  self.isHideMask = self:getParam("isHideMask", true) ~= 0
  self.isDestroy = false
end

function this:onEnter()
  self.result = L_BevTree.taskResult.Running
end

function this:execute()
  if string.isEmpty(self.effectPath) then
    printf("guide\230\143\144\231\164\186", "\231\137\185\230\149\136\232\183\175\229\190\132\230\152\175\231\169\186\231\154\132forceClick\229\191\133\233\161\187\233\133\141\229\144\136\231\137\185\230\149\136\228\189\191\231\148\168  ID: " .. self.id)
    return L_BevTree.taskResult.Failure
  end
  if self.result == L_BevTree.taskResult.Success or self.result == L_BevTree.taskResult.Failure then
    return self.result
  end
  if self.waitClick then
    if not L_CommonUtil.isValid(self.effectGameObject) and not self.waitDelay then
      self:dealClickFinished()
    end
    return self.result
  end
  local parentTrans
  local page = L_UI:getPage(self.pageName)
  if L_UI:checkPageShown(self.pageName) then
    if self.isUseListItem then
      parentTrans = self.tree.targetListItem.transform
    elseif string.isEmpty(self.targetPath) then
      parentTrans = page.transform
    else
      local node = page.gameObject.transform:Find(self.targetPath)
      if node ~= nil then
        parentTrans = node
      end
    end
  else
    L_UI:setTempTopGuideMask(false)
    return L_BevTree.taskResult.Running
  end
  if L_UI:checkPagePlayingAnim(page) then
    if not self.isPlayingAnimation then
      L_UI:setTempTopGuideMask(true)
    end
    self.isPlayingAnimation = true
    self.result = L_BevTree.taskResult.Running
    return L_BevTree.taskResult.Running
  end
  if not L_CommonUtil.isValid(parentTrans) then
    self.result = L_BevTree.taskResult.Running
    L_UI:setTempTopGuideMask(false)
    return L_BevTree.taskResult.Running
  end
  if not parentTrans.gameObject.activeInHierarchy or not parentTrans.gameObject.activeSelf then
    self.result = L_BevTree.taskResult.Running
    L_UI:setTempTopGuideMask(false)
    return L_BevTree.taskResult.Running
  end
  self.waitClick = true
  self.waitDelay = true
  L_UI:setTempTopGuideMask(true)
  Timer.once(self.delayPlayTime, function()
    L_UI:setTempTopGuideMask(false)
    if self.result == L_BevTree.taskResult.Failure then
      return self.result
    end
    self:dealEffect(parentTrans)
    self.waitDelay = false
    self.isPlayingAnimation = false
  end, self)
  return self.result
end

function this:getPageAnimationTime(page)
  if not page then
    return 0.3
  end
  local blendDefinition = L_UI:getPageBlendDefinition(page)
  if not blendDefinition then
    return 0.3
  end
  if not blendDefinition.m_openStyle.animation then
    return 0.3
  end
  local clipName = blendDefinition.m_openStyle.clipName
  local clip
  if not clipName then
    clip = blendDefinition.m_openStyle.animation.clip
  else
    clip = blendDefinition.m_openStyle.animation:GetClip(clipName)
  end
  if clip then
    return clip.length + 0.1
  end
  return 0.3
end

function this:openBlank(delayTime)
  if L_UI:checkPageOpen("pageBlank") then
    L_UI:close("pageBlank")
    Timer.once(0.1, function()
      L_UI:open("pageBlank", {timeClose = delayTime})
    end)
  else
    L_UI:open("pageBlank", {timeClose = delayTime})
  end
end

function this:dealEffect(parentTrans)
  if self.result == L_BevTree.taskResult.Failure then
    return
  end
  local scale, dimension
  local offset = C_Vector2.zero
  if not self.isDestroy then
    if not string.isEmpty(self.effectScale) then
      local scaleSplit = string.split(self.effectScale, "|")
      scale = L_Vector3.new(tonumber(scaleSplit[1]), tonumber(scaleSplit[2]), 1)
    end
    if not string.isEmpty(self.dimension) then
      local dimensionSplit = string.split(self.dimension, "|")
      dimension = C_Vector2(tonumber(dimensionSplit[1]), tonumber(dimensionSplit[2]))
    end
    if not string.isEmpty(self.offset) then
      local offsetSplit = string.split(self.offset, "|")
      offset = C_Vector2(tonumber(offsetSplit[1]), tonumber(offsetSplit[2]))
    end
  end
  local page = L_UI:getPage(self.pageName)
  local board = page.gameObject.transform:Find("board") or page.gameObject.transform:Find("Board")
  board = board or page.gameObject.transform
  local parentRect = parentTrans:GetComponent(typeof(C_RectTransform))
  local targetWorldPosition = parentRect:TransformPoint(C_Vector3.zero)
  self.effectGameObject = L_EffectUtil.playEffect(self.effectPath, board, scale, offset, dimension, targetWorldPosition)
  if L_CommonUtil.isValid(self.effectGameObject) and not self.isDestroy then
    local guideFxManager = self.effectGameObject:GetComponentInChildren(typeof(CS.Lens.Framework.UI.LGuideFxManager))
    if guideFxManager then
      self.scrollRect = parentTrans.gameObject:GetComponentInParent(typeof(C_LScrollRect))
      self:setScrollDisable(true)
      self:changeLongPressStatus(false)
      guideFxManager.useMask = not self.isHideMask
      guideFxManager.useEventTransfer = self.isUseEventTransfer
      guideFxManager:InitGuide(parentTrans.gameObject)
      self.finishCallback = self.finishCallback or handler(self, self.dealClickFinished)
      guideFxManager:RemoveClickCheckHandler(self.finishCallback)
      guideFxManager:AddClickCheckHandler(self.finishCallback)
    end
  end
  if not self.isHideMask and self.tree.inputActions then
    C_InputManager.EnableHighLevelControl(self.tree.inputActions, nil)
  end
  if not self.isHideMask and not self.isDestroy then
    L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.GuideControlMouse, {key = "guideClick", isShow = true})
  end
end

function this:changeLongPressStatus(status)
  C_LButton.GlobalLongInteractable = status
end

function this:setScrollDisable(disable)
  if L_CommonUtil.isValid(self.scrollRect) then
    self.scrollRect.disableScroll = disable
  end
end

function this:dealClickFinished()
  self.isDestroy = true
  self.result = L_BevTree.taskResult.Success
  if not self.isHideMask then
    L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.GuideControlMouse, {key = "guideClick", isShow = false})
  end
  if L_CommonUtil.isValid(self.effectGameObject) and self.isDestroy then
    C_GameObject.Destroy(self.effectGameObject.gameObject)
    if not self.isHideMask and not self.tree.inputActions then
      C_InputManager.DisableHighLevelControl()
    end
  end
end

function this:onReset()
  self.isDestroy = true
  self.result = L_BevTree.taskResult.Failure
  self:dealDestroy()
end

function this:onFinish()
  if self.result ~= L_BevTree.taskResult.Success then
    self.isDestroy = true
    self:dealDestroy()
    if not self.isHideMask then
      L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.GuideControlMouse, {key = "guideClick", isShow = false})
    end
  else
    self:setScrollDisable(false)
    self:changeLongPressStatus(true)
  end
  L_UI:setTempTopGuideMask(false)
end

function this:dealDestroy()
  if L_CommonUtil.isValid(self.effectGameObject) and self.isDestroy then
    C_GameObject.DestroyImmediate(self.effectGameObject.gameObject)
  end
end

return this
