import 'package:flutter/services.dart' show EventChannel;

import 'liblinphone_flutter_platform_interface.dart';
import 'models/call_type.dart';
import 'models/registration_state.dart';
import 'models/call_state.dart';

class LiblinphoneFlutter {
  final _registrationEventsStream = EventChannel(
    'liblinphone_flutter.nuark.xyz/registration_events',
  );

  final _callEventsStream = EventChannel(
    'liblinphone_flutter.nuark.xyz/call_events',
  );

  Stream<RegistrationState> get registrationEvents =>
      _registrationEventsStream.receiveBroadcastStream().map((dynamic event) {
        return RegistrationState.fromOrdinal(event);
      });

  Stream<CallState> get callEvents =>
      _callEventsStream.receiveBroadcastStream().map((dynamic event) {
        print("call event: $event");
        return CallState.fromOrdinal(event);
      });

  Future<bool> checkPermissions() async =>
      LiblinphoneFlutterPlatform.instance.checkPermissions();

  Future<bool> initialize() async =>
      LiblinphoneFlutterPlatform.instance.initialize();

  Future<bool> register(
    String username,
    String password,
    String serverIp,
    int serverPort,
  ) async => LiblinphoneFlutterPlatform.instance.register(
    username,
    password,
    serverIp,
    serverPort,
  );

  Future<bool> unregister() async =>
      LiblinphoneFlutterPlatform.instance.unregister();

  Future<bool> makeCall(String callTo, bool isVideoEnabled) async =>
      LiblinphoneFlutterPlatform.instance.makeCall(callTo, isVideoEnabled);

  Future<bool> answerCall() async =>
      LiblinphoneFlutterPlatform.instance.answerCall();

  Future<bool> hangupCall() async =>
      LiblinphoneFlutterPlatform.instance.hangupCall();

  Future<bool> inCall() async => LiblinphoneFlutterPlatform.instance.inCall();

  Future<CallType> callType() async =>
      LiblinphoneFlutterPlatform.instance.callType();

  Future<bool> toggleVideo() async =>
      LiblinphoneFlutterPlatform.instance.toggleVideo();

  Future<bool> toggleMicrophone() async =>
      LiblinphoneFlutterPlatform.instance.toggleMicrophone();

  Future<bool> stop() async => LiblinphoneFlutterPlatform.instance.stop();
}
