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

function this:param()
  self.pageName = ""
  self.tipsKey = ""
  self.showKeys = ""
  self.showTime = 0
  self.longPressKeys = ""
  self.longPressTime = 0
  self.delayTime = 0
  self.validPages = ""
  self.skipDetect = 0
  self.inputActions = ""
  self.isForce = 0
end

function this:parse()
  self.pageName = self:getParam("pageName")
  self.tipsKey = self:getParam("tipsKey")
  self.showKeys = self:getParam("showKeys")
  self.showTime = self:getParam("showTime", true) or 0
  self.longPressKeys = self:getParam("longPressKeys")
  self.longPressTime = self:getParam("longPressTime", true) or 0
  self.delayTime = self:getParam("delayTime", true) or 0
  self.validPages = self:getParam("validPages") or ""
  self.skipDetect = self:getParam("skipDetect", true) or 0
  self.skipDetect = self.skipDetect == 1
  local isForce = self:getParam("isForce", true) or 0
  self.isForce = isForce ~= 0
  self.inputActions = self:getParam("inputActions")
end

function this:onEnter()
  if self.isDestory then
    self.result = L_BevTree.taskResult.Failure
    return
  end
  self.waitSuccess = false
  local tips = L_WordsTpl:getValue(self.tipsKey)
  self.isDetectLongPress = not string.isEmpty(self.longPressKeys) and self.longPressTime > 0
  self.isDetectKeys = not string.isEmpty(self.showKeys)
  local showKeys = {}
  if self.isDetectKeys then
    self.pressKeys = {}
    self.comboKeys = {}
    local keys = string.split(self.showKeys, "|")
    for _, key in ipairs(keys) do
      if string.find(key, "#") ~= nil then
        local splitKeys = string.split(key, "#")
        table.insert(self.comboKeys, splitKeys)
        for _, k in ipairs(splitKeys) do
          table.insert(showKeys, k)
        end
      else
        table.insert(self.pressKeys, key)
        table.insert(showKeys, key)
      end
    end
  end
  if self.isDetectLongPress then
    self.longPressKey = Unity.KeyCode[self.longPressKeys]
    self.isPressed = false
  end
  local validPages = string.split(self.validPages, "|")
  table.insert(validPages, self.pageName)
  self.checkPages = validPages
  self.controlActions = string.split(self.inputActions, "|")
  self.showTipData = {
    showTips = tips,
    showKeys = showKeys,
    longPressKey = self.longPressKeys,
    showTime = self.showTime,
    showPageName = self.pageName,
    validPages = validPages,
    isForce = self.isForce
  }
  self.result = L_BevTree.taskResult.Running
end

function this:execute()
  if self.isDestory then
    return self.result
  end
  self:checkUIShow()
  self:checkEffectFinished()
  if self.result ~= L_BevTree.taskResult.Running then
    return self.result
  end
  if self.waitSuccess then
    return L_BevTree.taskResult.Running
  end
  if not L_GuideManager:checkPageOpen(self.pageName) then
    self.result = L_BevTree.taskResult.Running
    return L_BevTree.taskResult.Running
  end
  if not self.waitAction and not L_GuideManager:checkPageOpen("pageGuideCombinedTip") then
    if self.showTime ~= nil and self.showTime > 0 then
      self.waitAction = true
      self:disposeTimer()
      self.showTimer = Timer.once(self.showTime, function()
        self.waitAction = false
        self.waitSuccess = true
      end)
    end
    L_UI:open("pageGuideCombinedTip", self.showTipData)
    if 0 < self.delayTime then
      L_UI:setTempTopGuideMask(true)
      self.waitDelay = true
      Timer.once(self.delayTime, function()
        self.waitDelay = false
        L_UI:setTempTopGuideMask(false)
      end, self)
    end
  end
  if self.waitDelay then
    self.result = L_BevTree.taskResult.Running
    return self.result
  end
  if self.skipDetect then
    self.result = L_BevTree.taskResult.Running
    return self.result
  end
  if self.isDetectKeys then
    self:checkInputAction()
  elseif self.isDetectLongPress then
    self:checkLongPressAction()
  elseif not self.waitAction then
    self.result = L_BevTree.taskResult.Success
  end
  return self.result
end

function this:checkEffectFinished()
  if not self.waitSuccess then
    return
  end
  if not L_UI:checkPageOpen("pageGuideCombinedTip") then
    self.result = L_BevTree.taskResult.Success
  end
end

function this:checkInputAction()
  local keyCorrect = false
  if Unity.Input.anyKeyDown then
    for _, v in ipairs(self.pressKeys) do
      local key = Unity.KeyCode[v]
      if key ~= nil and Unity.Input.GetKeyDown(key) then
        self.waitSuccess = true
        keyCorrect = true
        break
      end
    end
  end
  if not keyCorrect then
    for _, keys in ipairs(self.comboKeys) do
      local isPress = true
      for i = 1, #keys do
        local key = Unity.KeyCode[keys[i]]
        if not Unity.Input.GetKey(key) then
          isPress = false
        end
      end
      if isPress then
        keyCorrect = true
        break
      end
    end
  end
  if keyCorrect then
    self:dealKeyCorrect()
  end
end

function this:checkLongPressAction()
  local keyCorrect = false
  if self.isDetectLongPress and self.longPressKey ~= nil then
    if Unity.Input.GetKeyDown(self.longPressKey) then
      self.isPressed = true
      self.curTime = C_Time.time
    elseif Unity.Input.GetKey(self.longPressKey) and self.isPressed and self.curTime ~= nil then
      if C_Time.time - self.curTime >= self.longPressTime then
        keyCorrect = true
      end
    elseif Unity.Input.GetKeyUp(self.longPressKey) then
      self.isPressed = false
      self.curTime = nil
    end
  end
  if keyCorrect then
    self:dealKeyCorrect()
  end
end

function this:dealKeyCorrect()
  self.bSendChecked = true
  L_GuideManager:internalSendEvent(G_GuideClasses.GuideEvent.InputChecked)
  self.waitSuccess = true
end

function this:dealDestroy()
  if L_UI:checkPageOpen("pageGuideCombinedTip") and not self.bSendChecked then
    L_UI:close("pageGuideCombinedTip")
  end
  self:destory()
end

function this:onReset()
  self.isDestory = true
  self:dealDestroy()
end

function this:onDestroy()
  self.isDestory = true
  self:dealDestroy()
end

function this:onFinish()
  self:dealDestroy()
  if self.isForce then
    C_InputManager.DisableHighLevelControl()
  end
end

function this:destory()
  self:disposeTimer()
end

function this:disposeTimer()
  if self.showTimer ~= nil then
    Timer.remove(self.showTimer)
    self.showTimer = nil
  end
end

function this:checkUIShow()
  if self.result ~= L_BevTree.taskResult.Running then
    return
  end
  local isShow = L_GuideManager:checkCurrentAllPages(self.checkPages)
  if self.lastIsShow ~= isShow then
    self:setInputControl(isShow)
  end
  self.lastIsShow = isShow
end

function this:setInputControl(isControl)
  if self.isForce then
    if isControl then
      if self.controlActions ~= nil and #self.controlActions > 0 then
        C_InputManager.EnableHighLevelControl(self.controlActions, nil)
      end
    else
      C_InputManager.DisableHighLevelControl()
    end
  end
end

return this
