import 'dart:async';

import 'package:appinio_swiper/appinio_swiper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:mohem_flutter_app/api/marathon/marathon_api_client.dart';
import 'package:mohem_flutter_app/models/marathon/marathon_model.dart';
import 'package:mohem_flutter_app/models/marathon/question_model.dart';
import 'package:mohem_flutter_app/ui/marathon/widgets/question_card.dart';

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

  MarathonDetailModel marathonDetailModel = MarathonDetailModel();
  List<CardContent> cardContentList = <CardContent>[];
  QuestionModel currentQuestion = QuestionModel();

  QuestionCardStatus questionCardStatus = QuestionCardStatus.question;

  int? selectedOptionIndex;
  int currentQuestionTime = 0;

  void onNewQuestionReceived(QuestionModel newQuestion) {
    if (currentQuestionNumber > 0) {
      swipeCardLeft();
    }
    selectedOptionIndex = null;
    currentQuestionNumber++;
    currentQuestion = newQuestion;
    cardContentList.add(const CardContent());
    currentQuestionTime = newQuestion.questionTime!;
    questionCardStatus = QuestionCardStatus.question;
    notifyListeners();
  }

  void addItemToList(CardContent value) {
    cardContentList.add(value);
    notifyListeners();
  }

  void updateCurrentQuestionOptionStatus(QuestionsOptionStatus status, int index) {
    for (int i = 0; i < currentQuestion.questionOptions!.length; i++) {
      currentQuestion.questionOptions![i].optionStatus = QuestionsOptionStatus.unSelected;
    }
    currentQuestion.questionOptions![index].optionStatus = status;
    selectedOptionIndex = index;
    notifyListeners();
  }

  void updateQuestionCardStatus(QuestionCardStatus status) {
    questionCardStatus = status;
    notifyListeners();
  }

  bool _isLoading = false;

  bool get isLoading => _isLoading;

  set isLoading(bool value) {
    _isLoading = value;
    notifyListeners();
  }

  bool _itsMarathonTime = false;

  bool get itsMarathonTime => _itsMarathonTime;

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

  bool _isMarathonCompleted = false;

  bool get isMarathonCompleted => _isMarathonCompleted;

  set isMarathonCompleted(bool value) {
    _isMarathonCompleted = value;
    notifyListeners();
  }

  int _currentQuestionNumber = 0;

  int get currentQuestionNumber => _currentQuestionNumber;

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

  int _totalMarathoners = 23;

  int get totalMarathoners => _totalMarathoners;

  set totalMarathoners(int value) {
    _totalMarathoners = value;
    notifyListeners();
  }

  void swipeCardLeft() {
    swiperController.swipeLeft();
    notifyListeners();
  }

  void getCorrectAnswerAndUpdateAnswerColor() {
    if (selectedOptionIndex != null) {
      if (currentQuestion.questionOptions![selectedOptionIndex!].isCorrectOption!) {
        updateCurrentQuestionOptionStatus(QuestionsOptionStatus.correct, selectedOptionIndex!);
      } else {
        updateCurrentQuestionOptionStatus(QuestionsOptionStatus.wrong, selectedOptionIndex!);
      }
    }
  }

  void updateCardStatusToAnswer() {
    if (currentQuestionNumber == 0) {
      return;
    }

    if (selectedOptionIndex != null) {
      if (currentQuestion.questionOptions![selectedOptionIndex!].isCorrectOption!) {
        updateQuestionCardStatus(QuestionCardStatus.correctAnswer);
      } else {
        updateQuestionCardStatus(QuestionCardStatus.wrongAnswer);
      }
    } else {
      updateQuestionCardStatus(QuestionCardStatus.skippedAnswer);
    }
  }

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

  void startTimer(BuildContext context) {
    const Duration oneSec = Duration(seconds: 1);
    timerU = Timer.periodic(
      oneSec,
      (Timer timer) async {
        if (currentQuestionTime == 2) {
          getCorrectAnswerAndUpdateAnswerColor();
        }
        if (currentQuestionTime == 0) {
          updateCardStatusToAnswer();
          // if (currentQuestionNumber == 9) {
          //   timer.cancel();
          //   cancelTimer();
          //   isMarathonCompleted = true;
          //   await Future<dynamic>.delayed(const Duration(seconds: 2)).whenComplete(
          //     () => Navigator.pushReplacementNamed(context, AppRoutes.marathonWinnerSelection),
          //   );
          //
          //   resetValues();
          //
          //   return;
          // }
          // timer.cancel();
        } else {
          currentQuestionTime--;
        }
        notifyListeners();
      },
    );
  }

  void resetValues() {
    _currentQuestionNumber = 0;
    cardContentList.clear();
    timerU.cancel();
    _isMarathonCompleted = false;
    currentQuestionTime = 0;
    currentQuestion = QuestionModel();
    notifyListeners();
  }

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

  Future<void> getMarathonDetailsFromApi() async {
    isLoading = true;
    notifyListeners();
    await MarathonApiClient().getMarathonToken().whenComplete(() async {
      marathonDetailModel = await MarathonApiClient().getMarathonDetails();
      isLoading = false;
      notifyListeners();
    });
  }

  Future<void> connectSignalrAndJoinMarathon(BuildContext context) async {
    await MarathonApiClient().buildHubConnection(context);
  }
}
