import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'duration_range.dart';

/// Event emitted from the platform implementation.
@immutable
class VideoEvent {
  /// Creates an instance of [VideoEvent].
  ///
  /// The [eventType] argument is required.
  ///
  /// Depending on the [eventType], the [duration],
  /// [rotationCorrection], and [buffered] arguments can be null.
  // TODO(stuartmorgan): Temporarily suppress warnings about not using const
  // in all of the other video player packages, fix this, and then update
  // the other packages to use const.
  // ignore: prefer_const_constructors_in_immutables
  VideoEvent({
    required this.eventType,
    this.duration,
    this.subtitles,
    this.qualities,
    this.size,
    this.rotationCorrection,
    this.buffered,
    this.isPlaying,
  });

  /// The type of the event.
  final VideoEventType eventType;

  /// Duration of the video.
  ///
  /// Only used if [eventType] is [VideoEventType.initialized].
  final Duration? duration;

  final Map<String, String>? subtitles;
  
  final List<Map<String, dynamic>>? qualities;

  /// Size of the video.
  ///
  /// Only used if [eventType] is [VideoEventType.initialized].
  final Size? size;

  /// Degrees to rotate the video (clockwise) so it is displayed correctly.
  ///
  /// Only used if [eventType] is [VideoEventType.initialized].
  final int? rotationCorrection;

  /// Buffered parts of the video.
  ///
  /// Only used if [eventType] is [VideoEventType.bufferingUpdate].
  final List<DurationRange>? buffered;

  /// Whether the video is currently playing.
  ///
  /// Only used if [eventType] is [VideoEventType.isPlayingStateUpdate].
  final bool? isPlaying;

  @override
  bool operator ==(Object other) {
    return identical(this, other) ||
        other is VideoEvent &&
            runtimeType == other.runtimeType &&
            eventType == other.eventType &&
            duration == other.duration &&
            subtitles == other.subtitles &&
            listEquals(qualities, other.qualities) &&
            size == other.size &&
            rotationCorrection == other.rotationCorrection &&
            listEquals(buffered, other.buffered) &&
            isPlaying == other.isPlaying;
  }

  @override
  int get hashCode => Object.hash(
    eventType,
    duration,
    subtitles,
    qualities,
    size,
    rotationCorrection,
    buffered,
    isPlaying,
  );
}

/// Type of the event.
///
/// Emitted by the platform implementation when the video is initialized or
/// completed or to communicate buffering events or play state changed.
enum VideoEventType {
  /// The video has been initialized.
  initialized,

  ready,

  /// The playback has ended.
  completed,

  /// Updated information on the buffering state.
  bufferingUpdate,

  /// The video started to buffer.
  bufferingStart,

  /// The video stopped to buffer.
  bufferingEnd,

  /// The playback state of the video has changed.
  ///
  /// This event is fired when the video starts or pauses due to user actions or
  /// phone calls, or other app media such as music players.
  isPlayingStateUpdate,

  /// Обновление доступных качеств
  didFetchQualities,

  /// An unknown event has been received.
  unknown,
}
