part of 'settings_bloc.dart';

enum FullscreenSettings { landscape, flexible, off }

extension FullscreenSettingsExtension on FullscreenSettings {
  String get title {
    switch (this) {
      case FullscreenSettings.landscape:
        return 'Альбомный';
      case FullscreenSettings.flexible:
        return 'Свободный';
      case FullscreenSettings.off:
        return 'Выкл.';
      default:
        return 'Неизвестно';
    }
  }
}

enum SkinColor { white }
enum VideoQualityNaming { common, rus, eng, resolution }
enum LogType { off, info, debug }

enum VideoQuality { auto, p2160, p1440, p1080, p720, p480, p360, p240, p144  }
extension VideoQualityIdentifier on VideoQuality {
  String get identifier {
    switch (this) {
      case VideoQuality.auto:
        return "auto";
      case VideoQuality.p144:
        return "p144";
      case VideoQuality.p240:
        return "p240";
      case VideoQuality.p360:
        return "p360";
      case VideoQuality.p480:
        return "p480";
      case VideoQuality.p720:
        return "p720";
      case VideoQuality.p1080:
        return "p1080";
      case VideoQuality.p1440:
        return "p1440";
      case VideoQuality.p2160:
        return "p2160";
    }
  }
}

sealed class SettingsState {}

class SettingsDismiss extends SettingsState {}

@immutable
class SettingsInitialState extends SettingsState {
  final String playerVersion;

  final bool isSkinByDefault;

  final FullscreenSettings fullscreenSettings;
  final bool isPipAvailable;
  final bool isSettingsAvailable;
  final SkinColor skinColor;

  final bool isAutostart;
  final double volume;
  final double brightness;
  final double speed;
  final VideoQualityNaming qualityNaming;
  final VideoQuality quality;
  final bool isSubtitlesAvailable;
  final int start;

  final bool isLoop;

  final int playlist;
  final int track;
  final int chunk;

  final LogType log;

  SettingsInitialState({
    required this.playerVersion,
    required this.isSkinByDefault,
    required this.fullscreenSettings,
    required this.isPipAvailable,
    required this.isSettingsAvailable,
    required this.skinColor,
    required this.isAutostart,
    required this.brightness,
    required this.volume,
    required this.speed,
    required this.qualityNaming,
    required this.quality,
    required this.isSubtitlesAvailable,
    required this.start,
    required this.isLoop,
    required this.playlist,
    required this.track,
    required this.chunk,
    required this.log
  });

  SettingsInitialState copy({
    String? playerVersion,

    bool? isSkinByDefault,

    FullscreenSettings? fullscreenSettings,
    bool? isPipAvailable,
    bool? isSettingsAvailable,
    SkinColor? skinColor,

    bool? isAutostart,
    double? volume,
    double? brightness,
    double? speed,
    VideoQualityNaming? qualityNaming,
    VideoQuality? quality,
    bool? isSubtitlesAvailable,
    int? start,

    bool? isLoop,

    int? playlist,
    int? track,
    int? chunk,

    LogType? log
  }) {
    return SettingsInitialState(
      playerVersion: playerVersion ?? this.playerVersion,
      isSkinByDefault: isSkinByDefault ?? this.isSkinByDefault,
      fullscreenSettings: fullscreenSettings ?? this.fullscreenSettings,
      isPipAvailable: isPipAvailable ?? this.isPipAvailable,
      isSettingsAvailable: isSettingsAvailable ?? this.isSettingsAvailable,
      skinColor: skinColor ?? this.skinColor,
      isAutostart: isAutostart ?? this.isAutostart,
      volume: volume ?? this.volume,
      brightness: brightness ?? this.brightness,
      speed: speed ?? this.speed,
      qualityNaming: qualityNaming ?? this.qualityNaming,
      quality: quality ?? this.quality,
      isSubtitlesAvailable: isSubtitlesAvailable ?? this.isSubtitlesAvailable,
      start: start ?? this.start,
      isLoop: isLoop ?? this.isLoop,
      playlist: playlist ?? this.playlist,
      track: track ?? this.track,
      chunk: chunk ?? this.chunk,
      log: log ?? this.log
    );
  }

