// ignore_for_file: use_build_context_synchronously

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hoshan/core/services/ad/cubit/on_show_add_cubit.dart';
import 'package:hoshan/core/services/api/dio_service.dart';
import 'package:hoshan/ui/screens/setting/cubit/ad_remaining_cubit.dart';
import 'package:tapsell_plus/tapsell_plus.dart';

class TapsellService {
  static const appId =
      'ameghpchlcbcibfnktqaaigmfnpogqrtlrkdblirmgmipjfdjtdhppfhiipdieicortsrg';
  static const rewardedVideoZone = '67ff817a63943b49f700b6a9';
  static const standardZone = '67ff819f63943b49f700b6aa';
  // static const interstitialZone = '5cfaa942e8d17f0001ffb292';

  // static const zoneIds = {
  //   "Tapsell": {
  //     "REWARDED": '5cfaa802e8d17f0001ffb28e',
  //     "INTERSTITIAL": '5cfaa942e8d17f0001ffb292',
  //     "NATIVE": '5cfaa9deaede570001d5553a',
  //     "STANDARD": '5cfaaa30e8d17f0001ffb294',
  //   },
  //   "Google AdMob": {
  //     "REWARDED": '5cfaa8aee8d17f0001ffb28f',
  //     "INTERSTITIAL": '5cfaa9b0e8d17f0001ffb293',
  //     "NATIVE": '5d123c9968287d00019e1a94',
  //     "STANDARD": '5cfaaa4ae8d17f0001ffb295',
  //   },
  //   "AdColony": {
  //     "REWARDED": '5d3362766de9f600013662d5',
  //     "INTERSTITIAL": '5d336289e985d50001427acf',
  //     "STANDARD": '60bf4ef0d40d970001693745',
  //   },
  //   "AppLovin": {
  //     "REWARDED": '5d3eb48c3aef7a0001406f84',
  //     "INTERSTITIAL": '5d3eb4fa3aef7a0001406f85',
  //     "STANDARD": '5d3eb5337a9b060001892441',
  //   },
  //   "Chart boost": {
  //     "REWARDED": '5cfaa8cee8d17f0001ffb290',
  //     "INTERSTITIAL": '60c5b303d756bf0001891f1c'
  //   },
  //   "Unity Ads": {
  //     "REWARDED": '5cfaa8eae8d17f0001ffb291',
  //     "INTERSTITIAL": '608d1c1c2d8e7e0001348111',
  //     "STANDARD": '608d20a7fb661b000190bfe4',
  //   }
  // };

  static Future initialize() async {
    try {
      TapsellPlus.instance.initialize(appId);
      TapsellPlus.instance.setGDPRConsent(true);
    } catch (e) {
      if (kDebugMode) {
        print('Error initializing Tapsell: $e');
      }
    }
  }

  // static Future<String> getResponseId(String type) async {
  //   final responseId = await TapsellPlus.instance
  //       .requestRewardedVideoAd(zoneIds['Tapsell']![type]!);
  //   return responseId;
  // }

  static Future showAdRewarded(BuildContext context) async {
    final responseId =
        await TapsellPlus.instance.requestRewardedVideoAd(rewardedVideoZone);

    await TapsellPlus.instance.showRewardedVideoAd(
      responseId,
      onOpened: (map) {
        // Ad opened
      },
      onError: (map) {},
      onClosed: (map) {
        // Ad closed
      },
      onRewarded: (map) async {
        try {
          await DioService().sendRequest().post('/advertisement/claim',
              data: {"ad_type": "rewarded_video"});
        } catch (e) {
          if (kDebugMode) {
            print('Error claiming ad: $e');
          }
        }
        context.read<AdRemainingCubit>().getRemainingAd();
      },
    );
  }

  Future showAdInterstitial() async {
    final responseId =
        await TapsellPlus.instance.requestRewardedVideoAd(rewardedVideoZone);
    TapsellPlus.instance.showInterstitialAd(responseId, onOpened: (map) {
      // Ad opened
    }, onError: (map) {
      // Ad had error - map contains `error_message`
    }, onClosed: (map) {
      // Ad closed
    });
  }

  static Future showAdBanner(BuildContext context) async {
    // Banner ads are disabled
    await TapsellPlus.instance.hideStandardBanner();
    // if (context.read<OnShowAddCubit>().state is OnShowAdd) {
    //   await TapsellPlus.instance.hideStandardBanner();
    // } else {
    //   await TapsellPlus.instance.requestStandardBannerAd(
    //       standardZone, TapsellPlusBannerType.BANNER_320x50,
    //       onResponse: (map) async {
    //     await TapsellPlus.instance.showStandardBannerAd(
    //         map['response_id']!,
    //         TapsellPlusHorizontalGravity.BOTTOM,
    //         TapsellPlusVerticalGravity.CENTER);

    //     // SAVE the responseId
    //   }, onError: (map) {
    //     // Error when requesting for an ad
    //   });
    // }

    // context.read<OnShowAddCubit>().toggleAdd();
  }
}
