import 'package:get/get_rx/src/rx_types/rx_types.dart';
import 'package:injectable/injectable.dart';

import '../../../utils/count_down_timer.dart';

/// 商店商品倒计时管理器
@lazySingleton
class StoreGoodsCountdownManager {
  /// 当前时间
  Rx<Duration> currentDuration = Duration.zero.obs;

  late CountdownTimer _timer;

  StoreGoodsCountdownManager() {
    _timer =
        CountdownTimer(
            initialDuration: Duration(minutes: 30),
            autoRestart: false,
          )
          ..onTick = (duration) {
            // 倒计时更新
            currentDuration.value = duration;
          }
          ..onCompleted = () {
            // 倒计时完成，重新开始
            _timer.reset();
            _timer.start();
          };
    startCountdown();
  }

  /// 开始倒计时
  void startCountdown() {
    _timer.start();
  }

  /// 暂停倒计时
  void pauseCountdown() {
    _timer.pause();
  }
}
