local this = class("pagePetDuelExit", G_UIPageBase)
local kiBoDuelTpl = L_GameTpl:getKiboDuelTpl()
local commonConditionTpl = L_GameTpl:getCommonConditionTpl()

function this.bind()
  return {
    module_commonTipSmall = L_Const.ModuleInfo.CommonTipSmall,
    targetList = {
      moduleName = "pages/petDuel/cellDuelTarget"
    }
  }
end

function this.methods()
  return {
    onClick_setting = function(self)
      L_UI:open("pageOption")
    end,
    module_commonTipSmall = {
      onClick_close = function(self)
        if self._params.closeCallbackTemp then
          self._params.closeCallbackTemp()
        end
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        if self._params.cancelCallback then
          self._params.cancelCallback()
        end
        L_UI:close(self.pageName)
      end,
      onClick_confirm = function(self)
        if self._params.confirmCallback then
          self._params.confirmCallback()
        end
        local hasNoMoreType = not math.isEmpty(self._params.showNoMoreType)
        if hasNoMoreType then
          if self._params.showNoMoreType == L_Const.ShowNoMoreType.curLogin then
            L_TipManager:setIsNeedOpen(not self.bind.go_showNoMore)
          elseif self._params.showNoMoreType == L_Const.ShowNoMoreType.today then
            C_PlayerPrefsUtility.SetInt(self._params.showNoMoreKey, L_TimeUtil.getNextDayTime(tonumber(L_GameConstTpl:getValue("DAILY_REFRESH_TIME"))))
          end
        end
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self:refreshPage(options)
end

function this:refreshPage(params)
  self._params = params or {}
  self.modules.module_commonTipSmall:initModule(self._params)
  if not params then
    return
  end
  local duelId = params.duelId
  local tpl = kiBoDuelTpl:getTplById(duelId)
  local duelTime = kiBoDuelTpl:getTime(tpl)
  local challengeList = kiBoDuelTpl:getChallenge(tpl)
  local tmp = {}
  for i = 1, #challengeList do
    local tpl_commonCondition = commonConditionTpl:getTplById(challengeList[i][1])
    local time = challengeList[i][2]
    local descShow = commonConditionTpl:getDescShowKey(tpl_commonCondition)
    table.insert(tmp, {
      time = time,
      descShow = string.gsub(descShow, "{time}", tostring(time)),
      duelTime = duelTime,
      leftTime = params.leftTime
    })
  end
  self.bind.targetList:clear()
  self.bind.targetList:insert_array(tmp)
end

function this:close()
  this.super.close(self)
  if self._params.pageClosedCallback then
    self._params.pageClosedCallback()
  end
end

function this:escHandle()
  if self._params.closeCallbackTemp then
    self._params.closeCallbackTemp()
  end
  L_UI:close(self.pageName)
end

return this
