using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
#if ANDROID_CN
using AnyThinkAds.Api;
#endif
using Touka.GameLogic;
#if ANDROID_CN
using UnityEngine;
#endif

public partial class TGAndroidAdManager : MonoBehaviour
{

    private static TGAndroidAdManager s_instance;
    public static TGAndroidAdManager Instance
    {
        get
        {
            if (s_instance == null)
            {
                GameObject TGGameObject = new GameObject
                {
                    name = "AdObject"
                };
                s_instance = TGGameObject.AddComponent<TGAndroidAdManager>();
                DontDestroyOnLoad(TGGameObject);
            }
            return s_instance;
        }
    }
    private string isReward = "False";

#if ANDROID_CN
    private ATCallbackListener rvListener = null;
    private InterstitalCallback ivListener = null;
    private BannerCallback bannerCallback = null;

#endif

#if UNITY_ANDROID && !UNITY_EDITOR && ANDROID_TW

    private AndroidJavaClass androidTWSDKManager = new AndroidJavaClass("com.touka.tiwai.TWSDKManager");
    private static AndroidJavaClass javaClassUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    private AndroidJavaObject currentActivity = javaClassUnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

#endif

    public void InitSDK(string appID, string appKey)
    {


#if UNITY_ANDROID
#if NO_SDK
        
#elif ANDROID_CN //中国地区
        ToukaAndroidCallbackInit();

        ATSDKAPI.initSDK(appID, appKey);

#elif ANDROID_GP

        MaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;
        MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailEvent;
        MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += InterstitialFailedToDisplayEvent;
        MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialDismissedEvent;
        MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayedEvent;

        // Attach callback
        MaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;
        MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailEvent;
        MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;
        MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent;
        MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;
        MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdDismissedEvent;
        MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;

        MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerLoadedEvent;
        MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerLoadFailEvent;

        MaxSdk.SetSdkKey("QVt-ZBOR44Frm2Ub8gEfbPo-K8SeKHvyzoq-TJ3op08cFx29THV9DviWinxrQBxNePGKU14gHOkClTrNOi_1a9");


        MaxSdk.InitializeSdk();

#elif ANDROID_TW && !UNITY_EDITOR

#endif
#endif
        
    }




#if UNITY_ANDROID
    #region banner
    public void LoadBannerWithUnitId(string unitId)
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区

        
#elif ANDROID_GP

#endif
    }

    public bool IsReadyBannerWithUnitId(string unitId)
    {
#if NO_SDK
        return false;
#elif ANDROID_CN //中国地区
        return false ;
#elif ANDROID_GP //海外地区
        return false;
#else
        return false;
#endif
    }



    public void ShowBannerWithUnitId(string unitId)
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区

#elif ANDROID_GP //海外地区

#endif
    }

    public void HideBanner()
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区

#elif ANDROID_GP //海外地区

#endif
    }

    public void RemoveBanner()
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区

#elif ANDROID_GP //海外地区

#endif
    }
    #endregion

    #region interstitial
    public void Loadinterstitial(string unitId)
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区 
        Dictionary<string, object> jsonmap = new Dictionary<string, object>();
        jsonmap.Add(AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL, AnyThinkAds.Api.ATConst.USE_REWARDED_VIDEO_AS_INTERSTITIAL_NO);
        ATInterstitialAd.Instance.loadInterstitialAd(unitId, jsonmap);

#elif ANDROID_GP //海外地区
        MaxSdk.LoadInterstitial(unitId);
#elif ANDROID_TW && !UNITY_EDITOR
        androidTWSDKManager.CallStatic("preLoadInteraction", currentActivity);
#endif
    }

    public bool IsReadyinterstitial(string unitId)
    {
#if NO_SDK
        return false;
#elif ANDROID_CN //中国地区

        return ATInterstitialAd.Instance.hasInterstitialAdReady(unitId);

#elif ANDROID_GP //海外地区
        return MaxSdk.IsInterstitialReady(unitId);
#elif ANDROID_TW && !UNITY_EDITOR

         return androidTWSDKManager.CallStatic<bool>("interactionCached");
#else
        return false;
#endif
    }

    public void Showinterstitial(string unitId)
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区
       ATInterstitialAd.Instance.showInterstitialAd(unitId);
#elif ANDROID_GP //海外地区
        MaxSdk.ShowInterstitial(unitId);
#elif ANDROID_TW && !UNITY_EDITOR
        androidTWSDKManager.CallStatic("showInteractionVideoAd", currentActivity);
#endif
    }
    #endregion


    #region native
    public void LoadNative(string unitId, float width, float height)
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区

#elif ANDROID_GP //海外地区
#elif !UNITY_EDITOR && ANDROID_TW

        androidTWSDKManager.CallStatic("preloadNativeAd",currentActivity, width, height);

