import 'dart:async';

import 'package:cashier_reserve/common/channel/call_log_model.dart';
import 'package:event_bus/event_bus.dart';


class EventManager {
  static EventBus? _eventBus;

  static final Map<dynamic, List<StreamSubscription>> _eventMap = {};

  static EventBus? get eventBus => getEventBus();

  static EventBus? getEventBus() {
    _eventBus ??= EventBus();
    return _eventBus;
  }

  static void postEvent(MyEvent event) {
    getEventBus()!.fire(event);
  }

  static void addListener<T>(dynamic widget, void Function(T event) onData) {
    StreamSubscription event = EventManager.eventBus!.on<T>().listen((T e) {
      onData(e);
    });
    List<StreamSubscription>? list = _eventMap[widget];
    list ??= [];
    list.add(event);
    _eventMap[widget] = list;
  }

  static void cancelListener(dynamic widget) {
    List<StreamSubscription>? list = _eventMap[widget];
    if (list == null) {
      return;
    }
    for (var event in list) {
      event.cancel();
    }
  }
}

class MyEvent {
  String name = '';
}

class GetCallLogEvent extends MyEvent {
  List<CallLogModel> callLogs;
  bool isLoadMore = false;
  bool isSuccess = false;

  GetCallLogEvent({this.callLogs = const [], this.isLoadMore = false, this.isSuccess = false});
}