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

import 'camera_device.dart';
import 'vital_sign_camera_platform_interface.dart';

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

  @override
  Future<List<CameraDevice>> availableCameraDevices() async {
    final devices =
        await methodChannel.invokeListMethod<Map>('availableCameraDevices');
    if (devices != null) {
      return devices.map((e) => CameraDevice.fromMap(e)).toList();
    } else {
      return [];
    }
  }

  @override
  Future<CameraPermissionStatus> requestCameraPermission() async {
    final status =
        await methodChannel.invokeMethod<String>('requestCameraPermission');
    return cameraPermissionFromString(status);
  }

  @override
  Future<CameraPermissionStatus> getCameraPermissionStatus() async {
    final status =
        await methodChannel.invokeMethod<String>('getCameraPermissionStatus');
    return cameraPermissionFromString(status);
  }
}
