local this = class("systemBreakManager")
local _systemTpl = L_GameTpl:getSystemUnlockTpl()

function this:jumpTo(systemId, ...)
  if not L_SystemUnlockManager:checkAndTip(systemId) then
    return
  end
  local quickName = this.quickBreakMap[systemId]
  if quickName then
    L_UI:open(quickName, ...)
  else
    local method = this.breakMap[systemId]
    if method then
      method(...)
    else
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("ui_cellNoviceTaskItem_05", {
        [0] = systemId
      }))
    end
  end
end

function this:checkBanSystemAndTip(systemId)
  if L_SystemUnlockStore:getSystemIsBan(systemId) then
    local reason = L_SystemUnlockStore:getSystemBanReason(systemId)
    local tip
    if not string.isEmpty(reason) then
      tip = reason
    else
      local tpl = _systemTpl:getTplById(systemId)
      tip = _systemTpl:getGmCloseTips(tpl)
    end
    if not string.isEmpty(tip) then
      L_FlyMsgManager:showNormalMsg(tip)
    end
    return true
  end
  return false
end

this.quickBreakMap = {
  [L_SystemConst.enum.mainMenu] = "pageMainMenu",
  [L_SystemConst.enum.gacha] = "pageRaffle",
  [L_SystemConst.enum.hero] = "pageHero",
  [L_SystemConst.enum.pet] = "pagePetBox",
  [L_SystemConst.enum.shop] = "pageMoneyShop",
  [L_SystemConst.enum.specialShop] = "pageSpecialShop",
  [L_SystemConst.enum.bag] = "pageBag",
  [L_SystemConst.enum.task] = "pageTask",
  [L_SystemConst.enum.achievement] = "pageAchievement",
  [L_SystemConst.enum.mail] = "pageMail",
  [L_SystemConst.enum.friend] = "pageFriends",
  [L_SystemConst.enum.formation] = "pageFormationMain",
  [L_SystemConst.enum.photo] = "pagePhoto",
  [L_SystemConst.enum.stamina] = "pageStamina",
  [L_SystemConst.enum.climbTower] = "pageTower",
  [L_SystemConst.enum.pamiTalk] = "pagePamiTalk",
  [L_SystemConst.enum.starManual] = "pageStarManual",
  [L_SystemConst.enum.announcemenet] = "pageAnnouncement",
  [L_SystemConst.enum.gameAct] = "pageGameAct",
  [L_SystemConst.enum.petDuel] = "pagePetDuelChapter",
  [L_SystemConst.enum.homeBag] = "pageHomeBag"
}
this.breakMap = {}
this.breakMap[L_SystemConst.enum.product] = function(callback)
  L_UI:open("pageProduct", {
    productType = L_Const.productType.cast
  }, callback)
end
this.breakMap[L_SystemConst.enum.chat] = function(option, callback)
  L_UI:open("pageChat", {uid = nil}, callback)
end
this.breakMap[L_SystemConst.enum.playerRename] = function(option, callback)
  L_PlayerManager:modifyName()
end
this.breakMap[L_SystemConst.enum.playerDisplayText] = function(option, callback)
  L_PlayerManager:modifySign()
end
this.breakMap[L_SystemConst.enum.playerCard] = function(option, callback)
  L_UI:open("pagePlayerCard", {
    uid = L_PlayerStore:getPlayerId()
  }, callback)
end
this.breakMap[L_SystemConst.enum.home] = function(option, callback)
  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
this.breakMap[L_SystemConst.enum.petStrength] = function(option, callback)
  if option ~= nil and option.petId ~= nil then
    L_UI:open("pagePet", option, callback)
    return
  end
  local petGuid = L_PetStore:getFirstOrderPet()
  L_UI:open("pagePet", {petId = petGuid}, callback)
end
this.breakMap[L_SystemConst.enum.mount] = function(option, callback)
  L_UI:backToRoot(function()
    L_UI:open("pageMountRoulette")
  end)
