import 'package:betting/commom/utils/native_ad_manager.dart';
import 'package:betting/data/base_service/api_response.dart';
import 'package:betting/data/model/res/infor_team/infor_team.dart';
import 'package:betting/data/model/res/matches/matches.dart';
import 'package:betting/data/repository/matches/matches_repo.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/detail_matches_regions_controller.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/h2h_matches_regions/h2h_matches_regions_controller.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/stats_matches_regions/stats_matches_regions_controller.dart';
import 'package:betting/view/mobile/find/detail_matches_in_regions/summary_matches_regions/summary_matches_regions_controller.dart';
import 'package:get/get.dart';
import 'package:intl/intl.dart';

class InforMatchesRegionsController extends GetxController {
  var itemMatches = Matches().obs;
  MatchesRepo matchesRepo = MatchesRepo();
  var infoTeam = InforTeam().obs;
  var h2hTeam = H2h().obs;
  RxList formTeam1 = [].obs;
  RxList formTeam2 = [].obs;
  RxList h2hFormTeam1 = <String>[].obs;
  RxList h2hFormTeam2 = <String>[].obs;
  var outputDateToDateFormat = DateFormat('dd/MM/yyyy');
  var outputTimeFormat = DateFormat('HH:mm');
  var isReady = false.obs;
  var nativeAds = [].obs;
  NativeAdManager nativeAd = NativeAdManager() ;

  @override
  void onInit() {
    for (int i = 0; i < 3; i++) {
      nativeAds.add(NativeAdManager());
    }
    for (var nativeAd in nativeAds) {
      nativeAd.loadAd();
    }

    itemMatches.value = Get.arguments;

    getInfoMatches(itemMatches.value.sId!);
    super.onInit();
  }

  getInfoMatches(String matchesId) async {
    await matchesRepo.inforMatches(matchesId).then(
      (value) {
        if (value.state == Status.SUCCESS) {
          infoTeam.value = value.object!;

          if (infoTeam.value.h2h != null) {
            h2hTeam.value = infoTeam.value.h2h!;

          }
          if (h2hTeam.value.team1Last6!.length > 5) {
            formTeam1.value = h2hTeam.value.team1Last6!.take(5).toList();
          } else {
            formTeam1.value = h2hTeam.value.team1Last6!;
          }

          if (h2hTeam.value.team2Last6!.length > 5) {
            formTeam2.value = h2hTeam.value.team2Last6!.take(5).toList();
          } else {
            formTeam2.value = h2hTeam.value.team2Last6!;
          }


          if(h2hTeam.value.team1Last6!.isNotEmpty){
            formTeam1.add(Team1Last6(ftScore: 'VIEW ALL'));

          }
          if(h2hTeam.value.team2Last6!.isNotEmpty){
            formTeam2.add(Team1Last6(ftScore: 'VIEW ALL'));

          }
          h2hFormTeam1.value = h2hTeam.value.h2hFormTeam1!;
          h2hFormTeam2.value = h2hTeam.value.h2hFormTeam2!;




        }
      },
    );
    isReady.value = true;
    update();
  }

  getUpdateTeam1(index){
    Get.find<DetailMatchesRegionsController>().itemArgument.value.ftScore = h2hTeam.value.team1Last6![index].ftScore!;
    Get.find<DetailMatchesRegionsController>().itemArgument.value.status = h2hTeam.value.team1Last6![index].status!;
    Get.find<DetailMatchesRegionsController>().itemArgument.value.home?.name =h2hTeam.value.team1Last6![index].home?.name??'';
    Get.find<DetailMatchesRegionsController>().itemArgument.value.away?.name = h2hTeam.value.team1Last6![index].away?.name??'';
    Get.find<SummaryMatchesRegionsController>().getMatchEvent('${h2hTeam.value.team1Last6![index].sId}');
    Get.find<SummaryMatchesRegionsController>().getStats('${h2hTeam.value.team1Last6![index].sId}');
    Get.find<StatsMatchesRegionsController>().getStatsMatches('${h2hTeam.value.team1Last6![index].sId}');

    Get.find<H2hMatchesRegionsController>().getInforMatchesH2h('${h2hTeam.value.team1Last6![index].sId}');
    Get.find<H2hMatchesRegionsController>().itemMatches.value.homeName = h2hTeam.value.team1Last6![index].home?.name??'';
    Get.find<H2hMatchesRegionsController>().itemMatches.value.awayName = h2hTeam.value.team1Last6![index].away?.name??'';

    Get.find<H2hMatchesRegionsController>().itemMatches.value.home?.sId = h2hTeam.value.team1Last6![index].home?.sId??'';
    Get.find<H2hMatchesRegionsController>().itemMatches.value.away?.sId = h2hTeam.value.team1Last6![index].away?.sId??'';


    Get.find<DetailMatchesRegionsController>().itemArgument.refresh();

    Get.find<H2hMatchesRegionsController>().update();
    Get.find<StatsMatchesRegionsController>().update();
    Get.find<SummaryMatchesRegionsController>().update();
  }

  getUpdateTeam2(index){
    Get.find<DetailMatchesRegionsController>().itemArgument.value.ftScore = h2hTeam.value.team2Last6![index].ftScore!;
    Get.find<DetailMatchesRegionsController>().itemArgument.value.status = h2hTeam.value.team2Last6![index].status!;
    Get.find<DetailMatchesRegionsController>().itemArgument.value.home?.name =h2hTeam.value.team2Last6![index].home?.name??'';
    Get.find<DetailMatchesRegionsController>().itemArgument.value.away?.name = h2hTeam.value.team2Last6![index].away?.name??'';
    Get.find<SummaryMatchesRegionsController>().getMatchEvent('${h2hTeam.value.team2Last6![index].sId}');
    Get.find<SummaryMatchesRegionsController>().getStats('${h2hTeam.value.team2Last6![index].sId}');
    Get.find<StatsMatchesRegionsController>().getStatsMatches('${h2hTeam.value.team2Last6![index].sId}');

    Get.find<H2hMatchesRegionsController>().getInforMatchesH2h('${h2hTeam.value.team2Last6![index].sId}');
    Get.find<H2hMatchesRegionsController>().itemMatches.value.homeName = h2hTeam.value.team2Last6![index].home?.name??'';
    Get.find<H2hMatchesRegionsController>().itemMatches.value.awayName = h2hTeam.value.team2Last6![index].away?.name??'';

    Get.find<H2hMatchesRegionsController>().itemMatches.value.home?.sId = h2hTeam.value.team2Last6![index].home?.sId??'';
    Get.find<H2hMatchesRegionsController>().itemMatches.value.away?.sId = h2hTeam.value.team2Last6![index].away?.sId??'';


    Get.find<DetailMatchesRegionsController>().itemArgument.refresh();

    Get.find<H2hMatchesRegionsController>().update();
    Get.find<StatsMatchesRegionsController>().update();
    Get.find<SummaryMatchesRegionsController>().update();
  }

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