local this = class("pageRaffleResultOne", G_UIPageBase)
local _gachaTpl = L_GameTpl:getGachaTpl()
local IMG_QUALITY = {
  [L_Const.quality.n] = "UI/Texture/RaffleBg/tex_gacha_bg_pinzhi_gx_r.png",
  [L_Const.quality.r] = "UI/Texture/RaffleBg/tex_gacha_bg_pinzhi_gx_r.png",
  [L_Const.quality.sr] = "UI/Texture/RaffleBg/tex_gacha_bg_pinzhi_gx_sr.png",
  [L_Const.quality.ssr] = "UI/Texture/RaffleBg/tex_gacha_bg_pinzhi_gx_ssr.png"
}
local IMG_QUALITY2 = {
  [L_Const.quality.n] = "Page/BSGacha/tex_gacha_word_pinzhi_3",
  [L_Const.quality.r] = "Page/BSGacha/tex_gacha_word_pinzhi_3",
  [L_Const.quality.sr] = "Page/BSGacha/tex_gacha_word_pinzhi_2",
  [L_Const.quality.ssr] = "Page/BSGacha/tex_gacha_word_pinzhi_1"
}

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {isSkip = false}
end

function this.bind()
  return {
    img_quality_bg = "",
    img_quality = "",
    img_type = "",
    go_type = false,
    go_img_hero = false,
    go_img_weapon = false,
    img_hero = "",
    img_weapon = "",
    img_quality_weapon = "",
    txt_name = "",
    txt_desc = "",
    go_new = false,
    go_extraReward = false,
    go_skipBtn = false,
    scrollList_extraReward = {
      moduleName = "modulePages/cellIcon"
    }
  }
end

function this.methods()
  return {
    onClick_continue = function(self)
      local nextResult = L_RaffleStore:getNextResult(self.data.isSkip)
      if nextResult then
        self:initResultInfo(nextResult)
      else
        L_UI:close(self.pageName)
        if L_RaffleStore:getResultNum() > 1 then
          L_UI:open("pageRaffleResultTen")
        end
      end
    end,
    onClick_skip = function(self)
      self.data.isSkip = true
      self.bind.go_skipBtn = false
      self.methods.onClick_continue(self)
    end
  }
end

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

function this:close(options)
  this.super.close(self, options)
  self:stopCurVoice()
end

function this:initPage()
  local serverData = L_RaffleStore:getFirstResult()
  self.bind.go_skipBtn = L_RaffleStore:getResultNum() > 1
  self:initResultInfo(serverData)
end

function this:initResultInfo(serverData)
  local item = L_ItemManager:parseItem(serverData.mainPropType, serverData.mainPropId)
  L_AudioUtil.playSESound("card_show")
  self.bind.img_quality_bg = IMG_QUALITY[item.quality]
  self.bind.img_quality = IMG_QUALITY2[item.quality]
  self:stopCurVoice()
  self.bind.go_type = item.itemType == L_Const.resType.hero
  if item.itemType == L_Const.resType.hero then
    self.bind.go_img_hero = true
    self.bind.go_img_weapon = false
    self.bind.img_type = L_Const.professionType[item.position]
    self.curVoice = L_PlayerManager:playHeroVoice(serverData.mainPropId, L_Const.cvType.Obtain)
  else
    self.bind.go_img_hero = false
    self.bind.go_img_weapon = true
    self.bind.img_weapon = item.icon
    self.bind.img_quality_weapon = item.imgQuality
  end
  self.bind.txt_name = item.name
  self.bind.txt_desc = item.desc
  self.bind.go_new = serverData.new
  self.bind.go_extraReward = false
  self:initAttachRewardList(serverData.attachRewards)
end

function this:initAttachRewardList(rewards)
  self.bind.go_extraReward = not table.isEmpty(rewards)
  local tmp = {}
  for i, v in pairs(rewards) do
    table.insert(tmp, {
      itemType = v.type,
      itemId = v.id,
      itemNum = v.num
    })
  end
  self.bind.scrollList_extraReward:clear()
  self.bind.scrollList_extraReward:insert_array(tmp)
end

function this:stopCurVoice()
  if L_CommonUtil.isValid(self.curVoice) then
    C_AudioManager.FreeSource(self.curVoice)
  end
  self.curVoice = nil
end

return this
