local this = class("jumpManager")
local _jumpTpl = L_GameTpl:getCommonJumpTpl()
local _worldBorthposTpl = L_GameTpl:getWorldBorthposTpl()
local _filterMarkTpl = L_GameTpl:getWorldFilterMarkTpl()

function this:jumpTo(jumpId, options)
  local jumpCfg = _jumpTpl:getTplById(jumpId)
  if not jumpCfg then
    errorf("jumpId\228\184\141\229\173\152\229\156\168 = " .. tostring(jumpId))
    return
  end
  local isComplete = L_ConditionManager:isComplete(_jumpTpl:getCondition(jumpCfg))
  if not isComplete then
    L_FlyMsgManager:showNormalMsg(_jumpTpl:getUnlockTips(jumpCfg))
    return
  end
  local func = function()
    options = options or {}
    options.jumpParams = _jumpTpl:getParam(jumpCfg)
    if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.Page then
      local pageName = _jumpTpl:getPageName(jumpCfg)
      if pageName == "pageActivitySevenDaySign" then
        options.gameEventType = tonumber(options.jumpParams[1])
      end
      local page = L_UI:getPage(pageName)
      if page ~= nil then
        L_UI:backToPage(pageName, options)
      else
        L_UI:open(pageName, options)
      end
      return
    end
    if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.MapCollectionTrace then
      self:jumpToMapCollectionTrace(_jumpTpl:getParam(jumpCfg))
      return
    end
    if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.HomeJump then
      self:jumpToHome()
      return
    end
    if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.SuperLink then
      self:jumpToWebView(_jumpTpl:getParam(jumpCfg)[1])
      return
    end
    if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.PageBigMap_SpecialPet then
      self:jumpToPageBigMapSpecialPet(options)
      return
    end
    if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.PageBigMap_HomeTrace then
      self:jumpToHomeBirthPos(tonumber(options.jumpParams[1]), jumpId)
      return
    end
  end
  if _jumpTpl:getNeedBackToRoot(jumpCfg) then
    L_UI:backToRoot(func)
  else
    func()
  end
end

function this:checkJumpFuncOpen(jumpId)
  local jumpCfg = _jumpTpl:getTplById(jumpId)
  if not jumpCfg then
    errorf("jumpId\228\184\141\229\173\152\229\156\168 = " .. tostring(jumpId))
    return true
  end
  local isComplete = L_ConditionManager:isComplete(_jumpTpl:getCondition(jumpCfg))
  return isComplete
end

function this:jumpToWebView(url, callback)
  local data = {
    url = url,
    title = L_WordsTpl:getValue("ui_pageMail_02"),
    callback = callback,
    showLoading = true
  }
  L_UI:open("pageCommonWeb", data)
end

function this:jumpToMailQuestionLink(url, option)
  L_MailStore:req_questionLinkUrl(option.guid, function(url)
    self:jumpToWebView(url)
  end)
end

function this:jumpToHome()
  if not L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.home) then
    return
  end
  if L_PlayerManager:inKiBoTransformMainControl() then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_transform_05"))
    return
  end
  if L_DungeonManager:getIsInDungeon() then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_cannot_gohome"))
    return
  end
  local curModule = L_WorldManager:getCurModule()
  if curModule == L_Const.worldModule.home then
    L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_systemBreakManager_01"))
    return
  end
  local data = {
    txtContent = L_WordsTpl:getValue("notice_home_transfer_enter"),
    confirmCallback = function()
      if L_WorldManager:getIsActive() then
        L_HomeStore:req_enterHome()
        L_PlayerManager:shotdownSendOperator()
      else
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_noActive_01"))
      end
    end
  }
  L_GameUtil.showCommonTip(data)
end

function this:jumpToPageBigMapSpecialPet(mailData)
  if not mailData.args or table.count(mailData.args) < 5 then
    errorf("\231\137\185\230\174\138\229\165\135\230\179\162\229\135\186\231\142\176\233\130\174\228\187\182\229\143\130\230\149\176\229\188\130\229\184\184")
    return
  end
  local option = {
    map_id = mailData.args[1].attr_id,
    obj_id = mailData.args[2].attr_id,
    special_type = mailData.args[5].attr_id
  }
  local bExist = false
  for _, v in ipairs(L_MapStore.specialMonsters) do
    if v.map_id == option.map_id and v.obj_id == option.obj_id and v.special_type == option.special_type then
      bExist = true
      break
    end
  end
  if not bExist then
    L_FlyMsgManager:showNormalMsg(L_GameTpl:getWordsTpl():getTplById("notice_special_pet_disappear"))
    return
  end
  local areaId = L_GameTpl:getWorldAreaTpl():getCurSceneAreaList(option.map_id)[1].id
  L_UI:open("pageBigMap", {
    areaId = areaId,
    specialParams = {
      map_id = option.map_id,
      obj_id = option.obj_id,
      special_type = option.special_type
    }
  })
end

function this:jumpToHomeBirthPos(worldFilterMarkId, jumpId)
  local homeCityId = L_GameTpl:getGameConstTpl():getValue("HOME_ID")
  local birthCfg = _worldBorthposTpl:getMainPointTplByCityId(homeCityId)
  local filterMarkCfg = _filterMarkTpl:getTplById(worldFilterMarkId)
  local jumpCfg = _jumpTpl:getTplById(jumpId)
  local node = {
    sceneId = homeCityId,
    type = L_MapConst.mapSievingType.homeTrace,
    id = worldFilterMarkId,
    iconPath = _filterMarkTpl:getIcon(filterMarkCfg),
    configPos = _worldBorthposTpl:getPosition(birthCfg),
    iconBg = _filterMarkTpl:getIconBg(filterMarkCfg),
    name = L_Config:provider(_jumpTpl:getHomeText(jumpCfg)[1]),
    desc = L_Config:provider(_jumpTpl:getHomeText(jumpCfg)[2])
  }
  L_MapStore:setNodeTargetTracing(homeCityId, node)
  local areaId = L_GameTpl:getWorldAreaTpl():getCurSceneAreaList(homeCityId)[1].id
  L_UI:open("pageBigMap", {areaId = areaId, hasHomeTrace = true})
end

function this:jumpToMapCollectionTrace(params)
  if not L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.mapCollectionTrace) then
    return
  end
  local page = L_UI:getPage("pageBigMap")
  if page then
    local pageName = "pageMapCollection"
    local options = {}
    options.sceneId = AzurWorldInstance.CurWorldId
    options.jumpParams = params
    local page = L_UI:getPage(pageName)
    if page ~= nil then
      L_UI:backToPage(pageName, options)
    else
      L_UI:open(pageName, options)
    end
  else
    L_UI:open("pageBigMap", {
      openCallback = function()
        local pageName = "pageMapCollection"
        local options = {}
        options.sceneId = AzurWorldInstance.CurWorldId
        options.jumpParams = params
        local page = L_UI:getPage(pageName)
        if page ~= nil then
          L_UI:backToPage(pageName, options)
        else
          L_UI:open(pageName, options)
        end
      end
    })
  end
end

function this:checkMapCollectionJumpSystemUnlock(accessJumpId)
  if math.isEmpty(accessJumpId) then
    return true
  end
  local jumpCfg = _jumpTpl:getTplById(accessJumpId)
  if not jumpCfg then
    errorf("jumpId\228\184\141\229\173\152\229\156\168 = " .. tostring(accessJumpId))
    return false
  end
  if _jumpTpl:getType(jumpCfg) == L_Const.commonJumpType.MapCollectionTrace then
    if L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.mapCollectionTrace) then
      return true
    else
      return false
    end
  end
  return true
end

return this
