import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/data/repository/matches/matches_repo.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';
import '../../../../commom/utils/native_ad_manager.dart';

class FootballController extends GetxController {
  MatchesRepo matchesRepo = MatchesRepo();
  var listMatches = <ItemsMatches>[].obs;
  var matsChesOnCompetition = <Matches>[].obs;
  var nativeAds = [].obs;
  var outputDateToDateFormat = DateFormat('yyyy-MM-dd');
  var outputTimeFormat = DateFormat('HH:mm');
  var listIdMatchesFavourites = <String>[].obs;
  var isReady = false.obs;

  @override
  void onInit() {
    getListMatchesOnDay('',outputDateToDateFormat.format(DateTime.now()),'');
    for (int i = 0; i < 3; i++) {
      nativeAds.add(NativeAdManager());
    }

    for (var nativeAd in nativeAds) {
      nativeAd.loadAd();
    }
    super.onInit();
  }
  getListMatchesOnDay(String teamId, date, String status) async{
    await matchesRepo.getListMatches(date, '', teamId, status).then((value) {
      if(value.state == Status.SUCCESS){
        listMatches.value = value.object!;
      }
    },);
    isReady.value= true;
    update();
  }

  addMatchesFavorites(String idMatches, int index) async {
    await matchesRepo.addMatchesFavorites(idMatches).then((value) {
      if (value.state == Status.SUCCESS) {
        listMatches.refresh();
      }
    },);
  }

  deleteCompetitionsFavorites(String idMatchesFavourites) async {
    await matchesRepo.deleteMatchesFavorites(idMatchesFavourites).then((value) {
      if (value.state == Status.SUCCESS) {
        listMatches.refresh();
      }
    });
  }


  @override
  void dispose() {
    for (var nativeAd in nativeAds) {
      if (nativeAd.nativeAd != null) nativeAd.nativeAd!.dispose();
    }
    super.dispose();
  }


  getMatchTimeText(index, indexMatches){
    if(listMatches[index].matches![indexMatches].status =='FINISHED'){
      return 'FT';
    }else if(listMatches[index].matches![indexMatches].status =='ADDED TIME'){
      return 'AET';
    }else if (listMatches[index].matches![indexMatches].status =='NOT STARTED'){
      return 'AP';
    }else {
      return outputTimeFormat.format(DateFormat('HH:mm:ss').parse('${listMatches[index].matches![indexMatches].time}'));
    }
  }
}
