import 'package:recording_tool/features/recording/platform/recording_platform.dart';

/// 录制会话状态（相机预览、权限、录制进度等）。
class RecordingSessionState {
  const RecordingSessionState({
    this.status = const RecordingStatus(state: RecordingState.idle),
    this.isTouchLocked = true,
    this.isPreviewReady = false,
    this.isStartingRecording = false,
    this.isSwitchingLens = false,
    this.hasDndAccess = false,
    this.isBatteryOptimizedIgnored = true,
    this.notificationsGranted = true,
    this.isMicrophoneGranted = false,
    this.zoomRatio = 1.0,
    this.minZoomRatio = 1.0,
    this.maxZoomRatio = 3.0,
    this.lastStreamUrl,
    this.errorMessage,
    this.permissionWarning,
    this.streamFinished = false,
    this.streamFailed = false,
  });

  final RecordingStatus status;
  final bool isTouchLocked;
  final bool isPreviewReady;
  final bool isStartingRecording;
  final bool isSwitchingLens;
  final bool hasDndAccess;
  final bool isBatteryOptimizedIgnored;
  final bool notificationsGranted;
  final bool isMicrophoneGranted;
  final double zoomRatio;
  final double minZoomRatio;
  final double maxZoomRatio;
  final String? lastStreamUrl;
  final String? errorMessage;
  final String? permissionWarning;
  final bool streamFinished;
  final bool streamFailed;

  bool get isRecording => status.isRecording;

  String get elapsedLabel {
    final totalSeconds = status.elapsedMillis ~/ 1000;
    final hours = (totalSeconds ~/ 3600).toString().padLeft(2, '0');
    final minutes = ((totalSeconds % 3600) ~/ 60).toString().padLeft(2, '0');
    final seconds = (totalSeconds % 60).toString().padLeft(2, '0');
    return '$hours:$minutes:$seconds';
  }

  RecordingSessionState copyWith({
    RecordingStatus? status,
    bool? isTouchLocked,
    bool? isPreviewReady,
    bool? isStartingRecording,
    bool? isSwitchingLens,
    bool? hasDndAccess,
    bool? isBatteryOptimizedIgnored,
    bool? notificationsGranted,
    bool? isMicrophoneGranted,
    double? zoomRatio,
    double? minZoomRatio,
    double? maxZoomRatio,
    String? lastStreamUrl,
    String? errorMessage,
    String? permissionWarning,
    bool? streamFinished,
    bool? streamFailed,
    bool clearPermissionWarning = false,
    bool clearStreamResult = false,
  }) {
    return RecordingSessionState(
      status: status ?? this.status,
      isTouchLocked: isTouchLocked ?? this.isTouchLocked,
      isPreviewReady: isPreviewReady ?? this.isPreviewReady,
      isStartingRecording: isStartingRecording ?? this.isStartingRecording,
      isSwitchingLens: isSwitchingLens ?? this.isSwitchingLens,
      hasDndAccess: hasDndAccess ?? this.hasDndAccess,
      isBatteryOptimizedIgnored:
          isBatteryOptimizedIgnored ?? this.isBatteryOptimizedIgnored,
      notificationsGranted: notificationsGranted ?? this.notificationsGranted,
      isMicrophoneGranted: isMicrophoneGranted ?? this.isMicrophoneGranted,
      zoomRatio: zoomRatio ?? this.zoomRatio,
      minZoomRatio: minZoomRatio ?? this.minZoomRatio,
      maxZoomRatio: maxZoomRatio ?? this.maxZoomRatio,
      lastStreamUrl: lastStreamUrl ?? this.lastStreamUrl,
      errorMessage: errorMessage,
      permissionWarning: clearPermissionWarning
          ? null
          : (permissionWarning ?? this.permissionWarning),
      streamFinished: clearStreamResult
          ? false
          : (streamFinished ?? this.streamFinished),
      streamFailed: clearStreamResult
          ? false
          : (streamFailed ?? this.streamFailed),
    );
  }
}
