import 'dart:ui';
import 'package:flutter/material.dart';
import '../utils/video_player_utils.dart';
import './video_player_bottom.dart';
import './video_player_center.dart';
import './video_player_gestures.dart';
import './video_player_top.dart';

class VideoPlayerPage extends StatefulWidget {
  const VideoPlayerPage({
    Key? key,
    required this.url,
  }) : super(key: key);
  final String url;
  @override
  _VideoPlayerPageState createState() => _VideoPlayerPageState();
}

class _VideoPlayerPageState extends State<VideoPlayerPage> {
  // 是否全屏
  bool get _isFullScreen =>
      MediaQuery.of(context).orientation == Orientation.landscape;
  Size get _window => MediaQueryData.fromWindow(window).size;
  double get _width => _isFullScreen ? _window.width : _window.width;
  double get _height => _isFullScreen ? _window.height : _window.width * 9 / 16;
  Widget? _playerUI;
  VideoPlayerTop? _top;
  VideoPlayerBottom? _bottom;
  LockIcon? _lockIcon; // 控制是否沉浸式的widget
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    VideoPlayerUtils.playerHandle(widget.url, autoPlay: false, looping: true);
    VideoPlayerUtils.initializedListener(
        key: this,
        listener: (initialize, widget) {
          if (initialize) {
            _top ??= VideoPlayerTop();
            // _lockIcon ??= LockIcon(
            //   lockCallback: () {
            //     _top!.opacityCallback(!TempValue.isLocked);
            //     _bottom!.opacityCallback(!TempValue.isLocked);
            //   },
            // );
            // _lockIcon ??= Container();
            _bottom ??= VideoPlayerBottom();
            _playerUI = widget;
            if (!mounted) return;
            setState(() {});
          }
        });
  }

  @override
  void didUpdateWidget(VideoPlayerPage oldWidget) {
    if (widget.url != oldWidget.url) {
      _playerUI = null;
      setState(() {});
      VideoPlayerUtils.playerHandle(widget.url, autoPlay: false, looping: true);
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  void dispose() {
    super.dispose();
    VideoPlayerUtils.removeInitializedListener(this);
    VideoPlayerUtils.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: _isFullScreen
          ? null
          : AppBar(
              title: const Text("视频示例"),
            ),
      body: safeAreaPlayerUI(),
    );
  }

  Widget safeAreaPlayerUI() {
    return SafeArea(
      // 全屏的安全区域
      top: !_isFullScreen,
      bottom: !_isFullScreen,
      left: !_isFullScreen,
      right: !_isFullScreen,
      child: Container(
        height: _height,
        width: _width,
        child: _playerUI != null
            ? VideoPlayerGestures(
                appearCallback: (appear) {
                  _top!.opacityCallback(appear);
                  // _lockIcon!.opacityCallback(appear);
                  _bottom!.opacityCallback(appear);
                },
                children: [
                  Center(
                    child: _playerUI,
                  ),
                  _top!,
                  // _lockIcon!,
                  _bottom!
                ],
              )
            : Container(
                alignment: Alignment.center,
                color: Colors.black,
                child: const CircularProgressIndicator(
                  strokeWidth: 3,
                ),
              ),
      ),
    );
  }
}
