local this = class("moduleDungeonCountdown", G_UIModuleBase)

function this.bind()
  return {txt_countdown = nil, go_content = false}
end

function this.methods()
  return {}
end

function this:open()
  L_DungeonStore:listenCallFunc(L_DungeonStore.event.syncCurDungeon, self.onEvent_syncCurDungeon, self)
  L_MultiDungeonStore:listenCallFunc(L_MultiDungeonStore.event.syncCurMultiDungeonInfo, self.onEvent_syncCurMultiDungeon, self)
  self:dealWithDungeonStatus()
end

function this:close()
  printf("moduleDungeonCountdown", "close")
  L_DungeonStore:unListenCallFunc(L_DungeonStore.event.syncCurDungeon, self.onEvent_syncCurDungeon)
  L_MultiDungeonStore:unListenCallFunc(L_MultiDungeonStore.event.syncCurMultiDungeonInfo, self.onEvent_syncCurMultiDungeon)
  L_TimerManager:clearTimer(self)
end

function this:dealWithDungeonStatus()
  printf("moduleDungeonCountdown", "dealWithDungeonStatus")
  local dungeonState = L_DungeonStore:getStatus()
  self._isWin = L_WorldManager:getCurModule() == L_Const.worldModule.dungeon and dungeonState == CS.Protocal.Cs.CampaignStatusType.CstWin
  self.bind.go_content = self._isWin
  if self._isWin then
    self:openDungeonWinCountdown()
  elseif dungeonState == CS.Protocal.Cs.CampaignStatusType.CstLose then
  end
end

function this:openDungeonWinCountdown()
  printf("moduleDungeonCountdown", "openDungeonWinCountdown")
  if not self._isWin then
    return
  end
  local curDungeon = L_DungeonStore:getDungeonCampaignInfoCSData()
  self._endTime = curDungeon.endTime + L_GameClientTpl:getValue("DUNGEON_STAY_TIME")
  local func = function()
    local nowTime = L_TimeUtil.getServerTime()
    local leftTime = self._endTime - nowTime
    self.bind.txt_countdown = L_WordsTpl:getValue("notice_pageMultiDungeonSettle", {
      [0] = leftTime
    })
    if leftTime <= 0 then
      L_TimerManager:stopTimer(self, "win_countdown")
      L_DungeonManager:exitDungeon(false)
    end
  end
  L_TimerManager:newOrResetTimer(self, "win_countdown", func, 1, -1)
  func()
end

function this:onEvent_syncCurDungeon()
  self:dealWithDungeonStatus()
end

function this:dealWithMultiDungeonStatus()
  local dungeonState = L_MultiDungeonStore:getCurMultiDungeonInfo().status
  self._isWin = L_WorldManager:getCurModule() == L_Const.worldModule.multiDungeon and dungeonState == L_Const.CampaignStatusType.CST_WIN
  self.bind.go_content = self._isWin
  if self._isWin then
    self:openMultiDungeonWinCountdown()
  elseif dungeonState == L_Const.CampaignStatusType.CST_LOSE then
  end
end

function this:openMultiDungeonWinCountdown()
  if not self._isWin then
    return
  end
  local curDungeon = L_MultiDungeonStore:getCurMultiDungeonInfo()
  self._endTime = curDungeon.end_time + 30
  local func = function()
    local nowTime = L_TimeUtil.getServerTime()
    local leftTime = self._endTime - nowTime
    self.bind.txt_countdown = L_WordsTpl:getValue("notice_moduleDungeonCountdown", {
      [0] = leftTime
    })
    if leftTime <= 0 then
      L_TimerManager:stopTimer(self, "win_countdown")
      L_UI:backToRoot(function()
        L_UI:open("pageMultiDungeonSettle")
      end)
    end
  end
  L_TimerManager:newOrResetTimer(self, "win_countdown", func, 0.5, -1)
  func()
end

function this:removeTimer()
  if self.timer then
    L_GameTimer.remove(self.timer)
    self.timer = nil
  end
end

function this:closeCountdown()
  L_TimerManager:clearTimer(self)
  self.bind.go_content = false
end

function this:onEvent_syncCurMultiDungeon()
  self:dealWithMultiDungeonStatus()
end

function this:activate()
  self:open()
  self:openModules()
  self:show()
  self:showModules()
end

function this:deActivate()
  self:hide()
  self:hideModules()
  self:close()
  self:closeModules()
end

return this
