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

class AudioSlider extends StatelessWidget {
  final String tag;
  final bool showTimer;
  final int? duration;
  final bool disableThumb;
  const AudioSlider({
    Key? key,
    required this.tag,
    this.showTimer = false,
    this.duration,
    this.disableThumb = false,
  }) : super(key: key);

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

  @override
  Widget build(BuildContext context) {
    return IgnorePointer(
      ignoring: !_isPlaying,
      child: Directionality(
        textDirection: TextDirection.ltr,
        child: StreamBuilder<Duration>(
          stream: _isPlaying ? MediaService.audioPlayer.positionStream : null,
          builder: (context, snapshot) {
            return Row(
              children: [
                Expanded(
                  child: ProgressBar(
                    thumbColor: Theme.of(context).colorScheme.title,
                    progressBarColor: DesignConfig.isDark
                        ? Theme.of(context).colorScheme.title
                        : Theme.of(context).colorScheme.primary,
                    baseBarColor: Theme.of(context).colorScheme.border,
                    bufferedBarColor: Theme.of(context).colorScheme.splash,
                    total: Duration(
                        seconds:
                            duration ?? MediaService.duration?.inSeconds ?? 0),
                    progress: snapshot.data ?? Duration.zero,
                    thumbRadius: disableThumb ? 0 : 6,
                    barHeight: 3,
                    timeLabelTextStyle: TextStyle(
                      fontSize: showTimer ? null : 0,
                      height: showTimer ? 3 : 0,
                      color: Theme.of(context).colorScheme.text,
                      fontFamily: DesignConfig.fontFamily.replaceAll(
                        '-FA',
                        '',
                      ),
                    ),
                    onSeek: (value) => _onSeek(value.inMilliseconds),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

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