local this = class("pageRaffle", G_UIPageBase)
local _gachaTpl = L_GameTpl:getGachaTpl()

function this:ctor(...)
  this.super.ctor(self, ...)
  self.data = {
    endTime = nil,
    selectCardPoolId = nil,
    selectRaffleTimes = 1
  }
end

function this.bind()
  return {
    go_upViewBtn = false,
    txt_todayTime = "",
    img_currency_one = "",
    img_currency_ten = "",
    module_currency = {
      moduleName = "modulePages/moduleCurrency"
    },
    scrollList_cardPool = {
      moduleName = "pages/Raffle/cellCardPool"
    }
  }
end

function this.methods()
  return {
    onClick_back = function(self)
      L_UI:close(self.pageName)
    end,
    onClick_root = function(self)
      L_UI:backToRoot()
    end,
    onClick_viewInfo = function(self)
      L_UI:open("pageRaffleInfo", {
        poolId = self.data.selectCardPoolId
      })
    end,
    onClick_history = function(self)
      L_UI:open("pageRaffleHistory")
    end,
    onClick_one = function(self)
      self.data.selectRaffleTimes = 1
      self:raffleConfirm()
    end,
    onClick_ten = function(self)
      self.data.selectRaffleTimes = 10
      self:raffleConfirm()
    end,
    onClick_viewUpInfo = function(self)
      L_UI:open("pageRaffleUpInfo", {
        poolId = self.data.selectCardPoolId
      })
    end,
    scrollList_cardPool = {
      onClick_select = function(self, bind)
        if not bind.go_active then
          self.data.selectCardPoolId = bind.id
          self:setCardPoolListActive()
          self:initCurrencyModule()
          self:initCardPoolInfo()
        end
      end
    }
  }
end

function this:check(options, callback)
  local result = L_SystemUnlockManager:checkAndTip(L_SystemConst.enum.gacha)
  if result then
    result = false
    local gachaPoolList = _gachaTpl:getAllTpl()
    for i, v in pairs(gachaPoolList) do
      local serverData = L_RaffleStore:getCardPool(_gachaTpl:getGachaId(v))
      if _gachaTpl:getIsInOpenTime(v) and not L_RaffleStore:getIsPoolTotalLimit(serverData) then
        result = true
        break
      end
    end
    if not result then
      L_FlyMsgManager:showNormalMsg(L_WordsTpl:getValue("notice_no_open_gacha"))
    end
  end
  callback(result)
end

function this:preOpen(options)
  this.super.preOpen(self, options)
  L_RaffleStore:listenCallFunc(L_RaffleStore.event.refreshCardPoolList, self.onEvent_refreshPoolInfo, self)
  options = options or {}
  if options.selectTab then
    self.data.selectCardPoolId = options.selectTab
  end
  if options.jumpParams then
    self.data.selectCardPoolId = tonumber(options.jumpParams[1])
  end
  self:initPage()
end

function this:show()
  this.super.show(self)
  self:initPage()
end

function this:close(options)
  this.super.close(self, options)
  L_RaffleStore:unListenCallFunc(L_RaffleStore.event.refreshCardPoolList, self.onEvent_refreshPoolInfo)
end

function this:initPage()
  self:initCardPoolList()
  self:initCurrencyModule()
  self:initCardPoolInfo()
end

function this:initCardPoolList()
  local gachaPoolList = _gachaTpl:getAllTpl()
  local tmp = {}
  for i, v in pairs(gachaPoolList) do
    local serverData = L_RaffleStore:getCardPool(_gachaTpl:getGachaId(v))
    if _gachaTpl:getIsInOpenTime(v) and not L_RaffleStore:getIsPoolTotalLimit(serverData) then
      table.insert(tmp, {
        id = _gachaTpl:getGachaId(v),
        img_cardPool = nil
      })
    end
  end
  if math.isEmpty(self.data.selectCardPoolId) then
    self.data.selectCardPoolId = tmp[1].id
  else
    local node, index = table.ipairsFind(tmp, function(v)
      return v.id == self.data.selectCardPoolId
    end)
    if not index then
      self.data.selectCardPoolId = tmp[1].id
    end
  end
  self.bind.scrollList_cardPool:clear()
  self.bind.scrollList_cardPool:insert_array(tmp)
  self:setCardPoolListActive()
end

function this:initCurrencyModule()
  local tpl_sys = L_GameTpl:getSystemUnlockTpl():getTplByTab(L_SystemConst.enum.gacha, self.data.selectCardPoolId)
  self.modules.module_currency:refreshRes(tpl_sys)
end