#endif
    }

    public bool IsReadNative(string unitId)
    {
#if NO_SDK
        return false;
#elif ANDROID_CN //中国地区
        return false;
#elif ANDROID_GP //海外地区
        return false;
#elif !UNITY_EDITOR && ANDROID_TW
        return false;
#else
        return false;
#endif
    }

    public void ShowNative(float x, float y, float width, float height, string unitId)
    {

#if NO_SDK
        
#elif ANDROID_CN //中国地区

#elif ANDROID_GP //海外地区

#elif !UNITY_EDITOR && ANDROID_TW
#endif
    }

    public void RemoveNative()
    {
#if NO_SDK
        
#elif ANDROID_CN //中国地区

#elif ANDROID_GP //海外地区

#endif

    }
    #endregion

    #region rewardVideo
    public void LoadVideo(string unitId)
    {
#if NO_SDK

#elif ANDROID_CN //中国地区
        Dictionary<string, string> jsonmap = new Dictionary<string, string>();
        ATRewardedVideo.Instance.loadVideoAd(unitId, jsonmap);
#elif ANDROID_GP //海外地区
        MaxSdk.LoadRewardedAd(unitId);
#elif !UNITY_EDITOR && ANDROID_TW
        androidTWSDKManager.CallStatic("preloadRewardAd",currentActivity);
#endif
    }

    public bool IsReadyVideo(string unitId)
    {
#if NO_SDK

        return false;

#elif ANDROID_CN //中国地区

        return ATRewardedVideo.Instance.hasAdReady(unitId); ;

#elif ANDROID_GP //海外地区
        return MaxSdk.IsRewardedAdReady(unitId);
#elif !UNITY_EDITOR && ANDROID_TW
        return androidTWSDKManager.CallStatic<bool>("isRewardVideoCached");
#else
        return false;
#endif
    }

    public void ShowVideo(string unitId)
    {
#if NO_SDK

#elif ANDROID_CN //中国地区

        ATRewardedVideo.Instance.showAd(unitId);

#elif ANDROID_GP //海外地区
        MaxSdk.ShowRewardedAd(unitId);
#elif ANDROID_TW && !UNITY_EDITOR
        androidTWSDKManager.CallStatic("showRewardVideo",currentActivity);
#endif
    }
    #endregion
#endif


#if ANDROID_TW
#region interstitialCallback

    public void TGInterstitialAdLoad(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onAdLoaded?.Invoke();
    }

    public void TGInterstitialAdLoadFail(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onAdFailedToLoad?.Invoke();
    }

    public void TGInterstitialAdShow(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onShowSuccess?.Invoke();
    }

    public void TGInterstitialAdsShowFail(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onShowFailed?.Invoke();
    }

    public void TGInterstitialAdClose(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onAdClosed?.Invoke();
    }

    public void TGInterstitialAdClick(string obj)
    {

        TGUniversalFunc.onSetInterstitialListener_onClick?.Invoke();
    }

    public void TGInterstitialAdsEndPlaying(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onEndPlaying?.Invoke();
    }

    public void TGInterstitialAdFailedToPlay(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onFailedToPlay?.Invoke();
    }

    public void TGInterstitialAdsStartPlayVideo(string obj)
    {
        TGUniversalFunc.onSetInterstitialListener_onStartPlayVideo?.Invoke();
    }

#endregion

#region videoCallback
    public void TGVideoAdLoaded(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdLoaded?.Invoke();
    }

    public void TGVideoAdLoadFail(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdFailedToLoad?.Invoke();
    }

    public void TGVideoAdPlayStart(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoStarted?.Invoke();
    }

    public void TGVideoAdPlayEnd(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdEnd?.Invoke();
    }

    public void TGVideoAdPlayFail(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdPlayFail?.Invoke();
    }

    public void TGVideoAdPlayClosed(string isRewardStr)
    {
        Debug.Log("Unity 激励视频 TGVideoAdPlayClosed " + isRewardStr);

        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdClosed?.Invoke(isRewardStr);
    }

    public void TGVideoAdPlayClicked(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoClickAd?.Invoke();
    }

    public void TGVideoAdDidRewardedSuccess(string obj)
    {
        TGUniversalFunc.onSetRewardListener_onRewarded?.Invoke();

    }
#endregion


#region bannerCallback
    public void TGBannerAdLoad(string obj)
    {
        TGUniversalFunc.onSetBannerListener_onAdLoaded?.Invoke();
    }

    public void TGBannerAdLoadFail(string obj)
    {
        TGUniversalFunc.onSetBannerListener_onAdFailedToLoad?.Invoke();
    }

    public void TGBannerAdDidShow(string obj)
    {
        TGUniversalFunc.onSetBannerListener_onShowSuccess?.Invoke();
    }

    public void TGBannerAdDidClick(string obj)
    {
        TGUniversalFunc.onSetBannerListener_onClick?.Invoke();
    }

    public void TGBannerAdDidClickCloseButton(string obj)
    {
        TGUniversalFunc.onSetBannerListener_onAdClosed?.Invoke();
    }
