import 'dart:math' as Math;

import 'package:betting/commom/utils/adHelper.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:flutter/widgets.dart';
import 'global_data.dart';

class GlobalController extends ValueNotifier<GlobalData> {
  FirebaseAnalytics analytics = FirebaseAnalytics.instance;

  GlobalController()
      : super(GlobalData(
          appOpenTime: DateTime.now(),
        ));

  void notify() {
    notifyListeners();
  }

  // void increaseAdmobInterAdsDisplayTimes() {
  //   value.admobInterAdsDisplayTimes += 1;
  //   notifyListeners();
  //   analytics.logEvent(
  //     name: "ads_admob_inter_show",
  //     parameters: {
  //       "placement": Utils.appLovinMAXAdMobInterID,
  //       "display_time": value.admobInterAdsDisplayTimes.toString(),
  //     },
  //   );
  // }

  void increaseNativeAdsDisplayTimes({String? screen}) {
    value.nativeAdsDisplayTimes += 1;
    notifyListeners();
    analytics.logEvent(
      name: "ads_native_show",
      parameters: {
        "placement": AdHelper.nativeAdUnitId,
        "display_time": value.nativeAdsDisplayTimes.toString(),
        "screen": screen ?? "",
      },
    );
  }

  // void increaseAppOpenAdsDisplayTimes() {
  //   value.appOpenAdsDisplayTimes += 1;
  //   notifyListeners();
  //   analytics.logEvent(
  //     name: "ads_app_open_show",
  //     parameters: {
  //       "placement": Utils.appLovinMAXAdMobID,
  //       "display_time": value.appOpenAdsDisplayTimes.toString(),
  //     },
  //   );
  // }
  //
  // void increaseBannerAdsDisplayTimes() {
  //   value.bannerAdsDisplayTimes += 1;
  //   notifyListeners();
  //   analytics.logEvent(
  //     name: "ads_banner_show",
  //     parameters: {
  //       "placement": "DefaultBanner",
  //       "display_time": value.bannerAdsDisplayTimes.toString(),
  //     },
  //   );
  // }

  void increaseInterAdsDisplayTimes() {
    value.interAdsDisplayTimes += 1;
    notifyListeners();
    analytics.logEvent(
      name: "ads_inter_show",
      parameters: {
        "placement": "DefaultInterstitial",
        "display_time": value.interAdsDisplayTimes.toString(),
      },
    );
  }

  void setCurrentScreenName(String screenName) {
    print("setCurrentScreenName ${screenName}");
    value.lastChangeScreen = DateTime.now();
    value.currentScreenName = screenName;
    notifyListeners();
  }

  void setLastInterstitialAdsDisplay() {
    value.lastInterstitialAdsDisplay = DateTime.now();
    notifyListeners();
  }

  void toggleGameFullscreen(bool isFullScreen) {
    if (value.isGameFullscreen != isFullScreen) {
      value.isGameFullscreen = isFullScreen;
      notifyListeners();
    }
  }

  void setScreenIndex(int screenIndex) {
    value.currentScreenIndex = screenIndex;
    notifyListeners();
  }

  void toggleRefresh(bool isRefreshing, [String? screenName]) {
    value.isRefreshing = isRefreshing;
    if (isRefreshing) {
      value.refreshScreenName = screenName;
    } else {
      value.refreshScreenName = null;
    }
    notifyListeners();
  }
}
