import 'dart:async';

import 'package:appinio_swiper/appinio_swiper.dart';
import 'package:flutter/cupertino.dart';
import 'package:mohem_flutter_app/config/routes.dart';

class MarathonProvider extends ChangeNotifier {
  final AppinioSwiperController swiperController = AppinioSwiperController();

  bool _itsMarathonTime = false;

  bool get itsMarathonTime => _itsMarathonTime;

  set itsMarathonTime(bool value) {
    _itsMarathonTime = value;
    notifyListeners();
  }

  int _currentQuestionNumber = 1;
  final int totalQuestions = 10;

  int get currentQuestionNumber => _currentQuestionNumber;

  set currentQuestionNumber(int value) {
    _currentQuestionNumber = value;
    notifyListeners();
  }

  Timer timer = Timer.periodic(const Duration(seconds: 1), (Timer timer) {});
  int start = 5;

  void startTimer(BuildContext context) {
    start = 5;
    const Duration oneSec = Duration(seconds: 1);
    timer = Timer.periodic(
      oneSec,
      (Timer timer) {
        if (start == 0) {
          if (currentQuestionNumber == 10) {
            timer.cancel();
            Navigator.pushNamed(context, AppRoutes.marathonWinnerSelection);
          }
          swiperController.swipeLeft();
          currentQuestionNumber = currentQuestionNumber + 1;
          timer.cancel();
        } else {
          start--;
        }
        notifyListeners();
      },
    );
  }

  void cancelTimer() {
    timer.cancel();
    notifyListeners();
  }
}