#endregion

#region nativeCallback
    public void TGNativeAdLoadFail(string obj)
    {
        TGUniversalFunc.onSetNativeListener_onAdFailedToLoad?.Invoke();
    }

    public void TGNativeAdLoaded(string obj)
    {
        TGUniversalFunc.onSetNativeListener_onAdLoaded?.Invoke();
    }

    public void TGNativeAdDidShow(string obj)
    {
        TGUniversalFunc.onSetNativeListener_onShowSuccess?.Invoke();
    }

    public void TGNativeAdDidClick(string obj)
    {
        TGUniversalFunc.onSetNativeListener_onClick?.Invoke();
    }
#endregion

#region native banner

    public void TGNativeBannerAdLoad(string obj)
    {
        TGUniversalFunc.onSetNativeBannerListener_onAdLoaded?.Invoke();
    }

    public void TGNativeBannerAdLoadFail(string obj)
    {
        TGUniversalFunc.onSetNativeBannerListener_onAdLoadFail?.Invoke();
    }


#endregion
#endif


#if ANDROID_CN

    private void ToukaAndroidCallbackInit()
    {
        if (bannerCallback == null)
        {
            bannerCallback = new BannerCallback();
            bannerCallback.adClient = this;
            ATBannerAd.Instance.setListener(bannerCallback);
        }

        if (ivListener == null)
        {
            ivListener = new InterstitalCallback();
            ivListener.adClient = this;
            ATInterstitialAd.Instance.setListener(ivListener);
        }

        if (rvListener == null)
        {
            ATCallbackListener rvListener = new ATCallbackListener();
            rvListener.adclient = this;
            ATRewardedVideo.Instance.setListener(rvListener);
        }
    }


    class BannerCallback : ATBannerAdListener
    {
        public TGAndroidAdManager adClient; 
        //广告自动刷新成功
        public void onAdAutoRefresh(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onAdAutoRefresh :" + placementId);
        }
        //广告自动刷新失败
        public void onAdAutoRefreshFail(string placementId, string code, string message)
        {
            Debug.Log("Developer callback onAdAutoRefreshFail : " + placementId + "--code:" + code + "--msg:" + message);
        }
        //广告被点击
        public void onAdClick(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onAdClick :" + placementId);
        }

        //v5.5.3之后不再执行该回调，转到onAdCloseButtonTapped方法回调
        public void onAdClose(string placementId)
        {
            Debug.Log("Developer callback onAdClose :" + placementId);
        }
        //广告展示成功
        public void onAdImpress(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onAdImpress :" + placementId);
        }
        //广告加载成功
        public void onAdLoad(string placementId)
        {
            Debug.Log("Developer callback onAdLoad :" + placementId);
            TGUniversalFunc.onSetBannerListener_onAdLoaded?.Invoke();
        }
        //广告加载失败
        public void onAdLoadFail(string placementId, string code, string message)
        {
            Debug.Log("Developer callback onAdLoadFail : : " + placementId + "--code:" + code + "--msg:" + message);
            TGUniversalFunc.onSetBannerListener_onAdFailedToLoad?.Invoke();
        }
        //广告关闭按钮被点击
        public void onAdCloseButtonTapped(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onAdCloseButtonTapped :" + placementId);
        }
    }


    class InterstitalCallback : ATInterstitialAdListener
    {
        public TGAndroidAdManager adClient;
        //广告被点击
        public void onInterstitialAdClick(string placementId, ATCallbackInfo callbackInfo)
        {

            Debug.Log("Developer callback onInterstitialAdClick :" + placementId);
        }
        //广告被关闭
        public void onInterstitialAdClose(string placementId, ATCallbackInfo callbackInfo)
        {
            TGUniversalFunc.onSetInterstitialListener_onAdClosed?.Invoke();
            Debug.Log("Developer callback onInterstitialAdClose :" + placementId);
        }
        //广告视频播放结束，部分广告平台有此回调
        public void onInterstitialAdEndPlayingVideo(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onInterstitialAdEndPlayingVideo :" + placementId);
        }
        //广告视频播放失败，部分广告平台有此回调
        public void onInterstitialAdFailedToPlayVideo(string placementId, string code, string message)
        {
            Debug.Log("Developer callback onInterstitialAdFailedToPlayVideo :" + placementId + "--code:" + code + "--msg:" + message);
        }
        //广告加载成功
        public void onInterstitialAdLoad(string placementId)
        {
            Debug.Log("Developer callback onInterstitialAdLoad :" + placementId);
            TGUniversalFunc.onSetInterstitialListener_onAdLoaded?.Invoke();

        }
        //广告加载失败
        public void onInterstitialAdLoadFail(string placementId, string code, string message)
        {
            Debug.Log("Developer callback onInterstitialAdLoadFail :" + placementId + "--code:" + code + "--msg:" + message);
            TGUniversalFunc.onSetInterstitialListener_onAdLoaded?.Invoke();
        }
        //广告展示成功
        public void onInterstitialAdShow(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onInterstitialAdShow :" + placementId);
            TGUniversalFunc.onSetInterstitialListener_onShowSuccess?.Invoke();
        }
        //广告视频开始播放，部分平台有此回调
        public void onInterstitialAdStartPlayingVideo(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer callback onInterstitialAdStartPlayingVideo :" + placementId);
        }
        //广告展示失败
        public void onInterstitialAdFailedToShow(string placementId)
        {
            Debug.Log("Developer callback onInterstitialAdFailedToShow :" + placementId);
            TGUniversalFunc.onSetInterstitialListener_onShowFailed?.Invoke();
        }
    }

    class ATCallbackListener : ATRewardedVideoListener
    {
        public TGAndroidAdManager adclient;
        //广告加载成功
        public void onRewardedVideoAdLoaded(string placementId)
        {
            Debug.Log("Developer onRewardedVideoAdLoaded------");
            TGUniversalFunc.onSetRewardListener_onRewardedVideoAdLoaded?.Invoke();

        }
        //广告加载失败
        public void onRewardedVideoAdLoadFail(string placementId, string code, string message)
        {
            Debug.Log("Developer onRewardedVideoAdLoadFail------:code" + code + "--message:" + message);
            TGUniversalFunc.onSetRewardListener_onRewardedVideoAdFailedToLoad?.Invoke();

        }
        //广告开始播放
        public void onRewardedVideoAdPlayStart(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer onRewardedVideoAdPlayStart------");
            TGUniversalFunc.onSetRewardListener_onRewardedVideoStarted?.Invoke();

        }
        //广告播放结束
        public void onRewardedVideoAdPlayEnd(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer onRewardedVideoAdPlayEnd------");
        }
        //广告播放失败
        public void onRewardedVideoAdPlayFail(string placementId, string code, string message)
        {
            Debug.Log("Developer onRewardedVideoAdPlayFail------code:" + code + "---message:" + message);
            TGUniversalFunc.onSetRewardListener_onRewardedVideoAdPlayFail?.Invoke();
        }
        //广告被关闭，其中isReward仅表示onRewardedVideoAdPlayClosed被回调时onReward()方法被回调了没有
        public void onRewardedVideoAdPlayClosed(string placementId, bool isReward, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer onRewardedVideoAdPlayClosed------isReward:" + isReward);
            TGUniversalFunc.onSetRewardListener_onRewardedVideoAdClosed?.Invoke(isReward == true ? "True" : "False");

        }
        //广告被点击
        public void onRewardedVideoAdPlayClicked(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer onRewardVideoAdPlayClicked------");
        }
        //激励成功，开发者可在此回调中下发奖励，一般先于onRewardedVideoAdPlayClosed回调，服务器激励则不一定
        public void onReward(string placementId, ATCallbackInfo callbackInfo)
        {
            Debug.Log("Developer onReward------");
        }
    }
