local this = {}

function this:req_collectAchievementReward(guid, callback)
  local data = {u32 = guid}
  L_Net:sendMessage(MsgGenCode.CSProtoAchievementReward, data, function(rspData, errorCode)
    local rewards = rspData.rewards
    if callback and errorCode == L_Const.errorCode.ErrCodeSucc then
      callback(rewards, rspData.src)
    end
  end)
end

function this:req_collectAchievementGroupReward(guid, callback)
  local data = {u32 = guid}
  L_Net:sendMessage(MsgGenCode.CSProtoAchievementGroupReward, data, function(rspData, errorCode)
    local rewards = rspData.rewards
    if callback and errorCode == L_Const.errorCode.ErrCodeSucc then
      callback(rewards, rspData.src)
    end
  end)
end

function this:req_clientBehaviourRecord(key, args, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoClientBehaviourRecord, {key = key, args = args}, function(rspData, errorCode)
    if callback and errorCode == L_Const.errorCode.ErrCodeSucc then
      callback()
    end
  end)
end

function this:rsp_syncAchievementInfo(data)
  local groups = data.groups
  local achievements = data.achievements
  if groups then
    for i = 1, #groups do
      self.data.achievementGroupRewardList[groups[i].id] = groups[i].is_reward
    end
  end
  if achievements then
    for i = 1, #achievements do
      self:rsp_syncAchievementItem(achievements[i])
    end
  end
  self:call(self.event.refreshAchievementList)
end

function this:rsp_syncAchievementTip(data)
  L_FlyMsgManager:showAchievementPopup(data)
end

function this:rsp_syncAchievementItem(data)
  local entity = self:getAchievementItem(data.id, true)
  local achievementTpl = L_GameTpl:getAchievementTpl()
  local tpl = achievementTpl:getTplById(data.achievement_id)
  entity.guid = data.id
  entity.achievementId = data.achievement_id
  entity.isReward = data.is_reward
  entity.count = data.count
  entity.finishTime = data.finish_time
  entity.groupId = achievementTpl:getGroupId(tpl)
  entity.version = achievementTpl:getAchievementVersion(data.achievement_id)
end

return this
