local Localization = CS.GameEntry.Localization
local RewardUtil = {}
local GetPic = function(rewardType, itemId)
  if rewardType ~= nil then
    if rewardType ~= RewardType.GOODS or itemId == nil then
    else
      local goods = DataCenter.ItemTemplateManager:GetItemTemplate(itemId)
      if goods ~= nil then
        return string.format(LoadPath.ItemPath, goods.icon)
      else
        local resourceType = tonumber(itemId)
        if resourceType < 100 then
          return DataCenter.ResourceManager:GetResourceIconByType(resourceType)
        end
      end
    end
    return DataCenter.RewardManager:GetPicByType(rewardType, itemId)
  end
  return ""
end
local GetName = function(rewardType, itemId)
  local name = ""
  if rewardType == nil then
  elseif rewardType == RewardType.GOODS then
    if itemId == nil then
    else
      local goods = DataCenter.ItemTemplateManager:GetItemTemplate(itemId)
      if goods ~= nil then
        name = DataCenter.ItemTemplateManager:GetName(itemId)
      else
        local resourceType = tonumber(itemId)
        name = DataCenter.ResourceManager:GetResourceNameByType(resourceType)
      end
    end
  elseif rewardType == RewardType.GOLD then
    name = Localization:GetString("100183")
  elseif rewardType == RewardType.OIL or rewardType == RewardType.METAL or rewardType == RewardType.FORMATION_STAMINA or rewardType == RewardType.WATER or rewardType == RewardType.PVE_POINT or rewardType == RewardType.DETECT_EVENT or rewardType == RewardType.FOOD or rewardType == RewardType.ELECTRICITY or rewardType == RewardType.WOOD or rewardType == RewardType.FLINT or rewardType == RewardType.OBSIDIAN then
    name = Localization:GetString(ResourceTypeTxt[rewardType])
  elseif rewardType == RewardType.ARM then
    local army = DataCenter.ArmyTemplateManager:GetArmyTemplate(itemId)
    if army ~= nil then
      name = Localization:GetString(army.name)
    end
  elseif rewardType == RewardType.EQUIP then
    local xmlData = LocalController:instance():getLine("equip_info_new_equip", itemId)
    if xmlData ~= nil then
      name = Localization:GetString(xmlData:GetString("name"))
    end
  elseif rewardType == RewardType.HERO then
  elseif rewardType == RewardType.HONOR or rewardType == RewardType.ALLIANCE_POINT then
    name = DataCenter.RewardManager:GetNameByType(rewardType, itemId)
  elseif rewardType == RewardType.MATERIAL then
    local goods = DataCenter.ItemTemplateManager:GetItemTemplate(itemId)
    if goods ~= nil then
      name = DataCenter.ItemTemplateManager:GetName(itemId)
    end
  elseif rewardType == RewardType.RESOURCE_ITEM then
    local template = DataCenter.ResourceItemDataManager:GetResourceItemTemplate(itemId)
    if template ~= nil then
      name = DataCenter.RewardManager:GetNameByType(rewardType, itemId)
    end
  elseif rewardType == RewardType.EXP then
    name = DataCenter.RewardManager:GetNameByType(rewardType)
  elseif rewardType == RewardType.WORKER or rewardType == RewardType.VISITOR then
  end
  return name
end
local IsHaveWorldReward = function()
  local list = DataCenter.CollectRewardDataManager:GetRewardListBySort()
  if not table.IsNullOrEmpty(list) and UITimeManager:GetInstance():GetServerTime() <= list[1].expireTime then
    return true
  end
  if not BattleFieldUtil.InBattleField() then
    local cardBoxList = DataCenter.SeasonDataManager.cardBoxList
    if cardBoxList ~= nil and table.count(cardBoxList) > 0 then
      return true
    end
  end
  if not BattleFieldUtil.InBattleField() then
    local isExistFlowerArrived = FlowerTrainUtils.IsExistAnySelfFlowerTrainArrived()
    if isExistFlowerArrived then
      return true
    end
  end
  return false
end
local GetRewardItem = function(rewardId)
  local res = {}
  local rewardConfig = LocalController:instance():getLine(TableName.RewardConfig, tonumber(rewardId))
  if rewardConfig ~= nil then
    local itemValues = rewardConfig:getValue("item") or ""
    local numValues = rewardConfig:getValue("num") or ""
    if not string.IsNullOrEmpty(itemValues) and not string.IsNullOrEmpty(numValues) then
      local ids = string.split(itemValues, "|")
      local nums = string.split(numValues, "|")
      if ids ~= nil and 0 < #ids then
        for i, id in pairs(ids) do
          local oneData = {}
          oneData.itemId = id
          oneData.count = tonumber(nums[i]) or 0
          oneData.rewardType = RewardType.GOODS
          table.insert(res, oneData)
        end
      end
    end
  end
  return res
end
local GetRewardItemAndResource = function(rewardId)
  local res = {}
  local rewardConfig = LocalController:instance():getLine(TableName.RewardConfig, tonumber(rewardId))
  if rewardConfig ~= nil then
    local itemValues = rewardConfig:getValue("item") or ""
    local numValues = rewardConfig:getValue("num") or ""
    if not string.IsNullOrEmpty(itemValues) and not string.IsNullOrEmpty(numValues) then
      local ids = string.split(itemValues, "|")
      local nums = string.split(numValues, "|")
      if ids ~= nil and 0 < #ids then
        for i, id in pairs(ids) do
          local oneData = {}
          oneData.itemId = id
          oneData.count = tonumber(nums[i]) or 0
          oneData.rewardType = RewardType.GOODS
          table.insert(res, oneData)
        end
      end
    end
    local resourceRandomType = rewardConfig:getValue("resource_randomtype") or ""
    local resourceRate = rewardConfig:getValue("resource_rate") or ""
    if not string.IsNullOrEmpty(resourceRandomType) and not string.IsNullOrEmpty(resourceRate) then
      local ids = string.split(resourceRandomType, "|")
      local nums = string.split(resourceRate, "|")
      if ids ~= nil and 0 < #ids then
        for i, id in pairs(ids) do
          local oneData = {}
          oneData.itemId = id
          if nums[i] then
            local numss = string.split(nums[i], ";")
            oneData.count = tonumber(numss[1]) or 0
          else
            oneData.count = 0
          end
          oneData.rewardType = RewardType.RESOURCE
          table.insert(res, oneData)
        end
      end
    end
  end
  return res
end
RewardUtil.GetPic = GetPic
RewardUtil.GetName = GetName
RewardUtil.IsHaveWorldReward = IsHaveWorldReward
RewardUtil.GetRewardItem = GetRewardItem
RewardUtil.GetRewardItemAndResource = GetRewardItemAndResource
return ConstClass("RewardUtil", RewardUtil)
