import 'package:daniulive/controllers/play_controller.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';

import 'index.dart';

class FullScreenPlayerController extends GetxController {
  FullScreenPlayerController({this.flvUrl = ""});

  final String flvUrl;

  final state = FullScreenPlayerState();

  PlayController? playController;

  void handlePageBack() {
    Get.back();
  }

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    playController = PlayController(
      flvUrl,
      userId: "full_screen_player",
    );
    playController!.init();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.immersive);
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.landscapeLeft, DeviceOrientation.landscapeRight]);
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
    disposePlayer();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
    SystemChrome.setPreferredOrientations(
        [DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  }

  void disposePlayer() {
    if (playController != null) {
      playController!.stop();
      playController!.dispose();
      playController = null;
    }
  }
}
