using AppodealAds.Unity.Api;
using AppodealAds.Unity.Common;
using System;
using System.Collections;
using System.Collections.Generic;
//using GoogleMobileAds.Api;
using UnityEngine;

public class AdsManager : MonoBehaviour, IAppodealInitializationListener, IInterstitialAdListener, IRewardedVideoAdListener
{
    // Start is called before the first frame update

    public static AdsManager instance;
    void Awake(){
        if(instance!= null){Destroy(gameObject);}
        instance = this;
    }

    const string intAdId = "ca-app-pub-3966734202864173/1197808629";
    const string rewardedAdId = "ca-app-pub-3966734202864173/3725853326";
    void Start()
    {
        DontDestroyOnLoad(gameObject);
        // Initialize the Google Mobile Ads SDK.
        //  MobileAds.Initialize(initStatus => { Debug.Log("admob Init status : " + initStatus.ToString());});
        int adTypes = Appodeal.INTERSTITIAL | Appodeal.REWARDED_VIDEO;
        string appKey = "010153e7ddbb96a5639a058bce268953b4d74b1038e266d1";

        Appodeal.initialize(appKey, adTypes,this); 
       
        Appodeal.setAutoCache(Appodeal.INTERSTITIAL, true);
        Appodeal.setAutoCache(Appodeal.REWARDED_VIDEO, true);
    }

    public void onInitializationFinished(List<string> errors) { }
    //private InterstitialAd interstitialAd;

    public void ShowInterstitial()
    {
        if (Appodeal.isLoaded(Appodeal.INTERSTITIAL) && Appodeal.canShow(Appodeal.INTERSTITIAL, "interestitial"))
        {
            Appodeal.show(Appodeal.INTERSTITIAL, "interestitial");
        }else if (!Appodeal.isAutoCacheEnabled(Appodeal.INTERSTITIAL))
        {
            Appodeal.cache(Appodeal.INTERSTITIAL);
            Debug.Log("Caching INT");
        }
    }

    public void ShowRewardedVideo()
    {
        if (Appodeal.isLoaded(Appodeal.REWARDED_VIDEO) && Appodeal.canShow(Appodeal.REWARDED_VIDEO, "Reward"))
        {
            Appodeal.show(Appodeal.REWARDED_VIDEO, "Reward");
        }
        else if (!Appodeal.isAutoCacheEnabled(Appodeal.REWARDED_VIDEO))
        {
            Appodeal.cache(Appodeal.REWARDED_VIDEO);
            Debug.Log("Caching REWARD");
        }
    }
    #region Interstitial callback handlers

    // Called when interstitial was loaded (precache flag shows if the loaded ad is precache)
    public void onInterstitialLoaded(bool isPrecache)
    {
        Debug.Log("Interstitial loaded");
    }

    // Called when interstitial failed to load
    public void onInterstitialFailedToLoad()
    {
        Debug.Log("Interstitial failed to load");
    }

    // Called when interstitial was loaded, but cannot be shown (internal network errors, placement settings, etc.)
    public void onInterstitialShowFailed()
    {
        Debug.Log("Interstitial show failed");
    }

    // Called when interstitial is shown
    public void onInterstitialShown()
    {
        Debug.Log("Interstitial shown");
    }

    // Called when interstitial is closed
    public void onInterstitialClosed()
    {
        Debug.Log("Interstitial closed");
    }

    // Called when interstitial is clicked
    public void onInterstitialClicked()
    {
        Debug.Log("Interstitial clicked");
    }

    // Called when interstitial is expired and can not be shown
    public void onInterstitialExpired()
    {
        Debug.Log("Interstitial expired");
    }

    #endregion
    #region Rewarded Video callback handlers

    //Called when rewarded video was loaded (precache flag shows if the loaded ad is precache).
    public void onRewardedVideoLoaded(bool isPrecache)
    {
        Debug.Log("RewardedVideo loaded");
    }

    // Called when rewarded video failed to load
    public void onRewardedVideoFailedToLoad()
    {
        Debug.Log("RewardedVideo failed to load");
    }

    // Called when rewarded video was loaded, but cannot be shown (internal network errors, placement settings, etc.)
    public void onRewardedVideoShowFailed()
    {
        Debug.Log("RewardedVideo show failed");
    }

    // Called when rewarded video is shown
    public void onRewardedVideoShown()
    {
        Debug.Log("RewardedVideo shown");
    }

    // Called when reward video is clicked
    public void onRewardedVideoClicked()
    {
        Debug.Log("RewardedVideo clicked");
    }

    // Called when rewarded video is closed
    public void onRewardedVideoClosed(bool finished)
    {
        Debug.Log("RewardedVideo closed");
    }

    // Called when rewarded video is viewed until the end
    public void onRewardedVideoFinished(double amount, string name)
    {
        Debug.Log("RewardedVideo finished");

        GameManager.AdWatched();
    }

    //Called when rewarded video is expired and can not be shown
    public void onRewardedVideoExpired()
    {
        Debug.Log("RewardedVideo expired");
    }

    #endregion
}