end
this.breakMap[L_SystemConst.enum.bagSpiritron] = function(option, callback)
  this.openBagByType(L_SystemConst.enum.bagSpiritron, callback)
end
this.breakMap[L_SystemConst.enum.bagAccessory] = function(option, callback)
  this.openBagByType(L_SystemConst.enum.bagAccessory, callback)
end
this.breakMap[L_SystemConst.enum.bagFood] = function(option, callback)
  this.openBagByType(L_SystemConst.enum.bagFood, callback)
end

function this.openBagByType(systemId, callback)
  local tpl = _systemTpl:getTplById(systemId)
  local param = _systemTpl:getParam(tpl)
  L_UI:open("pageBag", {bagType = param}, callback)
end

this.breakMap[L_SystemConst.enum.announcement] = function(option, callback)
  this.openStarManualTab(L_SystemConst.enum.announcement, callback)
end
this.breakMap[L_SystemConst.enum.spritMaterial] = function(option, callback)
  this.openStarManualTab(L_SystemConst.enum.spritMaterial, callback)
end
this.breakMap[L_SystemConst.enum.skillMaterial] = function(option, callback)
  this.openStarManualTab(L_SystemConst.enum.skillMaterial, callback)
end
this.breakMap[L_SystemConst.enum.wildBoss] = function(option, callback)
  this.openStarManualTab(L_SystemConst.enum.wildBoss, callback)
end
this.breakMap[L_SystemConst.enum.equipMaterial] = function(option, callback)
  this.openStarManualTab(L_SystemConst.enum.equipMaterial, callback)
end
this.breakMap[L_SystemConst.enum.weeklyBoss] = function(option, callback)
  this.openStarManualTab(L_SystemConst.enum.weeklyBoss, callback)
end

function this.openStarManualTab(systemId, callback)
  local tpl = _systemTpl:getTplById(systemId)
  local param = _systemTpl:getParam(tpl)
  L_UI:open("pageStarManual", {starManualId = param}, callback)
end

this.breakMap[L_SystemConst.enum.characterExpDungon] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.characterExpDungon)
end
this.breakMap[L_SystemConst.enum.spritExpDungon] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.spritExpDungon)
end
this.breakMap[L_SystemConst.enum.petExpDungon] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.petExpDungon)
end
this.breakMap[L_SystemConst.enum.coinDungon] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.coinDungon)
end
this.breakMap[L_SystemConst.enum.wildBossDungeon1] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.wildBossDungeon1)
end
this.breakMap[L_SystemConst.enum.wildBossDungeon2] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.wildBossDungeon2)
end
this.breakMap[L_SystemConst.enum.wildBossDungeon3] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.wildBossDungeon3)
end
this.breakMap[L_SystemConst.enum.wildBossDungeon4] = function(option, callback)
  this.dealOpenDugonBigMap(L_SystemConst.enum.wildBossDungeon4)
end

function this.dealOpenDugonBigMap(systemId)
  local sysTpl = _systemTpl:getTplById(systemId)
  local targetParam = _systemTpl:getTargetParam(sysTpl)
  L_UI:open("pageBigMap", {targetParam = targetParam, areaId = 100000})
end

this.breakMap[L_SystemConst.enum.spiritBreakDungon1] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.spiritBreakDungon1)
end
this.breakMap[L_SystemConst.enum.spiritBreakDungon2] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.spiritBreakDungon2)
end
this.breakMap[L_SystemConst.enum.spiritBreakDungon3] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.spiritBreakDungon3)
end
this.breakMap[L_SystemConst.enum.skillMaterialDungeon1] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.skillMaterialDungeon1)
end
this.breakMap[L_SystemConst.enum.skillMaterialDungeon2] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.skillMaterialDungeon2)
end
this.breakMap[L_SystemConst.enum.skillMaterialDungeon3] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.skillMaterialDungeon3)
end
this.breakMap[L_SystemConst.enum.skillMaterialDungeon4] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.skillMaterialDungeon4)
end
this.breakMap[L_SystemConst.enum.skillMaterialDungeon5] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.skillMaterialDungeon5)
end
this.breakMap[L_SystemConst.enum.warriorResource] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.warriorResource)
end
this.breakMap[L_SystemConst.enum.assassinResource] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.assassinResource)
end
this.breakMap[L_SystemConst.enum.archerResource] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.archerResource)
end
this.breakMap[L_SystemConst.enum.casterResource] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.casterResource)
end
this.breakMap[L_SystemConst.enum.supporterResource] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.supporterResource)
end
this.breakMap[L_SystemConst.enum.weeklyBossDungeon1] = function(option, callback)
  this.dealTeleport(L_SystemConst.enum.weeklyBossDungeon1)
