import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

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

/// An implementation of [LiblinphoneFlutterPlatform] that uses method channels.
class MethodChannelLiblinphoneFlutter extends LiblinphoneFlutterPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('liblinphone_flutter');

  @override
  Future<bool> checkPermissions() async {
    return (await methodChannel.invokeMethod<bool>('checkPermissions'))!;
  }

  @override
  Future<bool> initialize() async {
    return (await methodChannel.invokeMethod<bool>('initialize'))!;
  }

  @override
  Future<bool> register(
    String username,
    String password,
    String serverIp,
    int serverPort,
  ) async {
    return (await methodChannel
        .invokeMethod<bool>('register', <String, dynamic>{
          'username': username,
          'password': password,
          'serverIp': serverIp,
          'serverPort': serverPort,
        }))!;
  }

  @override
  Future<bool> unregister() async {
    return (await methodChannel.invokeMethod<bool>('unregister'))!;
  }

  @override
  Future<bool> makeCall(String callTo, bool isVideoEnabled) async {
    return (await methodChannel.invokeMethod<bool>(
      'makeCall',
      <String, dynamic>{'callTo': callTo, 'isVideoEnabled': isVideoEnabled},
    ))!;
  }

  @override
  Future<bool> answerCall() async {
    return (await methodChannel.invokeMethod<bool>('answerCall'))!;
  }

  @override
  Future<bool> hangupCall() async {
    return (await methodChannel.invokeMethod<bool>('hangupCall'))!;
  }

  @override
  Future<bool> inCall() async {
    return (await methodChannel.invokeMethod<bool>('inCall'))!;
  }

  @override
  Future<CallType> callType() async {
    final callTypeOrdinal = await methodChannel.invokeMethod<int>('callType');
    return CallType.fromOrdinal(callTypeOrdinal!);
  }

  @override
  Future<bool> toggleVideo() async {
    return (await methodChannel.invokeMethod<bool>('toggleVideo'))!;
  }

  @override
  Future<bool> toggleMicrophone() async {
    return (await methodChannel.invokeMethod<bool>('toggleMicrophone'))!;
  }

  @override
  Future<bool> stop() async {
    return (await methodChannel.invokeMethod<bool>('stop'))!;
  }
}
