import 'package:apivideo_live_stream/apivideo_live_stream.dart';
import 'package:flutter/material.dart';
import 'package:recording_tool/shared/widgets/app_bar.dart';

class PushSteamTestWidget extends StatefulWidget {
  const PushSteamTestWidget({super.key});

  final String rtmpUrl = 'rtmp://192.168.1.245:19090/live/2026-07-07';

  @override
  State<PushSteamTestWidget> createState() => _PushSteamTestWidgetState();
}

class _PushSteamTestWidgetState extends State<PushSteamTestWidget>
    with WidgetsBindingObserver {
  late final ApiVideoLiveStreamController _controller;
  bool _ready = false;
  bool _isStreaming = false;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);

    _controller = ApiVideoLiveStreamController(
      initialAudioConfig: AudioConfig(bitrate: 128000),
      initialVideoConfig: VideoConfig.withDefaultBitrate(
        resolution: Resolution.RESOLUTION_1080,
        fps: 30,
      ),
      onConnectionSuccess: () => {
        debugPrint('推流成功'),

        setState(() => _isStreaming = true),
      },
      onConnectionFailed: (reason) {
        setState(() => _isStreaming = false);
        debugPrint('推流失败: $reason');
      },
      onDisconnection: () => setState(() => _isStreaming = false),
    );
    WidgetsBinding.instance.addPostFrameCallback((_) => _initialize());
  }

  Future<void> _initialize() async {
    try {
      await _controller.initialize();
      await _controller.startPreview();

      setState(() => _ready = true);
    } catch (e) {
      debugPrint('初始化失败: $e');
    }
  }

  // 把服务端下发的完整 rtmp:// 地址拆成 "服务器地址" + "streamKey" 两部分
  Future<void> _startPush() async {
    final uri = Uri.parse(widget.rtmpUrl);
    final streamKey = uri.pathSegments.last;
    final baseUrl = widget.rtmpUrl.substring(
      0,
      widget.rtmpUrl.lastIndexOf(streamKey),
    );
    await _controller.startStreaming(streamKey: streamKey, url: baseUrl);
  }

  Future<void> _stopPush() => _controller.stopStreaming();

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    if (state == AppLifecycleState.inactive) {
      _controller.stop();
    } else if (state == AppLifecycleState.resumed) {
      _controller.startPreview();
    }
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: myAppBar(context: context),
      body: Stack(
        children: [
          if (_ready)
            ApiVideoCameraPreview(controller: _controller, fit: BoxFit.cover),
          Positioned(
            bottom: 32,
            left: 0,
            right: 0,
            child: Center(
              child: FloatingActionButton(
                backgroundColor: _isStreaming ? Colors.red : Colors.green,
                onPressed: _ready
                    ? (_isStreaming ? _stopPush : _startPush)
                    : null,
                child: Icon(_isStreaming ? Icons.stop : Icons.circle),
              ),
            ),
          ),
        ],
      ),
    );
  }
}
