getSurahStatistics static method

Future<SurahStatistics> getSurahStatistics()

Get comprehensive statistics about the Quran.

Returns SurahStatistics containing detailed analytics.

Example:

final stats = await QuranService.getSurahStatistics();
print('${stats.totalSurahs} surahs, ${stats.totalAyat} ayat');

Implementation

static Future<SurahStatistics> getSurahStatistics() async {
  await initialize();
  _ensureDataLoaded();

  final surahs = _quranData!.surahs;

  // Basic counts
  final totalSurahs = surahs.length;
  final totalAyat =
      surahs.fold<int>(0, (sum, surah) => sum + surah.numberOfAyahs);
  final meccanSurahs = surahs.where((s) => s.isMeccan).length;
  final medinanSurahs = surahs.where((s) => s.isMedianan).length;

  // Find extremes
  final longestSurah = surahs.reduce(
      (curr, next) => curr.numberOfAyahs > next.numberOfAyahs ? curr : next);
  final shortestSurah = surahs.reduce(
      (curr, next) => curr.numberOfAyahs < next.numberOfAyahs ? curr : next);

  // Calculate averages
  final averageAyatPerSurah = totalAyat / totalSurahs;

  // Create distribution map
  final Map<int, int> distribution = {};
  for (final surah in surahs) {
    distribution[surah.numberOfAyahs] =
        (distribution[surah.numberOfAyahs] ?? 0) + 1;
  }

  // Find mode (most common length)
  final mode = distribution.entries
      .reduce((curr, next) => curr.value > next.value ? curr : next)
      .key;

  // Calculate revelation analysis
  final meccanAyat = surahs
      .where((s) => s.isMeccan)
      .fold<int>(0, (sum, s) => sum + s.numberOfAyahs);
  final medinanAyat = surahs
      .where((s) => s.isMedianan)
      .fold<int>(0, (sum, s) => sum + s.numberOfAyahs);

  final meccanLengths =
      surahs.where((s) => s.isMeccan).map((s) => s.numberOfAyahs).toList();
  final medinanLengths =
      surahs.where((s) => s.isMedianan).map((s) => s.numberOfAyahs).toList();

  return SurahStatistics(
    totalSurahs: totalSurahs,
    totalAyat: totalAyat,
    meccanSurahs: meccanSurahs,
    medinanSurahs: medinanSurahs,
    averageAyatPerSurah: averageAyatPerSurah,
    longestSurah: longestSurah,
    shortestSurah: shortestSurah,
    ayatCounts: AyatCounts(
      min: shortestSurah.numberOfAyahs,
      max: longestSurah.numberOfAyahs,
      median: _calculateMedian(surahs.map((s) => s.numberOfAyahs).toList()),
      mode: mode,
      distribution: distribution,
    ),
    revelationAnalysis: RevelationAnalysis(
      meccanCharacteristics: RevelationCharacteristics(
        averageLength: meccanLengths.isNotEmpty
            ? meccanLengths.reduce((a, b) => a + b) / meccanLengths.length
            : 0,
        totalAyat: meccanAyat,
        shortestLength: meccanLengths.isNotEmpty
            ? meccanLengths.reduce((a, b) => a < b ? a : b)
            : 0,
        longestLength: meccanLengths.isNotEmpty
            ? meccanLengths.reduce((a, b) => a > b ? a : b)
            : 0,
      ),
      medinanCharacteristics: RevelationCharacteristics(
        averageLength: medinanLengths.isNotEmpty
            ? medinanLengths.reduce((a, b) => a + b) / medinanLengths.length
            : 0,
        totalAyat: medinanAyat,
        shortestLength: medinanLengths.isNotEmpty
            ? medinanLengths.reduce((a, b) => a < b ? a : b)
            : 0,
        longestLength: medinanLengths.isNotEmpty
            ? medinanLengths.reduce((a, b) => a > b ? a : b)
            : 0,
      ),
    ),
  );
}