function this:initCardPoolInfo()
  local tpl = _gachaTpl:getTplById(self.data.selectCardPoolId)
  L_ReddotManager:markNew(L_ReddotManager.DotDef.Gacha, self.data.selectCardPoolId)
  local serverData = L_RaffleStore:getCardPool(self.data.selectCardPoolId)
  self.bind.txt_todayTime = L_WordsTpl:getValue("notice_pageRaffle_01", {
    [0] = L_RaffleStore:getCardPoolTodayNum(serverData),
    [1] = _gachaTpl:getCanUsedNum(tpl)
  })
  local cost = _gachaTpl:getCostId(tpl)
  local cost_item = L_ItemManager:parseItem(cost[1], cost[2])
  self.bind.img_currency_one = cost_item.icon
  self.bind.img_currency_ten = cost_item.icon
  self.bind.go_upViewBtn = _gachaTpl:getGachaType(tpl) == L_Const.cardPoolType.timeHero or _gachaTpl:getGachaType(tpl) == L_Const.cardPoolType.timeWeapon
end

function this:setCardPoolListActive()
  for i = 1, #self.bind.scrollList_cardPool do
    local active = false
    if self.bind.scrollList_cardPool:getValue(i, "id") == self.data.selectCardPoolId then
      active = true
    end
    self.bind.scrollList_cardPool:change(i, {
      go_active = active,
      go_normal = not active
    })
  end
end

function this:onEvent_refreshPoolInfo()
  self:initPage()
end

function this:openDateChange()
end

function this:closeDateChange()
end

function this:closeRecordTimer()
  if self.recordTimer == nil then
    return
  end
  self.recordTimer:destroy()
  self.recordTimer = nil
end

function this:initTimerInfo(poolInfo)
  local nowTime = L_TimeUtil:getServerTime()
  if not L_TimeUtil.isSameDay(poolInfo.lastRaffleTime, nowTime) then
    self.bind.dairyCountLimit = L_WordsTpl:getValue("notice_pageRaffle_02", {
      [0] = 0,
      [1] = poolInfo.gachaDairyLimit
    })
  else
    self.bind.dairyCountLimit = L_WordsTpl:getValue("notice_pageRaffle_02", {
      [0] = poolInfo.day_count,
      [1] = poolInfo.gachaDairyLimit
    })
  end
end

function this:checkRaffle()
  local serverData = L_RaffleStore:getCardPool(self.data.selectCardPoolId)
  if L_RaffleStore:getIsPoolTodayLimit(serverData) then
    L_FlyMsgManager:showNormalMsgByKey("notice_pageRaffle_04")
    return false
  end
  if L_RaffleStore:getIsPoolTodayLimit(serverData, self.data.selectRaffleTimes) then
    L_FlyMsgManager:showNormalMsgByKey("notice_pageRaffle_05")
    return false
  end
  local tpl = _gachaTpl:getTplById(self.data.selectCardPoolId)
  if L_TimeUtil:getServerTime() >= _gachaTpl:getEndTime(tpl) and _gachaTpl:getEndTime(tpl) ~= 0 then
    L_FlyMsgManager:showNormalMsgByKey("notice_pageRaffle_06")
    return false
  end
  local cost = _gachaTpl:getCostId(tpl)
  local hadNum = L_BagStore:getItemNum(cost[2])
  local costNum = _gachaTpl:getCostNum(tpl, self.data.selectRaffleTimes)
  if hadNum < costNum then
    local data_tip = {
      txtTitle = L_WordsTpl:getValue("notice_pageRaffle_07"),
      txtContent = L_WordsTpl:getValue("notice_pageRaffle_08"),
      confirmCallback = function()
        L_UI:open("pageMoneyShop")
      end
    }
    L_GameUtil.showCommonTip(data_tip)
    return false
  end
  return true
end

function this:raffleConfirm()
  if self:checkRaffle() then
    local tpl = _gachaTpl:getTplById(self.data.selectCardPoolId)
    local cost = _gachaTpl:getCostId(tpl)
    local cost_item = L_ItemManager:parseItem(cost[1], cost[2])
    local cost_num = _gachaTpl:getCostNum(tpl, self.data.selectRaffleTimes)
    local describe = L_WordsTpl:getValue("notice_pageRaffle_03", {
      [0] = cost_item.icon,
      [1] = cost_num,
      [2] = self.data.selectRaffleTimes,
      [3] = _gachaTpl:getGachaName(tpl)
    })
    local data_tip = {
      txtTitle = L_WordsTpl:getValue("notice_pageRaffle_09"),
      txtContent = describe,
      confirmCallback = function()
        if self:checkRaffle() then
          L_RaffleStore:req_raffle(self.data.selectCardPoolId, self.data.selectRaffleTimes, function()
            L_UI:open("pageRaffleResultOne")
          end)
        end
      end
    }
    L_GameUtil.showCommonTip(data_tip)
  end
end

function this:enableSpiritChooseModule(bool)
  self.bind.spiritChooseModuleName = bool == true and "spiritChooseModule" or ""
end

return this
