import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/classes/colors.dart';
import 'package:mohem_flutter_app/widgets/AnimatedProgressBar.dart';
import 'package:video_player/video_player.dart';

class MyVideoProgressIndicator extends StatefulWidget {
  const MyVideoProgressIndicator(
    this.controller, {
    Key? key,
    this.colors = const VideoProgressColors(),
    required this.allowScrubbing,
    this.padding = const EdgeInsets.only(top: 5.0),
  }) : super(key: key);

  final VideoPlayerController controller;

  final VideoProgressColors colors;

  final bool allowScrubbing;

  final EdgeInsets padding;

  @override
  State<MyVideoProgressIndicator> createState() => _VideoProgressIndicatorState();
}

class _VideoProgressIndicatorState extends State<MyVideoProgressIndicator> {
  _VideoProgressIndicatorState() {
    listener = () {
      if (!mounted) {
        return;
      }
      setState(() {});
    };
  }

  late VoidCallback listener;

  VideoPlayerController get controller => widget.controller;

  VideoProgressColors get colors => widget.colors;

  @override
  void initState() {
    super.initState();
    controller.addListener(listener);
  }

  @override
  void deactivate() {
    controller.removeListener(listener);
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    Widget progressIndicator;
    if (controller.value.isInitialized) {
      int duration = controller.value.duration.inMilliseconds;
      int position = controller.value.position.inMilliseconds;

      int maxBuffering = 0;
      for (DurationRange range in controller.value.buffered) {
        int end = range.end.inMilliseconds;
        if (end > maxBuffering) {
          maxBuffering = end;
        }
      }

      progressIndicator = Stack(
        fit: StackFit.passthrough,
        children: <Widget>[
          // LinearProgressIndicator(
          //   value: maxBuffering / duration,
          //   valueColor: AlwaysStoppedAnimation<Color>(colors.bufferedColor),
          //   backgroundColor: colors.backgroundColor,
          //   minHeight: 8,
          // ),
          // LinearProgressIndicator(
          //   value: position / duration,
          //   valueColor: AlwaysStoppedAnimation<Color>(colors.playedColor),
          //   backgroundColor: Colors.transparent,
          //   minHeight: 8,
          // ),

          AnimatedProgressBar(
            value: position / duration,
            duration: const Duration(seconds: 2),
            height: 43,
            width: MediaQuery.of(context).size.width - 120,
            gradient: const LinearGradient(
              transform: GradientRotation(.83),
              begin: Alignment.topRight,
              end: Alignment.bottomLeft,
              colors: [
                MyColors.gradiantEndColor,
                MyColors.gradiantStartColor,
              ],
            ),
            backgroundColor: Colors.grey.withOpacity(0.6),
          ),
        ],
      );
    } else {
      // progressIndicator = LinearProgressIndicator(
      //   valueColor: AlwaysStoppedAnimation<Color>(colors.playedColor),
      //   backgroundColor: colors.backgroundColor,
      //   minHeight: 8,
      // );
      progressIndicator = AnimatedProgressBar(
        value: 1,
        duration: const Duration(seconds: 2),
        height: 43,
        width: MediaQuery.of(context).size.width - 120,
        gradient: const LinearGradient(
          transform: GradientRotation(.83),
          begin: Alignment.topRight,
          end: Alignment.bottomLeft,
          colors: [
            MyColors.gradiantEndColor,
            MyColors.gradiantStartColor,
          ],
        ),
        backgroundColor: Colors.grey.withOpacity(0.6),
      );
    }
    Widget paddedProgressIndicator = Padding(padding: widget.padding, child: progressIndicator);
    if (widget.allowScrubbing) {
      return VideoScrubber(controller: controller, child: paddedProgressIndicator);
    } else {
      return paddedProgressIndicator;
    }
  }
}
