import 'dart:convert';

import 'package:cashier_reserve/common/print/print.dart';
import 'package:flutter/services.dart';

import '../manager/event_manager.dart';
import 'call_log_model.dart';
import 'names.dart';

class MyEventChannel {
  static void startListener() {
    onGetCallLogResult();
  }

  static void onGetCallLogResult() {
    EventChannel channel = EventChannel(getChannelName(kCallLogCallback));
    channel.receiveBroadcastStream().listen((Object? o) {
      yjPrint("onGetCallLogResult");
      yjPrint(o);
      GetCallLogEvent event = GetCallLogEvent();
      if (o is String) {
        event.isSuccess = true;
        List<dynamic> list = json.decode(o);
        List<CallLogModel> callLogs = [];
        for (var item in list) {
          callLogs.add(CallLogModel.fromJson(item));
        }
        event.callLogs = callLogs.reversed.toList();
      } else {
        event.isSuccess = false;
      }
      EventManager.postEvent(event);
    }, onError: (Object error) {
      yjPrint("onGetCallLogResult error");
    });
  }
}