part of 'audio_effector.dart';

/// Created by Haoyi on 2022/8/25

class SoundEffect {
  static const off = 0;
  static const on = 1;
  static const reduced = 2;
}

class AudioEffect {
  final String path;
  final String? package;

  const AudioEffect(this.path, {this.package});

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is AudioEffect &&
          runtimeType == other.runtimeType &&
          path == other.path &&
          package == other.package;

  @override
  int get hashCode => path.hashCode ^ package.hashCode;
}

abstract class Audio {
  Future<AudioStreamControl?> play([int repeat = 0]);

  Future<void> load();

  void dispose();

  String prefixFile(String file) {
    return 'assets/audio/$file';
  }
}

class SingleSoundPool extends Audio {
  static final SoundPoolCache _cache = SoundPoolCache();
  final AudioEffect audio;

  SingleSoundPool({
    required this.audio,
  });

  @override
  Future<void> load() async {
    await _cache.load(audio);
  }

  @override
  Future<AudioStreamControl?> play([int repeat = 0]) {
    return _cache.play(audio, repeat);
  }

  @override
  void dispose() {
    _cache.invalidate(audio);
  }
}
