import 'package:betting/view/mobile/favourites_view/matches_view/list_fixtures/fixtures_screen_controller.dart';
import 'package:betting/view/mobile/favourites_view/matches_view/list_results/results_screen_controller.dart';
import 'package:betting/view/mobile/scores/football/football_controller.dart';
import 'package:betting/view/mobile/scores/scores_controller.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'package:intl/intl.dart';

import '../../../commom/utils/adHelper.dart';
import '../../../routes/app_pages.dart';
class HomeController extends GetxController with GetSingleTickerProviderStateMixin{
  RxInt currentIndex = 0.obs;
  var listTitle = ["Football","Basketball","Ice Hockey","Tennis","Crickes"].obs;
  AnimationController? animationController;
  RxInt indexSport = 0.obs;
  RxBool isRotating = false.obs;
  RxBool isNotification = false.obs;
  InterstitialAd? interstitialAd;
  RewardedAd? rewardedAd;
  var outputDateToDateFormat = DateFormat('yyyy-MM-dd');


  // TODO: Implement _loadRewardedAd()
  void _loadRewardedAd() {
    RewardedAd.load(
      adUnitId: AdHelper.rewardedAdUnitId,
      request: const AdRequest(),
      rewardedAdLoadCallback: RewardedAdLoadCallback(
        onAdLoaded: (ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
            onAdDismissedFullScreenContent: (ad) {
                ad.dispose();
                rewardedAd = null;
              _loadRewardedAd();
            },
          );

            rewardedAd = ad;
        },
        onAdFailedToLoad: (err) {
          if (kDebugMode) {
            print('Failed to load a rewarded ad: ${err.message}');
          }
        },
      ),
    );
  }



  void _loadInterstitialAd() {
    InterstitialAd.load(
      adUnitId: AdHelper.interstitialAdUnitId,
      request: const AdRequest(),
      adLoadCallback: InterstitialAdLoadCallback(
        onAdLoaded: (ad) {
          ad.fullScreenContentCallback = FullScreenContentCallback(
            onAdDismissedFullScreenContent: (ad) {
              ad.dispose();
              interstitialAd = null;
              _loadInterstitialAd();
            },
          );
            interstitialAd = ad;
        },
        onAdFailedToLoad: (err) {
          if (kDebugMode) {
            print('Failed to load an interstitial ad: ${err.message}');
          }
        },
      ),
    );
  }

  @override
  void onInit() async {
    super.onInit();
    _loadInterstitialAd();
    _loadRewardedAd();

    animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );
  }
  onTabTapped(index) {
    if(index != 4){
      currentIndex.value  = index;
      if(currentIndex.value == 0){
        WidgetsBinding.instance.addPostFrameCallback((_) {
          Get.find<ScoresController>().controllerDatePicker.jumpToSelection();
          Get.find<FootballController>().getListMatchesOnDay('',outputDateToDateFormat.format(DateTime.now()),'');
          Get.find<FootballController>().listMatches.refresh();
          Get.find<ScoresController>().getFavouriteTeams();
          Get.find<ScoresController>().favouriteTeams.refresh();

        });
      }
      if(currentIndex.value == 1){
        if(Get.isRegistered<ResultsScreenController>()) {
          Get.find<ResultsScreenController>().listMatchesFavourite.refresh();
          Get.find<ResultsScreenController>().getListMatchesOnTeamFavouriteResult();
        }
       if(Get.isRegistered<FixturesController>()){
         Get.find<FixturesController>().getListMatchesOnTeamFavourite();
         Get.find<FixturesController>().listMatChesFavourite.refresh();
       }

      }
      if(currentIndex.value == 2){
        interstitialAd?.show();
      }
      if(currentIndex.value  == 3){
        rewardedAd?.show(onUserEarnedReward: (AdWithoutView ad, RewardItem reward) {  });
      }
    }
    else{
      onRefreshPressed();
    }

    update();
  }
  @override
  void dispose() {
    animationController?.dispose();
    interstitialAd?.dispose();
    rewardedAd?.dispose();
    super.dispose();
  }

  void onRefreshPressed() {
      isRotating.value  = true;
      animationController?.repeat();
    // Do some refreshing work...
    Future.delayed(const Duration(seconds: 2), () {
      animationController?.stop();
        isRotating.value  = false;
    });
  }


  getTitleAppBar(){
    switch(currentIndex.value ){
      case 0 :
        return "Betting";
      case 1 :
        return "Favourites";
      case 2 :
        return "Watch";
      case 3 :
        return "News";
    }
  }


}



