import 'package:fis_measure/index.dart';
import 'package:fis_measure/interfaces/process/player/play_controller.dart';
import 'package:fis_vid/data_host/data_host.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vid_player_demo/controller/player_controller.dart';
import 'package:vid_player_demo/widgets/image_cache.dart';

import 'widgets/scene_player.dart';

class ScenePlayerView extends StatefulWidget {
  const ScenePlayerView(
    this.vidURL, {
    Key? key,
    this.viewSize = const Size(600, 500),
  }) : super(key: key);
  final String vidURL;
  final Size viewSize;

  @override
  _ScenePlayerViewState createState() => _ScenePlayerViewState();
}

class _ScenePlayerViewState extends State<ScenePlayerView> {
  IPlayerController? _playerController;
  IPlayerController get playerController => _playerController!;
  VidDataHost? _vidDataHost;
  VidDataHost get dataHost => _vidDataHost!;
  bool _ifInit = false;

  /// 外部播放状态控制器
  PlayerStateController? _playerStateController;
  PlayerStateController get playerStateController => _playerStateController!;
  void loadVidDataHost(String url) {
    print("${DateTime.now()} Load vid data host");
    _vidDataHost = VidDataHost(url);
    _playerController = VidPlayerController(dataHost: dataHost);
  }

  void clickPlay() {
    print("${DateTime.now()} Click play");
    if (_ifInit) {
      playerController.play();
    } else {
      dataHost.load().then((value) {
        if (value == null) {
          // TOOO: add log
        } else {
          playerController.setLoadState(true);
          Future.delayed(const Duration(milliseconds: 100), () {
            playerController.play();
          });
          print("${DateTime.now()} Load vid data host success");
        }
        _ifInit = true;
      });
    }
  }

  void clickPause() {
    print("${DateTime.now()} Click pause");
    playerController.pause();
  }

  void onClickPlay(Object sender, dynamic e) {
    print("${DateTime.now()} receive Play $e");
    clickPlay();
  }

  @override
  void initState() {
    super.initState();
    loadVidDataHost(widget.vidURL);
    _playerStateController = Get.find<PlayerStateController>();
    playerStateController.trigglePlay.addListener(onClickPlay);
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: [
          SizedBox(
            width: widget.viewSize.width,
            height: widget.viewSize.height,
            child: RepaintBoundary(
              child: VidScenePlayer(
                _playerController as VidPlayerController,
              ),
            ),
          ),
          const Text('基于 Scene 的播放器'),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              ElevatedButton(
                  onPressed: () {
                    clickPlay();
                  },
                  child: const Text('▶ Play')),
              ElevatedButton(
                  onPressed: () {
                    clickPause();
                  },
                  child: const Text('⏸ Pause')),
            ],
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    print("ScenePlayerView dispose");
    playerController.dispose();
    dataHost.release();
    playerStateController.trigglePlay.removeListener(onClickPlay);
    super.dispose();
  }
}
