import 'package:flutter/material.dart';
import 'package:flutter_webrtc/flutter_webrtc.dart';
import 'package:vsdk/app_player.dart';

import '../../../../enums/video_stream_mode_enum.dart';

/// 通用视频全屏播放页面（兼容 WebRTC + P2P）
///
/// 全屏展示摄像头实时画面，点击任意位置退出全屏。
///
/// 涉及页面：监控页 - 监控画面
class VideoFullscreenPage extends StatelessWidget {
  /// 当前视频传输模式
  final VideoStreamMode mode;

  /// WebRTC 模式下的视频渲染器
  final RTCVideoRenderer? webrtcRenderer;

  /// P2P 模式下的播放控制器
  final AppPlayerController? p2pController;

  const VideoFullscreenPage({
    super.key,
    required this.mode,
    this.webrtcRenderer,
    this.p2pController,
  });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: GestureDetector(
        onTap: () => Navigator.of(context).pop(),
        child: Stack(
          fit: StackFit.expand,
          children: [
            // 根据模式渲染不同的视频画面
            _buildVideoContent(),
            // 底部提示栏
            _buildBottomBar(),
          ],
        ),
      ),
    );
  }

  /// 按模式渲染视频内容
  Widget _buildVideoContent() {
    if (mode == VideoStreamMode.p2p && p2pController != null) {
      return AppPlayerView(controller: p2pController!);
    }
    if (mode == VideoStreamMode.webrtc && webrtcRenderer != null) {
      return RTCVideoView(
        webrtcRenderer!,
        objectFit: RTCVideoViewObjectFit.RTCVideoViewObjectFitContain,
      );
    }
    return const SizedBox.shrink();
  }

  /// 底部提示栏（左下角）
  Widget _buildBottomBar() {
    return Positioned(
      bottom: 0,
      left: 0,
      right: 0,
      child: Container(
        padding: const EdgeInsets.fromLTRB(20, 12, 20, 48),
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.bottomCenter,
            end: Alignment.topCenter,
            colors: [Colors.black54, Colors.transparent],
          ),
        ),
        child: const Row(
          children: [
            Icon(Icons.fullscreen_exit, color: Colors.white70, size: 20),
            SizedBox(width: 8),
            Text(
              '点击任意位置退出全屏',
              style: TextStyle(color: Colors.white70, fontSize: 14),
            ),
          ],
        ),
      ),
    );
  }
}
