import 'dart:async';

import 'package:fis_measure/view/mobile_view/widgets/throttle.dart' as utils;
import 'package:flutter/services.dart';
import 'package:get/get.dart';

class ProgressPanelController extends GetxController {
  ProgressPanelController({this.onPlayButtonClicked, this.onFrameChangeEvent});

  static const progressPanelId = "progress_panel";

  ValueChanged<bool>? onPlayButtonClicked;

  bool Function(int)? onFrameChangeEvent;

  RxString _progressStr = "0 : 0".obs;
  String get progressStr => _progressStr.value;
  set progressStr(String value) => _progressStr.value = value;

  RxString _durationStr = "0s : 0s".obs;
  String get durationStr => _durationStr.value;
  set durationStr(String value) => _durationStr.value = value;

  RxBool _isPlaying = false.obs;
  bool get isPlaying => _isPlaying.value;
  set isPlaying(bool value) => _isPlaying.value = value;

  RxInt _currentFrameIndex = 0.obs;
  int get currentFrameIndex => _currentFrameIndex.value;
  set currentFrameIndex(int value) => _currentFrameIndex.value = value;

  RxInt _totalFrames = 0.obs;
  int get totalFrames => _totalFrames.value;
  set totalFrames(int value) => _totalFrames.value = value;

  RxDouble _bufferProgress = (0.0).obs;
  double get bufferProgress => _bufferProgress.value;
  set bufferProgress(double value) => _bufferProgress.value = value;

  RxSet<int> _keyFrameSet = RxSet({});
  // ignore: invalid_use_of_protected_member
  Set<int> get keyFrameSet => _keyFrameSet.value;

  bool _enable = false;
  bool get enable => _enable;

  // 更新播放进度
  void updateProgress({
    required int currentFrameIndex,
    required int totalFrames,
    required double totalDuration,
  }) {
    this.currentFrameIndex = currentFrameIndex;
    this.totalFrames = totalFrames;
    // FIXME 时间实际显示的应该是总时间轴上的时间，例如 第一帧显示的时间可能是 12.2s : 60s
    progressStr = "$currentFrameIndex : ${totalFrames - 1}";
    double currentFrameTime =
        (currentFrameIndex / (totalFrames - 1)) * totalDuration;
    if (currentFrameTime.isNaN) {
      currentFrameTime = 0;
    }
    durationStr =
        "${currentFrameTime.toStringAsFixed(1)}s : ${totalDuration.toStringAsFixed(1)}s";
  }

  // 更新播放状态
  void updateStatus(bool isPlaying) {
    this.isPlaying = isPlaying;
  }

  void onClickPlayButton() {
    isPlaying = !isPlaying;
    onPlayButtonClicked?.call(isPlaying);
  }

  void onClickPrevButton() {
    print("onClickPrevButton");
    onFrameChangeEvent?.call(currentFrameIndex - 1);
  }

  void onClickNextButton() {
    print("onClickNextButton");
    onFrameChangeEvent?.call(currentFrameIndex + 1);
  }

  // 拖动进度条
  Future<bool> onProgressChanged(double value) {
    final completer = Completer<bool>();
    utils.throttle(
      () {
        final result = onFrameChangeEvent?.call(value.toInt());
        completer.complete(result);
      },
      'onDragTo',
      50,
    );
    return completer.future;
  }

  // 设置进度条是否可用（是否展示）
  void setProgressPanelEnable(bool enable) {
    _enable = enable;
    update([progressPanelId]);
  }

  // 新增关键帧
  void setKeyFrameMark(List<int> keyFrameIndexs) {
    _keyFrameSet.addAll(keyFrameIndexs);
  }

  // 清空关键帧
  void clearKeyFrameMark() {
    _keyFrameSet.clear();
  }

  // @override
  // void onInit() {
  //   super.onInit();
  // }

  @override
  void onReady() {
    super.onReady();
  }

  // @override
  // void onClose() {
  //   super.onClose();
  // }
}
