import 'package:audio_video_progress_bar/audio_video_progress_bar.dart';
import 'package:didvan/services/media/media.dart';
import 'package:flutter/material.dart';

class AudioSlider extends StatelessWidget {
  final String tag;
  const AudioSlider({Key? key, required this.tag}) : super(key: key);

  bool get _isPlaying => MediaService.audioPlayerTag == tag;

  @override
  Widget build(BuildContext context) {
    return IgnorePointer(
      ignoring: MediaService.audioPlayerTag != tag,
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: StreamBuilder<Duration>(
          stream: _isPlaying ? MediaService.audioPlayer.positionStream : null,
          builder: (context, snapshot) {
            return ProgressBar(
              total: MediaService.audioPlayer.duration ?? Duration.zero,
              progress: snapshot.data ?? Duration.zero,
              buffered:
                  _isPlaying ? MediaService.audioPlayer.bufferedPosition : null,
              thumbRadius: 6,
              barHeight: 3,
              timeLabelTextStyle: const TextStyle(fontSize: 0),
              onSeek: (value) => _onSeek(value.inMilliseconds),
            );
          },
        ),
      ),
    );
  }

  void _onSeek(int value) {
    MediaService.audioPlayer.seek(Duration(milliseconds: value));
  }
}
