import 'package:get/get.dart';

import '../../../../core/network/socket/events.dart';
import '../../../../core/network/socket/socket.dart';
import '../model/message.dart';

class ChatListener {
  SocketIO io = Get.find<SocketIO>();

  void receiveMessage(Function(MessageModel) handler) {
    io.listen(
        Events.messageIncome, (data) => handler(MessageModel.fromJson(data)));
  }

  void messagesReaded(Function(int messageId, int userId) handler) {
    io.listen(Events.messageRead,
        (data) => handler(data['message']['id'], data['user']['id']));
  }

  // void updatedRoom(Function(int roomId, String roomStatus) handler) {
  //   io.listen(Events.roomUpdated,
  //       (data) => handler(data['roomId'], data['roomstatus']));
  // }

  void unblockedRoom(Function(int roomId) handler) {
    io.listen(Events.roomUnblocked, (data) => handler(data["room"]["id"]));
  }

  void blockedRoom(Function(int roomId) handler) {
    io.listen(Events.roomBlocked, (data) => handler(data["room"]["id"]));
  }

  void blockingRoom(Function(int roomId) handler) {
    io.listen(Events.roomBlocking, (data) => handler(data["room"]["id"]));
  }

  void termminatedSession(Function(dynamic) handler) {
    io.listen(Events.sessionTerminated, handler);
  }
}
