import 'package:chewie/chewie.dart';
import 'package:flutter/Material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
import 'package:get/get.dart';

import '../resource/assets.gen.dart';
 void playVideoDialog(AssetEntity asset) async {
  try {
    final mediaUrl = await asset.getMediaUrl();
    if (mediaUrl == null) return;

    final videoPlayerController =
    VideoPlayerController.networkUrl(Uri.parse(mediaUrl));
    await videoPlayerController.initialize();

    final chewieController = ChewieController(
      videoPlayerController: videoPlayerController,
      autoPlay: true,
      looping: true,
      showOptions: false,
    );
    Get.dialog(
      useSafeArea: false,
      Dialog(
        insetPadding: EdgeInsets.zero,
        backgroundColor: Colors.black,
        child: SizedBox(
          width: double.infinity,
          height: double.infinity,
          child: Stack(
            children: [
              SafeArea(child:   Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  GestureDetector(
                    onTap: () {
                      Get.back();
                    },
                    child:Padding(padding: EdgeInsets.only(left: 16.w,top: 16.h),child:  Assets.images.iconBackArrow.image(
                      width: 28.w,
                      height: 28.h,
                    )),
                  ),
                  Expanded(
                    child: Chewie(controller: chewieController),
                  ),
                ],
              ),),
              IgnorePointer(
                child: Assets.images.bgHome.image(
                  width: 360.w,
                  fit: BoxFit.cover,
                ),
              ),
            ],
          ),
        ),
      ),
      barrierDismissible: true,
    ).then((_) {
      videoPlayerController.dispose();
      chewieController.dispose();
    });
    await videoPlayerController.play();
  } catch (e) {
    print('Error playing video: $e');
    Get.snackbar('Error', 'Failed to play video');
  }
}
