part of 'playerview_bloc.dart';

sealed class PlayerViewState {}

class PlayerViewDismiss extends PlayerViewState {}

@immutable
class PlayerViewController extends PlayerViewState {
  final double volume;
  final double speed;
  final int? start;
  final VideoQualityOption? quality;
  final List<Subtitle>? subtitles;
  final List<VideoQualityOption>? qualities;
  final Subtitle? currentSubtitle;

  PlayerViewController(
      {required this.volume,
      required this.speed,
      this.start,
      this.quality,
      this.subtitles,
      this.qualities,
      this.currentSubtitle});

  PlayerViewController copy(
      {double? volume,
      double? speed,
      int? start,
      VideoQualityOption? quality,
      List<Subtitle>? subtitles,
      List<VideoQualityOption>? qualities,
      Subtitle? currentSubtitle}) {
    return PlayerViewController(
      volume: volume ?? this.volume,
      speed: speed ?? this.speed,
      start: start ?? this.start,
      quality: quality ?? this.quality,
      subtitles: subtitles ?? this.subtitles,
      qualities: qualities ?? this.qualities,
      currentSubtitle: currentSubtitle ?? this.currentSubtitle
    );
  }

  factory PlayerViewController.createFromRepository(
      SettingsRepository repository) {
    return PlayerViewController(
        volume: repository.volume,
        speed: repository.speed,
        start: repository.start);
  }

  static final playbackSettings = [
    OptionDataContainer(
        key: const Key('PlaybackVolumeSettingID'),
        title: 'Громкость',
        options: const [
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume00ID'), '0.0'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume01ID'), '0.1'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume02ID'), '0.2'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume03ID'), '0.3'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume04ID'), '0.4'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume05ID'), '0.5'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume06ID'), '0.6'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume07ID'), '0.7'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume08ID'), '0.8'),
          OptionData.withValueEqualTitle(
              Key('PlaybackOptionVolume09ID'), '0.9'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionVolume10ID'), '1.0')
        ],
        selectedIndex: 4,
        onSelectedOption: (option) => VolumeChangedEvent(option)
    ),

      OptionDataContainer(
          key: const Key('PlaybackSpeedSettingID'),
          title: 'Скорость',
          options: const [
            OptionData(key: Key('PlaybackOptionSpeed025xID'), title: '0.25x', value: '0.25'),
            OptionData(key: Key('PlaybackOptionSpeed05xID'), title: '0.5x', value: '0.5'),
            OptionData(key: Key('PlaybackOptionSpeed075xID'), title: '0.75x', value: '0.75'),
            OptionData(key: Key('PlaybackOptionSpeedNormalID'), title: 'Обычная', value: '1.0'),
            OptionData(key: Key('PlaybackOptionSpeed125xID'), title: '1.25x', value: '1.25'),
            OptionData(key: Key('PlaybackOptionSpeed15xID'), title: '1.5x', value: '1.5'),
            OptionData(key: Key('PlaybackOptionSpeed175xID'), title: '1.75x', value: '1.75'),
            OptionData(key: Key('PlaybackOptionSpeed2xID'), title: '2x', value: '2.0')
          ],
          selectedIndex: 3,
          onSelectedOption: (option) => SpeedChangedEvent(option)
      )
  ];

  static final startPosition = NumericOptionData(
    key: const Key('PlaybackOptionStartPositionID'),
    title: 'Стартовая позиция',
    value: 0,
    onChange: (newValue) => StartPositionChangedEvent(newValue)
  );

  static OptionDataContainer? createSubs(List<Subtitle>? subs) {
    if (subs == null) { return null; }
    final options = subs.map((option) => OptionData(
        key: Key(option.id),
        title: option.title,
        value: option.id
    )).toList();

    return OptionDataContainer(
        key: const Key('PlaybackSubsSettingID'),
        title: 'Субтитры',
        options: options,
        selectedIndex: 0,
        onSelectedOption: (option) => SubsChangedEvent(option)
    );
  }

  static OptionDataContainer? createQualities(List<VideoQualityOption>? qualities) {
    if (qualities == null) { return null; }
    final options = qualities.map((option) => OptionData(
        key: Key(option.id),
        title: option.title,
        value: option.id
    )).toList();

    return OptionDataContainer(
        key: const Key('PlaybackQualitySettingID'),
        title: 'Качество',
        options: options,
        selectedIndex: 0,
        onSelectedOption: (option) => QualityChangedEvent(option)
    );
  }
}