end

function this.dealTeleport(systemId)
  local sysTpl = _systemTpl:getTplById(systemId)
  local markId = _systemTpl:getDugenon(sysTpl)
  local worldFilterMarkTpl = L_GameTpl:getWorldFilterMarkTpl()
  local tpl = worldFilterMarkTpl:getTplById(markId)
  local doorId = worldFilterMarkTpl:getParam(tpl)
  local worldId = worldFilterMarkTpl:getWorldId(tpl)
  local posId = worldFilterMarkTpl:getPosId(tpl)
  local worldmapId = worldFilterMarkTpl:getWorldmapId(tpl)
  L_SceneStore:transferToPoint(worldId, posId, L_Const.WorldMapTransportType.WMTT_MINIMAP, function()
    this.checkInitTimer = Timer.repeated(0.1, function()
      if L_UI:checkPageOpen("pageAttendance") or L_UI:checkPageOpen("pageReward") then
        if L_UI:checkPageOpen("pageBlank") then
          L_UI:close("pageBlank")
        end
        return
      end
      if not L_LoadingManager:isLoading() then
        if not L_UI:checkPageOpen("pageBlank") then
          L_UI:open("pageBlank", {timeClose = 10})
        end
        local entity = AzurWorld.WorldMapMgr:TryGetEntity(worldmapId)
        if entity then
          L_UI:open("pageDungeonEntry", {entity = entity, doorId = doorId})
          Timer.once(1, function()
            L_UI:close("pageBlank")
          end)
          Timer.remove(this.checkInitTimer)
          this.checkInitTimer = nil
        end
      end
    end)
  end)
end

this.breakMap[L_SystemConst.enum.adventure] = function(option)
  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.jumpToMoneyShop(subSysTemId, option, callback)
  local func = function()
    local tpl = _systemTpl:getTplById(subSysTemId)
    local param = _systemTpl:getParam(tpl)
    option = option or {}
    option.shopId = param
    local page = L_UI:getPage("pageMoneyShop")
    if L_UI:checkPageShown("pageMoneyShop") then
      page.cls:refresh(option, callback)
    else
      L_UI:open("pageMoneyShop", option, callback)
    end
  end
  if subSysTemId == L_SystemConst.enum.moneyShopRecharge then
    local data = {
      txtTitle = "",
      txtContent = L_WordsTpl:getValue("notice_shop_purchase_diamond"),
      confirmCallback = func
    }
    L_GameUtil.showCommonTip(data)
  else
    func()
  end
end

this.breakMap[L_SystemConst.enum.moneyShopRecharge] = function(...)
  this.jumpToMoneyShop(L_SystemConst.enum.moneyShopRecharge, ...)
end
this.breakMap[L_SystemConst.enum.moneyShopYellow] = function(...)
  this.jumpToMoneyShop(L_SystemConst.enum.moneyShopYellow, ...)
end
this.breakMap[L_SystemConst.enum.moneyShopGreen] = function(...)
  this.jumpToMoneyShop(L_SystemConst.enum.moneyShopGreen, ...)
end
this.breakMap[L_SystemConst.enum.moneyShopGift] = function(...)
  this.jumpToMoneyShop(L_SystemConst.enum.moneyShopGift, ...)
