local this = {}

function this:req_queryPlayerBaseInfo(data, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoPlayerInfo, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self:syncPlayerBaseInfoList(rspData.player_infos)
    end
    if callback then
      callback(rspData, errorCode)
    end
  end)
end

function this:req_operationPlayer(data, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoOpFriend, data, function(rspData, errorCode)
    if callback then
      callback(errorCode)
    end
  end)
end

function this:req_modifyFriendMark(data, callback)
  L_Net:sendMessage(MsgGenCode.CSProtoChangeFriendRemark, data, function(rspData, errorCode)
    if errorCode == L_Const.errorCode.ErrCodeSucc then
      self.data.friendList[data.id].mark = rspData.remark
      self:call(self.event.refreshIdList)
    end
    if callback then
      callback(errorCode)
    end
  end)
end

function this:rsp_syncPlayerList(data)
  local listAll = data.info
  local friendList = listAll.friends
  local applicationList = listAll.apply
  local blackList = listAll.ban
  local recentTeamList = listAll.recent
  self.data.friendList = {}
  if friendList then
    for i, v in pairs(friendList) do
      local entity = self:getPlayerItem(self.data.friendList, v.id.pid, true)
      entity.playerUid = v.id.pid
      entity.mark = v.remark
      entity.time = v.time
    end
  end
  self.data.friendApplicationList = {}
  if applicationList then
    for i, v in pairs(applicationList) do
      local entity = self:getPlayerItem(self.data.friendApplicationList, v.id.pid, true)
      entity.playerUid = v.id.pid
      entity.time = v.time
    end
  end
  self.data.blackList = {}
  if blackList then
    for i, v in pairs(blackList) do
      local entity = self:getPlayerItem(self.data.blackList, v.id.pid, true)
      entity.playerUid = v.id.pid
      entity.time = v.time
    end
  end
  self.data.recentTeamList = {}
  if recentTeamList then
    for i, v in pairs(recentTeamList) do
      local entity = self:getPlayerItem(self.data.recentTeamList, v.id.pid, true)
      entity.playerUid = v.id.pid
      entity.time = v.time
    end
  end
  self:call(self.event.refreshIdList)
end

function this:rsp_friendIncChange(data)
  for _, v in ipairs(data.datalist) do
    if v.type & L_Const.FriendChangeType.Add2FriendList == L_Const.FriendChangeType.Add2FriendList then
      local entity = self:getPlayerItem(self.data.friendList, v.friendItem.id.pid, true)
      entity.playerUid = v.friendItem.id.pid
      entity.mark = v.friendItem.remark
      entity.time = v.friendItem.time
    end
    if v.type & L_Const.FriendChangeType.Add2BlackList == L_Const.FriendChangeType.Add2BlackList then
      local entity = self:getPlayerItem(self.data.blackList, v.friendItem.id.pid, true)
      entity.playerUid = v.friendItem.id.pid
      entity.time = v.friendItem.time
    end
    if v.type & L_Const.FriendChangeType.Add2ApplyList == L_Const.FriendChangeType.Add2ApplyList then
      local entity = self:getPlayerItem(self.data.friendApplicationList, v.friendItem.id.pid, true)
      entity.playerUid = v.friendItem.id.pid
      entity.time = v.friendItem.time
    end
    if v.type & L_Const.FriendChangeType.DelFromFriendList == L_Const.FriendChangeType.DelFromFriendList then
      self.data.friendList[v.friendItem.id.pid] = nil
    end
    if v.type & L_Const.FriendChangeType.DelFromBlackList == L_Const.FriendChangeType.DelFromBlackList then
      self.data.blackList[v.friendItem.id.pid] = nil
    end
    if v.type & L_Const.FriendChangeType.DelFromApplyList == L_Const.FriendChangeType.DelFromApplyList then
      self.data.friendApplicationList[v.friendItem.id.pid] = nil
    end
  end
  self:call(self.event.refreshIdList)
end

function this:syncPlayerBaseInfoList(data)
  if table.isEmpty(data) then
    return
  end
  for i, v in pairs(data) do
    local entity = self:getPlayerBaseInfo(v.id, true)
    table.merge(entity, v)
  end
end

return this
