local util = {}

function util.parseRewardConfig(reward)
  local result = {}
  for _, v in pairs(reward) do
    local itemType = v[1]
    local itemId = v[2]
    local itemNum = v[3]
    if itemType == L_Const.resType.currency and itemId == L_Const.currencyType.freeExpBook then
      local bookMap = L_RewardManager:exp2BookItemNumMap(itemNum)
      for _, id in ipairs(L_Const.expBookIds) do
        if bookMap[id] and 0 < bookMap[id] then
          table.insert(result, {
            itemType = L_Const.resType.commonItem,
            itemId = id,
            itemNum = bookMap[id]
          })
        end
      end
    else
      table.insert(result, {
        itemType = itemType,
        itemId = itemId,
        itemNum = itemNum
      })
    end
  end
  return result
end

function util.parseRangeRewardConfig(reward)
  local result = {}
  for _, v in pairs(reward) do
    local itemType = v[1]
    local itemId = v[2]
    local itemNumTxt = ""
    local itemNum_1 = v[3]
    local itemNum_2 = v[4] or v[3]
    if itemNum_1 == itemNum_2 then
      itemNumTxt = tostring(itemNum_1)
    else
      itemNumTxt = string.format("%d~%d", itemNum_1, itemNum_2)
    end
    table.insert(result, {
      itemType = itemType,
      itemId = itemId,
      itemNumTxt = itemNumTxt
    })
  end
  return result
end

function util.parseRewardConfigItem(reward)
  local result = {}
  local itemType = reward[1]
  local itemId = reward[2]
  local itemNum = reward[3]
  result = {
    itemType = itemType,
    itemId = itemId,
    itemNum = itemNum
  }
  return result
end

function util.paresRewardProto(rewards)
  local result = {}
  for _, v in ipairs(rewards) do
    if not math.isEmpty(v.itemtype) then
      table.insert(result, {
        itemType = v.itemtype,
        itemId = v.itemid,
        itemNum = v.itemnum,
        guid = v.guid
      })
    end
  end
  return result
end

function util.getEmptyModuleDatas(num)
  local datas = {}
  for i = 1, num do
    table.insert(datas, {})
  end
  return datas
end

function util.checkItemEnough(reward, showTip)
  if table.isEmpty(reward) then
    return true
  end
  for _, item in ipairs(reward) do
    if L_ItemTplManager:getItemNum(item[1], item[2]) < item[3] then
      if showTip then
        L_FlyMsgManager:showNormalMsg(L_WordsTpl:getTplById("notice_common_lackItem"))
      end
      return false
    end
  end
  return true
end

function util.getItemStacks(itemType, id, num)
  local res = {}
  if itemType == L_Const.resType.commonItem then
    local _commonItemTpl = L_GameTpl:getCommonItemTpl()
    local tpl = _commonItemTpl:getTplById(id)
    local stackNum = tpl.stackNum
    if stackNum <= 0 then
      res = {num}
    else
      while 0 < num do
        table.insert(res, math.min(num, stackNum))
        num = num - stackNum
      end
    end
  elseif itemType == L_Const.resType.petEgg then
    for i = 1, num do
      table.insert(res, 1)
    end
  else
    res = {num}
  end
  return res
end

function util.setModuleItemSelect(modules, bindDataKey, value, bindKey)
  for _, module in ipairs(modules) do
    module.bind[bindKey] = value == module.bind[bindDataKey]
  end
end

return util
