local this = class("pageDungeonReward", G_UIPageBase)
local _staminaChestTpl = L_GameTpl:getStaminaChestDropTpl()
local ShowType = {NormalDungeon = 1, WorldBoss = 2}

function this.bind()
  return {
    go_up_left = false,
    txt_leftTimes = nil,
    go_leftTimes = false,
    title_txt = "",
    list_icon = L_Const.ModuleInfo.CellIconBag,
    go_up_btn = false,
    txt_upLeftTimes = nil,
    module_currency = L_Const.ModuleInfo.Currency,
    go_dropClickMask = false,
    detailedInfoList1 = {
      moduleName = "pages/reward/rewardTimesItemOption"
    },
    moduleCommonTipMedium = L_Const.ModuleInfo.CommonTipMedium
  }
end

function this.methods()
  return {
    detailedInfoList1 = {
      onChange = function(self, val)
        self:changeSetting(val)
        self.bind.go_dropClickMask = false
      end,
      changeDropdown = function(self, callback)
        self.dropMaskCallback = callback
        self.bind.go_dropClickMask = true
      end
    },
    onClick_changeDropdown = function(self)
      self.bind.go_dropClickMask = false
      if self.dropMaskCallback then
        self.dropMaskCallback()
      end
    end,
    moduleCommonTipMedium = {
      onClick_close = function(self)
        L_UI:close(self.pageName)
      end,
      onClick_cancel = function(self)
        self:cancelReward()
      end,
      onClick_confirm = function(self)
        self:checkGetReward()
      end,
      onClick_bg = function(self)
        L_UI:close(self.pageName)
      end
    }
  }
end

function this:check(options, callback)
  local tpl = _staminaChestTpl:getTplById(options.staminaChestId)
  local lastTime = _staminaChestTpl:getTime(tpl)
  local serverData = L_StaminaChestStore:getStaminaBox(options.staminaChestId)
  if L_TimeUtil.getServerTime() >= serverData.finish_time + lastTime then
    L_FlyMsgManager:showNormalMsgByKey("ui_pageStaminaReward_02")
    callback(false)
    return
  end
  callback(true)
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  self._staminaChestId = options.staminaChestId
  self.openFromType = options.openFromType
  self._callback = options.callback
  self.rewardTimes = 1
  self.staminaReward = {}
  table.insert(self.staminaReward, 1)
  local rewardTql = L_GameTpl:getGameConstTpl():getEntrustStaminaReward()
  for i = 1, #rewardTql do
    table.insert(self.staminaReward, rewardTql[i])
  end
  self:refreshView()
  local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplById(L_SystemConst.enum.mainMenu)
  self.modules.module_currency:refreshRes(tpl_sys)
  self.modules.detailedInfoList1:refresh_dropMenu(self.staminaReward)
  local params = {}
  if self.openFromType == ShowType.NormalDungeon then
    params.txtTitle = L_WordsTpl:getValue("ui_stamina_chest_01")
    params.txtCancel = L_WordsTpl:getValue("dungeon_entrust_stamina_no_get")
    params.txtConfirm = L_WordsTpl:getValue("dungeon_entrust_stamina_get")
  elseif self.openFromType == ShowType.WorldBoss then
    params.txtTitle = L_WordsTpl:getValue("ui_stamina_chest_01")
    params.txtCancel = L_WordsTpl:getValue("ui_stamina_chest_03")
    params.txtConfirm = L_WordsTpl:getValue("ui_stamina_chest_02")
  end
  self.modules.moduleCommonTipMedium:initModule(params)
end