end
this.breakMap[L_SystemConst.enum.moneyShopCity] = function(...)
  this.jumpToMoneyShop(L_SystemConst.enum.moneyShopCity, ...)
end
this.breakMap[L_SystemConst.enum.moneyRaffleGoods] = function(...)
  this.jumpToMoneyShop(L_SystemConst.enum.moneyRaffleGoods, ...)
end
this.breakMap[L_SystemConst.enum.science] = function(...)
  this.jumpToPageHero(L_SystemConst.enum.science, ...)
end
this.breakMap[L_SystemConst.enum.skill] = function(...)
  this.jumpToPageHero(L_SystemConst.enum.skill, ...)
end
this.breakMap[L_SystemConst.enum.star] = function(...)
  this.jumpToPageHero(L_SystemConst.enum.star, ...)
end
this.breakMap[L_SystemConst.enum.spiritron] = function(...)
  this.jumpToPageHero(L_SystemConst.enum.spiritron, ...)
end
this.breakMap[L_SystemConst.enum.accessory] = function(...)
  this.jumpToPageHero(L_SystemConst.enum.accessory, ...)
end
this.breakMap[L_SystemConst.enum.heroData] = function(...)
  this.jumpToPageHero(L_SystemConst.enum.heroData, ...)
end

function this.jumpToPageHero(subSysTemId, option, callback)
  local func = function()
    option = option or {}
    if subSysTemId == L_SystemConst.enum.science then
      option.selectModuleType = L_Const.heroModuleType.Gift
    elseif subSysTemId == L_SystemConst.enum.skill then
      option.selectModuleType = L_Const.heroModuleType.Skill
    elseif subSysTemId == L_SystemConst.enum.star then
      option.selectModuleType = L_Const.heroModuleType.Star
    elseif subSysTemId == L_SystemConst.enum.spiritron then
      option.selectModuleType = L_Const.heroModuleType.Spiritron
    elseif subSysTemId == L_SystemConst.enum.accessory then
      option.selectModuleType = L_Const.heroModuleType.Accessory
    elseif subSysTemId == L_SystemConst.enum.heroData then
      option.selectModuleType = L_Const.heroModuleType.Data
    end
    local page = L_UI:getPage("pageHero")
    if L_UI:checkPageShown("pageHero", true) then
      page.cls:refresh(option, callback)
    else
      L_UI:open("pageHero", option, callback)
    end
  end
  func()
end

this.breakMap[L_SystemConst.enum.activation] = function(option, callback)
  if not L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.activation) and not L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.noviceTask) then
    return
  elseif L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.activation) then
    L_UI:open("pageActivityTask", option, callback)
  elseif L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.noviceTask) then
    if L_NoviceTaskStore:checkIfAllTaskCompletedAndAllRewardReceived() == true then
      return
    end
    L_UI:open("pageActivityTask")
  end
end
this.breakMap[L_SystemConst.enum.homeScience] = function(option, callback)
  local nextDayTimeKey = string.format("pageHomeScienceEnter_%d", L_PlayerStore:getPlayerId())
  local lastTime = C_PlayerPrefsUtility.GetInt(nextDayTimeKey) or 0
  if lastTime > L_TimeUtil.getServerTime() then
    L_UI:open("pageHomeScienceNew", option, callback)
  else
    L_UI:open("pageHomeScienceEnter", option, callback)
  end
end
this.breakMap[L_SystemConst.enum.noviceTask] = function(option, callback)
  if not L_SystemUnlockStore:getSystemIsOpen(L_SystemConst.enum.noviceTask) then
    return
  end
  if L_NoviceTaskStore:checkIfAllTaskCompletedAndAllRewardReceived() then
    return
  end
  if L_DungeonManager:getIsInTrainTaskDungeon() then
    return
  end
  L_LoadingManager:openSaveFormationLoading(nil, function()
    L_UI:open("pageNoviceTask", option, callback)
  end)
end
return this
