import 'package:flutter/material.dart';
import 'package:daniulive/views/play_view.dart';
import 'package:get/get.dart';
import 'index.dart';

/// 简易全屏播放器
class FullScreenPlayerPage extends GetView<FullScreenPlayerController> {
  const FullScreenPlayerPage({Key? key, required this.flvUrl})
      : super(key: key);
  final String flvUrl;

  // 主视图
  Widget _buildView() {
    return PlayView(
      controller: controller.playController!,
      onViewCreated: (value) async {
        print("onViewCreated: $value");
        await Future.delayed(Duration(milliseconds: 300));
        controller.playController!.play();
        await Future.delayed(Duration(milliseconds: 200));
        controller.state.isShowMask = false;
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<FullScreenPlayerController>(
      init: FullScreenPlayerController(flvUrl: flvUrl),
      builder: (_) {
        return Center(
          child: Container(
            color: Colors.black,
            child: SafeArea(
              child: Stack(
                children: [
                  Center(child: SizedBox.expand(child: _buildView())),
                  Positioned(
                    child: InkWell(
                      onTap: () {
                        controller.handlePageBack();
                      },
                      child: Container(
                        decoration: BoxDecoration(
                          color: Colors.white.withOpacity(.4),
                          borderRadius: BorderRadius.circular(22),
                        ),
                        padding: const EdgeInsets.all(8),
                        margin: const EdgeInsets.only(top: 4, left: 4),
                        child: const Icon(
                          Icons.close,
                          size: 28,
                          color: Colors.white,
                        ),
                      ),
                    ),
                  ),
                  _buildBlackMask(),
                ],
              ),
            ),
          ),
        );
      },
    );
  }

  /// 全黑蒙层
  Widget _buildBlackMask() {
    return Obx(
      () {
        if (controller.state.isShowMask) {
          return Center(
            child: SizedBox.expand(
                child: Container(
              color: Colors.black,
              child: Center(
                child: CircularProgressIndicator(
                  color: Colors.grey,
                ),
              ),
            )),
          );
        } else {
          return SizedBox();
        }
      },
    );
  }
}