function this:refreshView()
  local tpl_chest = _staminaChestTpl:getTplById(self._staminaChestId)
  local leftRewardTime, isUp = L_StaminaChestManager:getStaminaRewardTimes(self._staminaChestId)
  self.bind.go_up_btn = isUp
  self.bind.go_up_left = isUp
  if isUp then
    self.bind.txt_upLeftTimes = L_WordsTpl:getValue("ui_pageStaminaReward_01", {
      [0] = leftRewardTime
    })
    self.bind.go_leftTimes = true
    self.bind.txt_leftTimes = tostring(leftRewardTime)
  else
    local refreshType, canRewardTimes = table.unpack(_staminaChestTpl:getRewardChance(tpl_chest))
    local isInfinite = refreshType == L_Const.StaminaBoxCountType.SBCT_INFINITE
    self.bind.go_leftTimes = not isInfinite
    if not isInfinite then
      self.bind.txt_leftTimes = tostring(leftRewardTime)
    end
  end
  local rewardlist = isUp and _staminaChestTpl:getRewardDisplayUp(tpl_chest) or _staminaChestTpl:getRewardDisplay(tpl_chest)
  local rewardTimesItems = {}
  for i = 1, #rewardlist do
    local temp = {}
    temp[1] = rewardlist[i][1]
    temp[2] = rewardlist[i][2]
    temp[3] = rewardlist[i][3] * self.rewardTimes
    if rewardlist[i][4] then
      temp[4] = rewardlist[i][4] * self.rewardTimes
    end
    table.insert(rewardTimesItems, temp)
  end
  local actualReward = L_DataUtil.parseRangeRewardConfig(rewardTimesItems)
  self.bind.list_icon:clear()
  self.bind.list_icon:insert_array(actualReward)
  local currencyConfigData = L_ItemTplManager:getCurrencyItem(L_Const.currencyType.stamina)
  local needCost = L_DataUtil.parseRewardConfig(_staminaChestTpl:getNeed(tpl_chest))[1]
  self.bind.title_txt = string.format(L_WordsTpl:getValue("ui_pageStaminaReward_03", {
    [0] = "<p=%s w=25 h=25/><color=#3266b4>%s</color>"
  }), currencyConfigData.icon, needCost.itemNum * self.rewardTimes)
end

function this:checkGetReward()
  local tpl = _staminaChestTpl:getTplById(self._staminaChestId)
  local lastTime = _staminaChestTpl:getTime(tpl)
  local serverData = L_StaminaChestStore:getStaminaBox(self._staminaChestId)
  if L_TimeUtil.getServerTime() >= serverData.finish_time + lastTime then
    L_FlyMsgManager:showNormalMsgByKey("ui_pageStaminaReward_02")
    L_UI:close(self.pageName)
    return
  end
  local needCost = L_DataUtil.parseRewardConfig(_staminaChestTpl:getNeed(tpl))[1]
  if L_ItemTplManager:getItemNum(needCost.itemType, needCost.itemId) < needCost.itemNum * self.rewardTimes then
    L_FlyMsgManager:showNormalMsg(L_GameUtil.getStrByKey("notice_dungeon_lackenergy"))
    return
  end
  L_StaminaChestStore:req_CSProtoStaminaBoxGet(self._staminaChestId, self.rewardTimes, function(msg, errCode)
    if errCode == L_Const.errorCode.ErrCodeSucc then
      L_UI:close(self.pageName, nil, function()
        if self.openFromType == ShowType.NormalDungeon then
          local list = C_VarList:Get()
          list:AddObject(msg.rewards)
          C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnDungeonEntrustChestAcquire, list)
        elseif self.openFromType == ShowType.WorldBoss then
          L_RewardManager:showPage(msg.rewards, msg.src)
        end
        if self._callback then
          self._callback()
        end
      end)
    end
  end)
end

function this:cancelReward()
  L_UI:close(self.pageName)
  if self.openFromType == ShowType.NormalDungeon then
    local list = C_VarList:Get()
    C_WorldEvent.instance:Dispatch(C_EWorldEvent.OnDungeonEntrustChestAcquire, list)
  end
end

function this:changeSetting(index)
  self.bind.list_icon:clear()
  self.rewardTimes = self.staminaReward[index]
  self:refreshView()
end

return this
