part of 'control_board.dart';

class _PlayButton extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PlayButtonState();
}

class _PlayButtonState extends State<_PlayButton> {
  final playerController = Get.find<IPlayerController>() as VidPlayerController;

  @override
  void initState() {
    WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
      if (mounted) {
        playerController.eventHandler.addListener(onControllerEvent);
      }
    });
    super.initState();
  }

  @override
  void dispose() {
    playerController.eventHandler.removeListener(onControllerEvent);
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final playing = playerController.playing;
    const iconSize = 36.0;
    return IconButton(
      splashRadius: 2,
      padding: EdgeInsets.zero,
      onPressed: () {
        if (playing) {
          playerController.pause();
        } else {
          playerController.play();
        }
      },
      icon: playing
          ? const Icon(Icons.stop_circle_outlined, size: iconSize)
          : const Icon(Icons.play_circle_outline, size: iconSize),
    );
  }

  void onControllerEvent(Object sender, VidPlayerEvent e) {
    if (e is VidPlayerStatusChangeEvent) {
      onPlayStatusChanged(e);
    }
  }

  void onPlayStatusChanged(VidPlayerStatusChangeEvent e) {
    if (mounted) {
      setState(() {});
    }
  }
}
