import 'package:nut_player_example/src/features/settings_screen/domain/settings_bloc.dart';

class VideoQualityOption {
  String id;
  int bandwidth;
  int width;
  int height;
  String title;
  late VideoQuality quality;

  VideoQualityOption({
    required this.id,
    required this.bandwidth,
    required this.width,
    required this.height,
    required this.title
  }) {
    quality = _videoQuality(width,height,bandwidth);
  }

  static VideoQuality _qualityFromResolution(int pixels) {
    if (pixels >= 1000 && pixels < 90500) {
      return VideoQuality.p144;
    } else if (pixels >= 90500 && pixels < 170500) {
      return VideoQuality.p240;
    } else if (pixels >= 170500 && pixels < 280500) {
      return VideoQuality.p360;
    } else if (pixels >= 280500 && pixels < 640500) {
      return VideoQuality.p480;
    } else if (pixels >= 640500 && pixels < 1500500) {
      return VideoQuality.p720;
    } else if (pixels >= 1500500 && pixels < 2400500) {
      return VideoQuality.p1080;
    } else if (pixels >= 2400500 && pixels < 6000500) {
      return VideoQuality.p1440;
    } else if (pixels >= 6000500) {
      return VideoQuality.p2160;
    } else {
      return VideoQuality.auto;
    }
  }

  static VideoQuality _qualityFromBandwidth(int bandwidth) {
    if (bandwidth >= 1 && bandwidth < 400001) {
      return VideoQuality.p144;
    } else if (bandwidth >= 400001 && bandwidth < 800001) {
      return VideoQuality.p240;
    } else if (bandwidth >= 800001 && bandwidth < 1200001) {
      return VideoQuality.p360;
    } else if (bandwidth >= 1200001 && bandwidth < 1800001) {
      return VideoQuality.p480;
    } else if (bandwidth >= 1800001 && bandwidth < 3500001) {
      return VideoQuality.p720;
    } else if (bandwidth >= 3500001 && bandwidth < 8000001) {
      return VideoQuality.p1080;
    } else if (bandwidth >= 8000001 && bandwidth < 12000001) {
      return VideoQuality.p1440;
    } else if (bandwidth >= 12000001) {
      return VideoQuality.p2160;
    } else {
      return VideoQuality.auto;
    }
  }

  static VideoQuality _videoQuality(int width, int height, int bandwidth) {
    if (width != 0 && height != 0) {
      final pixels = width * height;
      return _qualityFromResolution(pixels);
    } else {
      return _qualityFromBandwidth(bandwidth);
    }
  }
}