#else
#endif

#if ANDROID_GP

    //iv
    private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetInterstitialListener_onAdLoaded?.Invoke();
    }

    private void OnInterstitialLoadFailEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
    {
        TGUniversalFunc.onSetInterstitialListener_onAdFailedToLoad?.Invoke();
    }

    private void OnInterstitialDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetInterstitialListener_onShowSuccess?.Invoke();
    }

    private void InterstitialFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetInterstitialListener_onShowFailed?.Invoke();
        TGUniversalFunc.onSetInterstitialListener_onFailedToPlay?.Invoke();
    }

    private void OnInterstitialDismissedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {

        TGUniversalFunc.onSetInterstitialListener_onAdClosed?.Invoke();
    }

    // video
    private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdLoaded?.Invoke();
    }

    private void OnRewardedAdLoadFailEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdFailedToLoad?.Invoke();
    }

    private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adinfo)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdPlayFail?.Invoke();
    }

    private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoStarted?.Invoke();
    }

    private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoClickAd?.Invoke();
    }

    private void OnRewardedAdDismissedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdClosed?.Invoke(isReward);
        isReward = "False";
    }

    private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo)
    {
        isReward = "True";
    }

    private void OnBannerLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo)
    {
        TGUniversalFunc.onSetBannerListener_onAdLoaded?.Invoke();
    }

    private void OnBannerLoadFailEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo)
    {
        TGUniversalFunc.onSetBannerListener_onAdFailedToLoad?.Invoke();
    }
    

#endif

}
