const { game } = cc;

import { InterstitialCallback } from "./interstitial_callback";
import { RewardedCallback } from "./rewarded_callback";
import { PrerollCallback } from "./preroll_callback";
import {
  InterstitialAdEvent,
  PrerollAdEvent,
  RewardedVideoAdEvent,
} from "./ad_event";
import { InterstitialType } from "./interstitial_type";
import SkinController from "../Scripts/Controllers/SkinsController";
import GamePlay from "../Scripts/Controllers/GamePlay";

// This is needed since there're no definition for AdSense object here.
// tslint:disable-next-line:no-any
const ADS_BY_GOOGLE = (window as any).adsbygoogle;
type AdBreakCallback =
  | InterstitialCallback
  | RewardedCallback
  | PrerollCallback;

const adBreak = (o: AdBreakCallback) => {
  ADS_BY_GOOGLE.push(o);
};

//let showRewardedAdFn : (() => void) | null;

/**
 * API to show Preroll Ad when available.
 * If there are no ad available this function will request an ad.
 */
// export function  showPrerollAd(){
//     try {
//         const game_instance = game;
//         adBreak({
//             type: 'preroll',
//             adBreakDone: (placementInfo: any) => {
//                 game_instance.emit(PrerollAdEvent.AD_BREAK_DONE);
//             },
//         });
//         console.log(ADS_BY_GOOGLE)
//     } catch(e) {
//         console.log(e)
//     }
// }

/**
 * API to show Interstitial Ad when available.
 * If there are no ad available this function will request an ad.
 */
export function showInterstitialAd(type: InterstitialType, name: string) {
  try {
    console.log("try to show InterstitialAd");
    const gameInstance = game;
    adBreak({
      type,
      name,
      beforeAd: () => {
        console.log("beforeAd");
        gameInstance.emit(InterstitialAdEvent.BEFORE_AD);
        cc.audioEngine.pauseMusic();
        cc.audioEngine.pauseAllEffects();
      },
      afterAd: () => {
        console.log("afterAd");
        gameInstance.emit(InterstitialAdEvent.AFTER_AD);
      },
      adBreakDone: (placementInfo: any) => {
        console.log(placementInfo);
        gameInstance.emit(InterstitialAdEvent.AD_BREAK_DONE);
        console.log("adBreakDone");
        cc.audioEngine.resumeMusic();
        cc.audioEngine.resumeAllEffects();

        if (
          placementInfo.breakName == "earn15Coin" &&
          placementInfo.breakStatus == "viewed"
        ) {
          SkinController.instance.onShowNotiAds("ok");
        }

        if (
          (placementInfo.breakName == "earn15Coin" &&
            placementInfo.breakStatus == "ignored") ||
          placementInfo.breakStatus == "other"
        ) {
          SkinController.instance.onShowNotiAds("cancel");
        }

        if (
          placementInfo.breakName == "earn15Coin" &&
          placementInfo.breakStatus == "notReady"
        ) {
          SkinController.instance.onShowNotiAds("notReady");
        }

        if (
          placementInfo.breakName == "earn15Coin" &&
          placementInfo.breakStatus == "frequencyCapped"
        ) {
          SkinController.instance.onShowNotiAds("frequencyCapped");
        }

        if (
          placementInfo.breakName == "doubleCoin" &&
          placementInfo.breakStatus == "viewed"
        ) {
          GamePlay.instance.onShowNotiAds("ok");
        }
        if (
          (placementInfo.breakName == "doubleCoin" &&
            placementInfo.breakStatus == "ignored") ||
          placementInfo.breakStatus == "other"
        ) {
          GamePlay.instance.onShowNotiAds("cancel");
        }

        if (
          placementInfo.breakName == "doubleCoin" &&
          placementInfo.breakStatus == "notReady"
        ) {
          SkinController.instance.onShowNotiAds("notReady");
        }

        if (
          placementInfo.breakName == "doubleCoin" &&
          placementInfo.breakStatus == "frequencyCapped"
        ) {
          GamePlay.instance.onShowNotiAds("frequencyCapped");
        }
      },
    });
  } catch (e) {
    console.log(e);
  }
}

/**
 * API to request Rewarded Ad.
 * To show the Rewarded Ad you could call showRewardedAd after
 * beforeReward callback.
 */
// export function requestRewardedAd(name: string) {
//   try {
//     console.log("try to show InterstitialAd");

//     const gameInstance = game;
//     adBreak({
//       type: 'reward',
//       name,
//       beforeAd: () => {
//         console.log("beforeAd");
//         gameInstance.emit(RewardedVideoAdEvent.BEFORE_AD);
//       },
//       afterAd: () => {
//         console.log("afterAd");
//         gameInstance.emit(RewardedVideoAdEvent.AFTER_AD);
//       },
//       adBreakDone: (placementInfo: unknown) => {
//         console.log("adBreakDone");
//         console.log(placementInfo)
//         gameInstance.emit(RewardedVideoAdEvent.AD_BREAK_DONE);
//       },
//       beforeReward: (showAdFn: () => void)  => {
//         showRewardedAdFn = showAdFn;
//         console.log("beforeReward");
//         gameInstance.emit(RewardedVideoAdEvent.BEFORE_REWARD);
//       },
//       adDismissed: () => {
//         console.log("adDismissed");
//         gameInstance.emit(RewardedVideoAdEvent.AD_DISMISSED);
//       },
//       adViewed: () => {
//         console.log("adViewed");
//         gameInstance.emit(RewardedVideoAdEvent.AD_VIEWED);
//       },
//     });
//   } catch (e) {
//     console.log(e);
//   }
// }

/**
 * API to show Rewarded Ad when available.
 */
// export function showRewardedAd() {
//   if (!showRewardedAdFn) {
//     console.log('No Rewarded Ad available');
//     return;
//   }
//   showRewardedAdFn();
//   showRewardedAdFn = null;
// }
