const socialManager = require("../../game/SocialManager");

module.exports = (io, socket) => {
  if (!socialManager.io) socialManager.init(io);

  socket.on("player:search", async ({ query }) => {
    try {
      const players = await socialManager.searchPlayers(query, socket.user.id);
      socket.emit("player:search_results", players);
    } catch (e) {
      console.error(e);
    }
  });

  socket.on("friend:add", async ({ friendId }) => {
    try {
      await socialManager.sendFriendRequest(socket.user, friendId);
    } catch (e) {
      socket.emit("error", { message: "FAILED_TO_SEND_REQUEST" });
    }
  });

  socket.on("friend:remove", async ({ friendId }) => {
    try {
      await socialManager.removeFriend(socket.user.id, friendId);
    } catch (e) {
      console.error(e);
      socket.emit("error", { message: "FAILED_TO_REMOVE_FRIEND" });
    }
  });
  socket.on("friend:accept", async ({ friendId, id }) => {
    try {
      await socialManager.acceptFriendRequest(socket.user.id, friendId, id);
    } catch (e) {
      console.error(e);
      socket.emit("error", { message: "FAILED_TO_ACCEPT_FRIEND" });
    }
  });

  socket.on("friend:get_list", async () => {
    try {
      const list = await socialManager.getFriendList(socket.user.id);
      socket.emit("friend:list", list);
    } catch (e) {
      console.error(e);
    }
  });
};
