import 'dart:async';
import 'package:flutter/foundation.dart';

class OtpTimerHelper {
  final ValueNotifier<int> remainingSeconds = ValueNotifier(60);
  final ValueNotifier<bool> canResend = ValueNotifier(false);
  Timer? _timer;

  void startTimer() {
    canResend.value = false;
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      if (remainingSeconds.value > 0) {
        remainingSeconds.value--;
      } else {
        canResend.value = true;
        _timer?.cancel();
      }
    });
  }

  void resetTimer() {
    _timer?.cancel();
    remainingSeconds.value = 60;
    startTimer();
  }
  
  String formatTime() {
    int seconds = remainingSeconds.value;
    return "0:${seconds.toString().padLeft(2, '0')}";
  }

  void dispose() {
    _timer?.cancel();
    remainingSeconds.dispose();
    canResend.dispose();
  }
}