import 'pixel_format.dart';
import 'video_stabilization_mode.dart';
import 'auto_focus_system.dart';
import 'color_space.dart';

/// The range of the frame rate.
class FrameRateRange {
  /// The minimum frame rate.
  final int minFrameRate;

  // The maximum frame rate.
  final int maxFrameRate;

  const FrameRateRange(this.minFrameRate, this.maxFrameRate);
  factory FrameRateRange.fromMap(Map map) {
    return FrameRateRange(map['minFrameRate'], map['maxFrameRate']);
  }
}

/// The capture format of the camera device.
class CameraCaptureFormat {
  final double photoHeight;
  final double photoWidth;
  final double? videoHeight;
  final double? videoWidth;
  final bool isHighestPhotoQualitySupported;
  final int? maxISO;
  final int? minISO;
  final double? fieldOfView;
  final double? maxZoom;
  final List<ColorSpace> colorSpaces;
  final bool supportsVideoHDR;
  final bool supportsPhotoHDR;
  final List<FrameRateRange> frameRateRanges;
  final AutoFocusSystem autoFocusSystem;
  final List<VideoStabilizationMode> videoStabilizationModes;
  final PixelFormat pixelFormat;

  const CameraCaptureFormat(
      this.photoHeight,
      this.photoWidth,
      this.videoHeight,
      this.videoWidth,
      this.isHighestPhotoQualitySupported,
      this.maxISO,
      this.minISO,
      this.fieldOfView,
      this.maxZoom,
      this.colorSpaces,
      this.supportsVideoHDR,
      this.supportsPhotoHDR,
      this.frameRateRanges,
      this.autoFocusSystem,
      this.videoStabilizationModes,
      this.pixelFormat);

  factory CameraCaptureFormat.fromMap(Map map) {
    return CameraCaptureFormat(
        map['photoHeight'],
        map['photoWidth'],
        map['videoHeight'],
        map['videoWidth'],
        map['isHighestPhotoQualitySupported'] ?? false,
        map['maxISO'],
        map['minISO'],
        map['fieldOfView'],
        map['maxZoom'],
        (map['colorSpaces'] as List<Object?>)
            .where((e) => e != null)
            .map((e) => e as String)
            .map(colorSpaceFromString)
            .toList(),
        map['supportsVideoHDR'],
        map['supportsPhotoHDR'],
        (map['frameRateRanges'] as List<Object?>)
            .where((element) => element != null)
            .map((e) => FrameRateRange.fromMap(e! as Map))
            .toList(),
        autoFocusSystemFromString(map['autoFocusSystem']),
        (map['videoStabilizationModes'] as List<Object?>)
            .where((element) => element != null)
            .map((e) => e! as String)
            .map(videoStabilizationModeFromString)
            .toList(),
        pixelFormatFromString(map['pixelFormat']));
  }
}
