import 'package:flutter/material.dart';
import 'package:flutter_svga/flutter_svga.dart';
import 'package:tancent_vap/utils/constant.dart';
import 'package:tancent_vap/widgets/vap_view.dart';
import 'package:yumi/shared/tools/sc_gift_vap_svga_manager.dart';

class VapPlusSvgaPlayer extends StatefulWidget {
  final String tag;

  const VapPlusSvgaPlayer({super.key, required this.tag});

  @override
  State<VapPlusSvgaPlayer> createState() => _VapPlusSvgaPlayerState();
}

class _VapPlusSvgaPlayerState extends State<VapPlusSvgaPlayer>
    with TickerProviderStateMixin {
  late SVGAAnimationController _svgaController;

  void _giftFxLog(String message) {
    debugPrint('[GiftFX][Widget] $message');
  }

  @override
  void initState() {
    super.initState();
    _svgaController = SVGAAnimationController(vsync: this);
    _giftFxLog('initState tag=${widget.tag}');
    if (widget.tag == "room_gift") {
      SCGiftVapSvgaManager().bindSvgaCtrl(_svgaController);
    }
  }

  @override
  void dispose() {
    _giftFxLog('dispose tag=${widget.tag}');
    SCGiftVapSvgaManager().dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return IgnorePointer(
      child: RepaintBoundary(
        child: Stack(
          children: [
            Positioned.fill(
              child: RepaintBoundary(
                child: VapView(
                  scaleType: ScaleType.fitCenter,
                  onViewCreated: (controller) {
                    _giftFxLog('onViewCreated tag=${widget.tag}');
                    if (widget.tag == "room_gift") {
                      SCGiftVapSvgaManager().bindVapCtrl(controller);
                      // VapManager().play("https://res.cloudinary.com/dkmchpua1/video/upload/v1737624783/vcg9co6yyfqsadgety1n.mp4");
                    }
                  },
                ),
              ),
            ),
            Positioned.fill(
              child: RepaintBoundary(
                child: SVGAImage(
                  _svgaController,
                  fit: BoxFit.contain,
                  allowDrawingOverflow: false,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