  factory SettingsInitialState.createFromRepository(SettingsRepository repository) {
    return SettingsInitialState(
        playerVersion: "",
        isSkinByDefault: repository.isSkinByDefault,
        fullscreenSettings: SettingsMapper.fullscreenSetting(repository.fullscreenSettings),
        isPipAvailable: repository.isPipAvailable,
        isSettingsAvailable: repository.isSettingsAvailable,
        skinColor: SettingsMapper.skinColor(repository.skinColor),
        isAutostart: repository.isAutostart,
        volume: repository.volume,
        brightness: repository.brightness,
        speed: repository.speed,
        qualityNaming: SettingsMapper.qualityNaming(repository.qualityNaming),
        quality: SettingsMapper.quality(repository.quality),
        isSubtitlesAvailable: repository.isSubtitlesAvailable,
        start: repository.start,
        isLoop: repository.isLoop,
        playlist: repository.playlist,
        track: repository.track,
        chunk: repository.chunk,
        log: SettingsMapper.logType(repository.log)
    );
  }

  static final skinSettings = [
    BoolOptionData(
        key: const Key('SkinOptionDefaultID'),
        title: 'По умолчанию',
        isSelected: true,
        onChange: (_) => SkinChangedEvent(true)
    ),
    BoolOptionData(
        key: const Key('SkinOptionByUserID'),
        title: 'Свой',
        isSelected: false,
        onChange: (_) => SkinChangedEvent(false)
    )
  ];

  static final standardSkinSettings = [
    OptionDataContainer(
        key: const Key('FullscreenID'),
        title: 'Полноэкранный режим',
        options: const [
          OptionData.withValueEqualTitle(Key('SkinStandardFullscreenAlbumID'), 'Альбомный'),
          OptionData.withValueEqualTitle(Key('SkinStandardFullscreenFlexID'),'Свободный'),
          OptionData.withValueEqualTitle(Key('SkinStandardFullscreenOffID'), "Выкл.")
        ],
        onSelectedOption: (option) => FullscreenChangedEvent(option)
    ),
    BoolOptionData(
        key: const Key('SkinStandardPipID'),
        title: 'Картинка в картинке',
        isSelected: false,
        onChange: (isOn) => PipChangedEvent(isOn)
    ),
    BoolOptionData(
        key: const Key('SkinStandardSettingsID'),
        title: 'Настройки',
        isSelected: true,
        onChange: (isOn) => SettingsChangedEvent(isOn)
    ),
    OptionDataContainer(
        key: const Key('ColorID'),
        title: 'Цвет',
        options: const [
          OptionData(key: Key('SkinStandardColor1ID'), title: '#0D70D1'),
          OptionData(key: Key('SkinStandardColor2ID'), title: '#000000'),
          OptionData(key: Key('SkinStandardColor3ID'), title: '#FFFFFF')
        ]
    )
  ];

