local this = class("dungeonManager")
local _staminaChestDropTpl = L_GameTpl:getStaminaChestDropTpl()
local _dungeonTpl = L_GameTpl:getDungeonTpl()
local _trainTaskTpl = L_GameTpl:getTrainTaskTpl()

function this:getDungeonId()
  local sceneId = AzurWorldInstance.CurWorldId
  return sceneId
end

function this:getIsInTrainTaskDungeon()
  if not self:getIsInDungeon() then
    return false
  end
  local curDungeonId = L_DungeonStore:getDungeonCampaignInfoCSData().dungeonId
  local tpl_dungeon = _dungeonTpl:getTplById(curDungeonId)
  return _dungeonTpl:getDungeonType(tpl_dungeon) == L_DungeonConst.DungeonType.TrainTask
end

function this:getIsInDungeon()
  local sceneId = AzurWorldInstance.CurWorldId
  local cityTpl = L_GameTpl:getWorldCityTpl()
  local tpl = cityTpl:getTplById(sceneId)
  return cityTpl:getType(tpl) == 2
end

function this:getIsRandomDungeon()
  return false
end

function this:getMainDungeonId()
  if not self:getIsInDungeon() then
    return
  end
  return AzurWorldInstance.CurWorldId
end

function this:createDungeon(groupId, difficult)
  if math.isEmpty(groupId) or math.isEmpty(difficult) then
    return
  end
  L_DungeonStore:req_createDungeon(groupId, difficult)
end

function this:parseDungeonSceneMap()
end

function this:isStaminaChestReceive()
  local curDungeonId = L_DungeonStore:getDungeonCampaignInfoCSData().dungeonId
  print("curDungeonId", curDungeonId)
  local tpl = _staminaChestDropTpl:getTplByWorldFilter(L_Const.StaminaBoxSystemType.SBST_CAMPAIGN, curDungeonId)
  if not tpl then
    return true
  end
  local staminaChestId = _staminaChestDropTpl:getId(tpl)
  local serverData = L_StaminaChestStore:getStaminaBox(staminaChestId)
  print("serverData.finish_time", serverData.finish_time)
  return serverData.finish_time == 0
end

function this:isReward()
  local reward = L_DungeonStore:getReward()
  local count = 0
  if reward == nil then
    return false
  end
  if reward.rewards == nil then
    return false
  end
  if reward.rewards.Count ~= nil then
    count = reward.rewards.Count
  else
    count = #reward.rewards
  end
  return reward ~= nil and reward.rewards ~= nil and 0 < count
end

function this:exitDungeon(isProactive)
  if isProactive then
    if self:isReward() then
      local callback = function()
        self:reqExitDungeon()
      end
      L_RewardManager:showFullPageReward(L_DungeonStore:getReward().rewards, callback, "\229\165\150\229\138\177")
      return
    end
    if self:isStaminaChestReceive() then
      L_GameUtil.showCommonTip({
        txtContent = "\231\161\174\229\174\154\233\128\128\229\135\186\229\137\175\230\156\172?",
        confirmCallback = function()
          if L_FormationManager:isRevivingHeros() then
            return
          end
          L_DungeonStore:req_exitDungeon()
        end
      })
    else
      L_GameUtil.showCommonTip({
        txtContent = L_WordsTpl:getValue("notice_exit_dungeon"),
        confirmCallback = function()
          if L_FormationManager:isRevivingHeros() then
            return
          end
          L_DungeonStore:req_exitDungeon()
        end
      })
    end
  else
    printf("dungeonManager", "exitDungeon")
    L_DungeonStore:req_exitDungeon()
  end
end

function this:onEvent_closePage(pageName)
  if pageName == "pageReward" then
    self:reqExitDungeon()
  end
end

function this:reqExitDungeon()
  printf("dungeonManager", "exitDungeon")
  L_DungeonStore:req_exitDungeon()
end

function this:restartTrainTask()
  if not self:getIsInTrainTaskDungeon() then
    return
  end
  local curDungeonId = L_DungeonStore:getDungeonCampaignInfoCSData().dungeonId
  local taskId = _trainTaskTpl:getTaskIdByDungeonId(curDungeonId)
  L_EntrustStore:req_CSProtoTeachStart(taskId)
end

return this
