local this = {}

function this:init()
  this.super.init(self)
  self.data = {
    curCardIndex = 1,
    historyList = {},
    resultList = {},
    cardPoolList = {}
  }
end

function this:state()
  return {}
end

function this:getCurCardIndex()
  return self.data.curCardIndex
end

function this:getHistoryList()
  return self.data.historyList
end

function this:getResultList()
  return self.data.resultList
end

function this:getCardPoolList()
  return self.data.cardPoolList
end

function this:getHistoryPageCount(cardPoolType)
  local historyList = self:getHistoryList()
  local count = 0
  local gachaTpl = L_GameTpl:getGachaTpl()
  for i = 1, #historyList do
    local gachaType = gachaTpl:getGachaType(gachaTpl:getTplById(historyList[i].cardPoolId))
    if gachaType == cardPoolType then
      count = count + 1
    end
  end
  return count == 0 and 1 or math.ceil(count / 10)
end

function this:getCardPool(pool_id, isCreate)
  local cardPoolList = self:getCardPoolList()
  local item = cardPoolList[pool_id]
  if item == nil and isCreate then
    item = require(L_R.store .. "raffle.data.cardPool").new()
    cardPoolList[pool_id] = item
  end
  return item
end

function this:getIsPoolTotalLimit(cardPool)
  if not cardPool then
    return false
  end
  local gachaTpl = L_GameTpl:getGachaTpl()
  local tpl = gachaTpl:getTplById(cardPool.pool_id)
  local totalLimit = gachaTpl:getGachaLimit(tpl)
  if math.isEmpty(totalLimit) then
    return false
  end
  return totalLimit <= cardPool.total_count
end

function this:getIsPoolTodayLimit(cardPool, times)
  if not cardPool then
    return false
  end
  local gachaTpl = L_GameTpl:getGachaTpl()
  local tpl = gachaTpl:getTplById(cardPool.pool_id)
  local todayLimit = gachaTpl:getCanUsedNum(tpl)
  if math.isEmpty(todayLimit) then
    return false
  end
  local curTodayNum = self:getCardPoolTodayNum(cardPool)
  if times then
    return times > todayLimit - curTodayNum
  else
    return todayLimit <= curTodayNum
  end
end

function this:getCardPoolTodayNum(cardPool)
  local nowTime = L_TimeUtil:getServerTime()
  local lastTime = cardPool and cardPool.last_time or nowTime
  if not L_TimeUtil.isSameDay(lastTime, nowTime) then
    return 0
  else
    return cardPool and cardPool.day_count or 0
  end
end

function this:getHistoryListByParam(pageNumber, cardPoolType)
  local historyList = self:getHistoryList()
  local res = {}
  local index = 1
  local gachaTpl = L_GameTpl:getGachaTpl()
  for i = 1, #historyList do
    local gachaType = gachaTpl:getGachaType(gachaTpl:getTplById(historyList[i].cardPoolId))
    if gachaType == cardPoolType then
      if index > (pageNumber - 1) * 10 and index <= pageNumber * 10 then
        table.insert(res, historyList[i])
      end
      index = index + 1
    end
  end
  return res
end

function this:getFirstResult()
  self.data.curCardIndex = 1
  return self.data.resultList[self.data.curCardIndex]
end

function this:getResultNum()
  return #self.data.resultList
end

function this:getNextResult(isSkip)
  self.data.curCardIndex = self.data.curCardIndex + 1
  if not isSkip then
    return self.data.resultList[self.data.curCardIndex]
  end
  for i = self.data.curCardIndex, #self.data.resultList do
    local item = L_ItemManager:parseItem(self.data.resultList[i].mainPropType, self.data.resultList[i].mainPropId)
    local rarity = item.quality
    if rarity >= L_Const.quality.ssr or rarity == L_Const.quality.sr and self.data.resultList[i].new then
      self.data.curCardIndex = i
      return self.data.resultList[i]
    end
  end
  return nil
end

return this
