import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/model/res/competitions/competitions.dart';
import 'package:betting/data/model/res/goal_score/goal_score_model.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/data/repository/goal_scorers/goal_scorers_repo.dart';
import 'package:betting/data/repository/matches/matches_repo.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class OverviewTournamentController extends GetxController {
  final _matchRepo = MatchesRepo();
  final _topScoreRepo = GoalScorerRepo();
  var outputDateToDateFormat = DateFormat('yyyy-MM-dd');
  RxList teams = <ItemsMatches>[].obs;
  RxList topScores = <GoalScore>[].obs;
  String competitionId = '';
  RxBool isLoading = true.obs;

  @override
  void onInit() {
    super.onInit();
    if(Get.arguments.runtimeType == ItemsMatches) {
      competitionId = Get.arguments.sId;
    } else if(Get.arguments.runtimeType == ItemsCompetitions) {
      competitionId = Get.arguments.id;
    }
    var date= outputDateToDateFormat.format(DateTime.now());
    getMatchesByCompetitionId(date, competitionId);
    getListTopScore(competitionId);
  }

  /// todo: Method get list match by competition id
  void getMatchesByCompetitionId(String? date, String competitionId) async {
    await _matchRepo.getListMatches(date, competitionId,'','').then((value) {
      if(value.state == Status.SUCCESS) {
        teams.value = value.object!;
        isLoading.value = false;
      }
    });
  }

  /// todo: add match to list favourite
  addMatchesFavorites(String idMatches, int index) async {
    await _matchRepo.addMatchesFavorites(idMatches).then((value) {
      if (value.state == Status.SUCCESS) {
        teams[0].matches.where((match) => match.sId == idMatches).first.favourite = true;
        teams.refresh();
      }
    },);
  }

  /// todo: Method delete match from list favourite
  deleteMatchFavorites(String idMatchesFavourites, int index) async {
    await _matchRepo.deleteMatchesFavorites(idMatchesFavourites).then((value) {
      if (value.state == Status.SUCCESS) {
        teams[0].matches.where((match) => match.sId == idMatchesFavourites).first.favourite = false;
        teams.refresh();
      }
    });
  }

  /// todo: Method get top score by competition id
  void getListTopScore(String competitionId) async {
    await _topScoreRepo.getListTopScore(0, 20, competitionId, '', '').then((value) {
      if(value.state == Status.SUCCESS) {
        topScores.value = value.object!;
      }
    });
  }
}