  static final playbackOptions = [
    BoolOptionData(
        key: const Key('PlaybackOptionAutostartID'),
        title: 'Автостарт',
        isSelected: false,
        onChange: (isOn) => AutostartChangedEvent(isOn)
    ),
    OptionDataContainer(
        key: const Key('PlaybackOptionVolumeID'),
        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')
        ],
        onSelectedOption: (option) => VolumeChangedEvent(option)
    ),
    OptionDataContainer(
        key: const Key('PlaybackOptionBrightnessID'),
        title: 'Яркость',
        options: const [
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness00ID'), '0.0'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness01ID'), '0.1'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness02ID'), '0.2'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness03ID'), '0.3'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness04ID'), '0.4'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness05ID'), '0.5'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness06ID'), '0.6'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness07ID'), '0.7'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness08ID'), '0.8'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness09ID'), '0.9'),
          OptionData.withValueEqualTitle(Key('PlaybackOptionBrightness10ID'), '1.0')
        ],
        onSelectedOption: (option) => BrightnessChangedEvent(option)
    ),
    OptionDataContainer(
        key: const Key('PlaybackOptionSpeedID'),
        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')
        ],
        onSelectedOption: (option) => SpeedChangedEvent(option)
    ),
    OptionDataContainer(
        key: const Key('PlaybackOptionQualityID'),
        title: 'Качество',
        options: const [
          OptionData(key: Key('PlaybackOptionQualityAutoID'), title: 'Авто', value: "auto"),
          OptionData(key: Key('PlaybackOptionQuality4kID'), title: '4K', value: "p2160"),
          OptionData(key: Key('PlaybackOptionQuality1440pID'), title: '1440p Ultra HD', value: "p1440"),
          OptionData(key: Key('PlaybackOptionQuality1080pID'), title: '1080p FHD', value: "p1080"),
          OptionData(key: Key('PlaybackOptionQuality720pID'), title: '720p HD', value: "p720"),
          OptionData(key: Key('PlaybackOptionQuality480pID'), title: '480p', value: "p480"),
          OptionData(key: Key('PlaybackOptionQuality360pID'), title: '360p', value: "p360"),
          OptionData(key: Key('PlaybackOptionQuality240pID'), title: '240p', value: "p240"),
          OptionData(key: Key('PlaybackOptionQuality144pID'), title: '144p', value: "p144"),
        ],
        onSelectedOption: (option) => QualityChangedEvent(option)
    ),
    OptionDataContainer(
        key: const Key('PlaybackOptionQualityNamingID'),
        title: 'Название качества',
        options: const [
          OptionData(key: Key('PlaybackOptionQualityNameCommonID'), title: 'Common', value: "common"),
          OptionData(key: Key('PlaybackOptionQualityNameRusID'), title: 'Russian', value: "rus"),
          OptionData(key: Key('PlaybackOptionQualityNameEngID'), title: 'English', value: "eng"),
          OptionData(key: Key('PlaybackOptionQualityNameResolutionID'), title: 'Resolution', value: "resolution"),
        ],
        onSelectedOption: (option) => QualityNamingChangedEvent(option)
    ),
    BoolOptionData(
        key: const Key('PlaybackOptionSubtitlesID'),
        title: 'Субтитры',
        isSelected: true,
        onChange: (isOn) => SubsChangedEvent(isOn)
    ),
    NumericOptionData(
        key: const Key('PlaybackOptionStartPositionID'),
        title: 'Стартовая позиция',
        value: 0,
        onChange: (value) => StartPositionChangedEvent(value)
    )
  ];

   static final extraOption = BoolOptionData(
       key: const Key('ExtraOptionLoopID'),
       title: 'Зацикленность',
       isSelected: false,
       onChange: (isOn) => LoopChangedEvent(isOn)
   );

   static List<NumericOptionData> timeoutsOptions = [
      NumericOptionData(
          key: const Key('TimeoutsOptionManifestID'),
          title: 'Манифест (мс)',
          value: 5000,
          onChange: (value) => PlaylistTimeoutsChangedEvent(value)
      ),
      NumericOptionData(
          key: const Key('TimeoutsOptionTrackID'),
          title: 'Трек (мс)',
          value: 3000,
          onChange: (value) => TrackTimeoutsChangedEvent(value)
      ),
      NumericOptionData(
          key: const Key('TimeoutsOptionChunkID'),
          title: 'Сегмент (мс)',
          value: 3000,
          onChange: (value) => ChunkTimeoutsChangedEvent(value)
      )
  ];

  static const logOptions = {
    "LogType.off": OptionData(key: Key('LogOptionOffId'), title: 'Выкл.'),
    "LogType.info": OptionData(key: Key('LogOptionInfoID'), title: 'Инфо.'),
    "LogType.debug": OptionData(key: Key('LogOptionDebugID'), title: 'Отладка')
  };
}

