local this = {}
local guideGroupTpl = L_GameTpl:getGuideGroupTpl()
local _guids_kibo_different_color_stop_detection = L_Config:getConfig("game", "GUIDE_KIBO_DIFFERENT_COLOR_STOP_DETECTION")
local _guids_kibo_different_color_distance = L_Config:getConfig("game", "GUIDE_KIBO_DIFFERENT_COLOR_DISTANCE")
local guide_hide_white_list = L_Config:getConfig("game", "GUIDE_UI_HIDE_EFFECT")

function this:checkRangeEntities()
  if self.isRangeGuidesComplete then
    return false
  end
  if not L_PlayerStore:isPlayerLogin() then
    return false
  end
  local guides = _guids_kibo_different_color_stop_detection
  local isComplete = true
  for _, v in ipairs(guides) do
    if not L_GuideStore:isGuideComplete(v) then
      isComplete = false
      break
    end
  end
  self.isRangeGuidesComplete = isComplete
  if isComplete then
    self.c_unitConfigIdList = nil
    return false
  end
  local isHasInRange = false
  local count
  count, self.c_unitConfigIdList = C_LuaUtility.GetRangeEntityUnitIdsBySpecialParam(_guids_kibo_different_color_distance, self.c_unitConfigIdList or {})
  isHasInRange = 0 < count
  if isHasInRange then
    for i = 0, count - 1 do
      local unitId = self.c_unitConfigIdList[i]
      local param = {
        guideParams = {
          {
            type = L_Const.triggerGuideType.rangeChecker,
            param = tostring(unitId)
          }
        }
      }
      self:triggerGuideByParams(param)
    end
  end
  return isHasInRange
end

function this:checkPageOpen(pageName)
  if not string.isEmpty(pageName) then
    local config = L_UI:getPageConfig(pageName)
    if not config then
      return false
    end
    if config.uiType == L_UI.groupKeys.stack then
      local curPageName = L_UI:currentPage(L_UI.groupKeys.stack)
      if curPageName ~= pageName then
        return false
      end
    end
    return L_UI:checkPageShown(pageName)
  end
  return false
end

function this:isPlayingEnterAnimation(page)
  return L_UI:checkPagePlayingAnim(page)
end

function this:getCheckKeys(showKeys)
  local res = {}
  local pressKeys = {}
  local comboKeys = {}
  local keys = string.split(showKeys, "|")
  for _, key in ipairs(keys) do
    if string.find(key, "#") ~= nil then
      local splitKeys = string.split(key, "#")
      table.insert(comboKeys, splitKeys)
      for _, k in ipairs(splitKeys) do
        table.insert(res, k)
      end
    else
      table.insert(pressKeys, key)
      table.insert(res, key)
    end
  end
  return res, pressKeys, comboKeys
end

function this:checkInputAction(pressKeys, comboKeys)
  local keyCorrect = false
  if Unity.Input.anyKeyDown then
    for _, v in ipairs(pressKeys) do
      local key = Unity.KeyCode[v]
      if key ~= nil and Unity.Input.GetKeyDown(key) then
        keyCorrect = true
        break
      end
    end
  end
  if not keyCorrect then
    for _, keys in ipairs(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
  return keyCorrect
end

function this:checkCurrentAllPages(validPages)
  validPages = validPages or {}
  local isShow = true
  for _, pageType in pairs(L_UI.groupKeys) do
    local pages = L_UI:getAllPages(pageType)
    isShow = self:checkAllPages(pages, validPages)
    if not isShow then
      return isShow
    end
  end
  return isShow
end

function this:checkAllPages(pages, validPages)
  local isShow = true
  for _, page in pairs(pages) do
    if not table.containsValue(validPages, page.pageName) and L_UI:checkPageOpen(page.pageName) and not self:isPageCanShowGuide(page.pageName) then
      isShow = false
      break
    end
  end
  return isShow
end

function this:isPageCanShowGuide(pageName)
  return table.containsValue(guide_hide_white_list, pageName)
end

function this:setWorldPause(isPause)
  if not isPause then
    C_WorldProxy:GetInstance():Resume()
  else
    C_WorldProxy:GetInstance():Pause()
  end
  L_WorldManager:translateUpdate(not isPause)
end

function this:triggerScene(guideId)
  local tpl = guideGroupTpl:getTplById(guideId)
  local triggerScenes = guideGroupTpl:getTriggerScene(tpl)
  if table.isEmpty(triggerScenes) then
    return true
  end
  for _, id in ipairs(triggerScenes) do
    if self:checkTriggerSceneId(id, guideId) then
      return true
    end
  end
  return false
end

function this:checkTriggerSceneId(triggerId, guideId)
  local curModule = L_WorldManager:getCurModule()
  if triggerId == L_Const.guideTriggerScene.world then
    if curModule == L_Const.worldModule.main or curModule == L_Const.worldModule.interim then
      return true
    end
    printf("guide triggerScene", string.format("\232\167\166\229\143\145\229\164\177\232\180\165 gudieId: %s \228\187\133\229\156\168\229\164\167\228\184\150\231\149\140\228\184\173\232\167\166\229\143\145", guideId))
  elseif triggerId == L_Const.guideTriggerScene.home then
    if L_HomeUtil.isInHome() then
      return true
    end
    printf("guide triggerScene", string.format("\232\167\166\229\143\145\229\164\177\232\180\165 gudieId: %s \228\187\133\229\156\168\229\174\182\229\155\173\228\184\173\232\167\166\229\143\145", guideId))
  elseif triggerId == L_Const.guideTriggerScene.dungeon then
    if curModule == L_Const.worldModule.dungeon or curModule == L_Const.worldModule.multiDungeon then
      return true
    end
    printf("guide triggerScene", string.format("\232\167\166\229\143\145\229\164\177\232\180\165 gudieId: %s \228\187\133\229\156\168\229\137\175\230\156\172\228\184\173\232\167\166\229\143\145", guideId))
  elseif triggerId == L_Const.guideTriggerScene.kiBoDuel then
    if C_KiBoDuelLuaWrapper:IsInKiBoDuel() then
      return true
    end
    printf("guide triggerScene", string.format("\232\167\166\229\143\145\229\164\177\232\180\165 gudieId: %s \228\187\133\229\156\168\229\165\135\230\179\162\229\175\185\230\136\152\228\184\173\232\167\166\229\143\145", guideId))
  elseif triggerId == L_Const.guideTriggerScene.metting then
    if curModule == L_Const.worldModule.meeting then
      return true
    end
    printf("guide triggerScene", string.format("\232\167\166\229\143\145\229\164\177\232\180\165 gudieId: %s \228\187\133\229\156\168metting\228\184\173\232\167\166\229\143\145", guideId))
  elseif triggerId == L_Const.guideTriggerScene.prologue then
    if curModule == L_Const.worldModule.prologue then
      return true
    end
    printf("guide triggerScene", string.format("\232\167\166\229\143\145\229\164\177\232\180\165 gudieId: %s \228\187\133\229\156\168prologue\228\184\173\232\167\166\229\143\145", guideId))
  end
  return false
end

return this
