import 'package:flutter/material.dart';
import 'package:loopin/components/shark_video.dart';
import 'package:media_kit/media_kit.dart';
import 'package:media_kit_video/media_kit_video.dart';

class PreviewVideo extends StatefulWidget {
  final String videoUrl;
  final double? width;
  final double? height;

  const PreviewVideo({
    super.key,
    required this.videoUrl,
    this.width,
    this.height,
  });

  @override
  State<PreviewVideo> createState() => _PreviewVideoPageState();
}

class _PreviewVideoPageState extends State<PreviewVideo> {
  late final Player _player = Player();
  late VideoController videoController = VideoController(_player);

  @override
  void initState() {
    super.initState();
    _player.open(Media(widget.videoUrl));
  }

  @override
  void dispose() {
    _player.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    final videoWidth = widget.width ?? 1.0;
    final videoHeight = widget.height ?? 1.0;
    final isHorizontal = videoWidth > videoHeight;

    return SafeArea(
      child: Stack(
        children: [
          Scaffold(
            backgroundColor: Colors.black,
            body: Center(
              child: Video(
                controller: videoController,
                fit: isHorizontal ? BoxFit.contain : BoxFit.cover,
                controls: (state) => MyMaterialVideoControls(state),
              ),
            ),
          ),
          // 关闭按钮
          Positioned(
            top: 20,
            left: 20,
            child: GestureDetector(
              onTap: () {
                Navigator.pop(context);
              },
              child: const Icon(
                Icons.close,
                color: Colors.white,
                size: 28,
              ),
            ),
          ),
        ],
      ),
    );
  }
}
