import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/model/res/competitions/competitions.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/data/model/res/schedule/schedule_model.dart';
import 'package:betting/data/repository/competitions/competitions_repo.dart';
import 'package:betting/data/repository/matches/matches_repo.dart';
import 'package:get/get.dart';

class MatchesFootballController extends GetxController {
  final _competitionsRepo = CompetitionsRepo();
  final _matchRepo = MatchesRepo();
  var stage = "All Stages".obs;
  RxList schedules = <Schedule>[].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;
    }
    
    getListScheduleByCompetitionId(competitionId);
  }

  /// todo: Method get list schedule
  void getListScheduleByCompetitionId(String competitionId) async{
    await _competitionsRepo.getListSchedule(competitionId).then((value) {
      if(value.state == Status.SUCCESS) {
        schedules.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) {
        print('Thêm mới thành công');
        schedules[index].matches.where((match) => match.sId == idMatches).first.favourite = true;
        schedules.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) {
        schedules[index].matches.where((match) => match.sId == idMatchesFavourites).first.favourite = false;
        schedules.refresh();
      }
    });
  }
}



