using System;
using System.Collections;
using GoogleMobileAds.Api;
using InnerNet;
using UnityEngine;

public static class AdPlayer
{
	private static InterstitialAd interstitial;

	private const string appId = "unexpected_platform";

	private const string adUnitId = "unexpected_platform";

	public static void ShowInterstitial(MonoBehaviour parent, bool playAgain)
	{
		parent.StartCoroutine(AdPlayer.CoShowAd(playAgain));
	}

	private static IEnumerator CoShowAd(bool playAgain)
	{
		if (playAgain)
		{
			yield return DestroyableSingleton<EndGameManager>.Instance.CoJoinGame();
		}
		else
		{
			AmongUsClient.Instance.ExitGame(DisconnectReasons.ExitGame);
		}
		yield break;
	}

	public static void RequestInterstitial()
	{
		try
		{
			MobileAds.Initialize("unexpected_platform");
			if (AdPlayer.interstitial == null)
			{
				AdPlayer.interstitial = new InterstitialAd("unexpected_platform");
				AdRequest adRequest = new AdRequest.Builder().Build();
				if (SaveManager.ShowAdsScreen.HasFlag(ShowAdsState.NonPersonalized))
				{
					adRequest.Extras.Add("npa", "1");
				}
				AdPlayer.interstitial.OnAdLoaded += AdPlayer.Interstitial_OnAdLoaded;
				AdPlayer.interstitial.OnAdFailedToLoad += AdPlayer.Interstitial_OnAdFailedToLoad;
				AdPlayer.interstitial.OnAdClosed += AdPlayer.Interstitial_OnAdClosed;
				AdPlayer.interstitial.OnAdLeavingApplication += AdPlayer.Interstitial_OnAdLeavingApplication;
				AdPlayer.interstitial.LoadAd(adRequest);
			}
		}
		catch
		{
			try
			{
				if (AdPlayer.interstitial != null)
				{
					AdPlayer.interstitial.Destroy();
				}
			}
			catch
			{
			}
			AdPlayer.interstitial = null;
		}
	}

	private static void Interstitial_OnAdLoaded(object sender, EventArgs e)
	{
	}

	private static void Interstitial_OnAdLeavingApplication(object sender, EventArgs e)
	{
		try
		{
			if (AdPlayer.interstitial != null)
			{
				AdPlayer.interstitial.Destroy();
			}
		}
		finally
		{
			AdPlayer.interstitial = null;
		}
	}

	private static void Interstitial_OnAdFailedToLoad(object sender, AdFailedToLoadEventArgs e)
	{
		try
		{
			if (AdPlayer.interstitial != null)
			{
				AdPlayer.interstitial.Destroy();
				Debug.LogError("Couldn't load ad: " + (e.Message ?? "No Message"));
			}
		}
		finally
		{
			AdPlayer.interstitial = null;
		}
	}

	private static void Interstitial_OnAdClosed(object sender, EventArgs e)
	{
		try
		{
			if (AdPlayer.interstitial != null)
			{
				AdPlayer.interstitial.Destroy();
			}
		}
		finally
		{
			AdPlayer.interstitial = null;
		}
	}
}