local this = {}
local _itemRewardFormTpl = L_GameTpl:getItemRewardFormTpl()
local _reasonItemnumChangeTpl = L_GameTpl:getReasonItemnumChangeTpl()
local _commonItemTpl = L_GameTpl:getCommonItemTpl()

function this:ShowReward(rewards, isOne)
  if rewards == nil or rewards.rewards == nil then
    return
  end
  local count = 0
  if rewards.rewards.Count ~= nil then
    count = rewards.rewards.Count
  else
    count = #rewards.rewards
  end
  if count == 0 then
    return
  end
  local ignoreQueueCheck = rewards.ignoreQueueCheck
  local title = rewards.title or nil
  local forceShow = rewards.forceShow and rewards.forceShow or false
  local cacheList = {
    listfull = {},
    listhalf = {},
    listfly = {},
    listTitle = {},
    petEggList = {},
    petList = {},
    heroList = {}
  }
  local list = cacheList.listfull
  local listhalf = cacheList.listhalf
  local listfly = cacheList.listfly
  local listTitle = cacheList.listTitle
  local petEggList = cacheList.petEggList
  local petList = cacheList.petList
  local heroList = cacheList.heroList
  self:filterSpecialSrc(rewards, cacheList)
  if 0 < #petEggList then
    for i, v in ipairs(petEggList) do
      self:addQueuePage("pageGetPetEgg", {configId = v})
    end
  end
  if 0 < #petList then
    for i, v in ipairs(petList) do
      local entity = L_PetStore:getPetItem(v)
      if entity then
        self:addQueuePage("pageGetPet", {pet = entity, forceShow = forceShow})
      end
    end
  end
  if 0 < #list then
    self:addQueuePage("pageReward", {
      itemList = list,
      ignoreQueueCheck = ignoreQueueCheck,
      title = title
    })
  end
  if 0 < #listTitle then
    self:addQueuePage("pageRewardTitle", {titleList = listTitle})
  end
  if 0 < #heroList then
    for i, v in ipairs(heroList) do
      self:addQueuePage("pageGetHero", {configId = v})
    end
  end
  if 0 < #listhalf then
    self:addQueuePage("pageFreeReward", {
      itemList = listhalf,
      ignoreQueueCheck = ignoreQueueCheck,
      title = title
    })
  end
  if 0 < #listfly then
    if ignoreQueueCheck then
      L_FlyMsgManager:showItemServerRewards(listfly, ignoreQueueCheck)
    else
      self:addQueuePage("pageFlyReward", {itemList = listfly}, isOne)
    end
  end
end

function this:filterSpecialSrc(rewards, cacheList)
  local listfull = cacheList.listfull
  local listhalf = cacheList.listhalf
  local listfly = cacheList.listfly
  local listTitle = cacheList.listTitle
  local petEggList = cacheList.petEggList
  local petList = cacheList.petList
  local heroList = cacheList.heroList
  local srcId = rewards.src
  local isFromTaskFinish = false
  local tpl = _reasonItemnumChangeTpl:getTplById(srcId)
  if tpl then
    isFromTaskFinish = _reasonItemnumChangeTpl:getId(tpl) == 88
  end
  for i, v in pairs(rewards.rewards) do
    local item = v
    if not math.isEmpty(v.itemtype) then
      local parseData = L_ItemManager:parseItem(v.itemtype, v.itemid)
      local txt_count = ""
      if v.itemtype == L_Const.resType.playerTitle then
        L_ReddotManager:markNew(L_ReddotManager.DotDef.Title, v.itemid)
        table.insert(listTitle, {parseData = parseData})
      elseif v.itemtype == L_Const.resType.petEgg then
        L_ReddotManager:markNew(L_ReddotManager.DotDef.Title, v.itemid)
        table.insert(petEggList, v.itemid)
      elseif v.itemtype == L_Const.resType.pet then
        L_ReddotManager:markNew(L_ReddotManager.DotDef.PetNew, v.guid)
        table.insert(petList, v.guid)
      elseif v.itemtype == L_Const.resType.hero and isFromTaskFinish then
        table.insert(heroList, v.itemid)
      else
        local srcForm = this:getShowTypeByTpl(item, srcId)
        local showType = srcForm ~= nil and srcForm or L_Const.rewardShowType.full
        if v.itemnum and v.itemnum > 0 then
          txt_count = tostring(v.itemnum)
          if showType == L_Const.rewardShowType.fly then
            table.insert(listfly, {
              itemtype = v.itemtype,
              itemid = v.itemid,
              txt_count = txt_count,
              guid = v.guid,
              itemnum = v.itemnum
            })
          elseif showType == L_Const.rewardShowType.half then
            table.insert(listhalf, {
              itemType = v.itemtype,
              itemId = v.itemid,
              txt_count = txt_count,
              guid = v.guid
            })
          elseif showType == L_Const.rewardShowType.full then
            table.insert(listfull, {
              itemType = v.itemtype,
              itemId = v.itemid,
              txt_count = txt_count,
              guid = v.guid
            })
          end
        end
      end
    end
  end
end

function this:getShowTypeByTpl(item, srcId)
  local subType
  if item.itemtype == L_Const.resType.commonItem then
    local itemTpl = _commonItemTpl:getTplById(item.itemid)
    if itemTpl then
      subType = itemTpl.type
    end
  elseif item.itemtype == L_Const.resType.currency then
    subType = item.itemid
  end
  local srcForm = _reasonItemnumChangeTpl:getFormType(srcId, item.itemtype, subType)
  if not srcForm then
    local itemForm = _itemRewardFormTpl:getFormType(item.itemtype, subType)
    srcForm = itemForm
  end
  return srcForm
end

function this:showFullPageReward(rewardsList, closeCallback, title)
  local list = {}
  for i, v in pairs(rewardsList) do
    local num = v.itemnum or v.itemNum
    if num and 0 < num then
      local txt_count = tostring(num)
      table.insert(list, {
        itemType = v.itemtype or v.itemType,
        itemId = v.itemid or v.itemId,
        txt_count = txt_count,
        guid = v.guid
      })
    end
  end
  local param = {
    itemList = list,
    title = title,
    callback = closeCallback
  }
  L_UI:open("pageReward", param)
end

return this
