using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using System;

public class TGiOSAdManager : MonoBehaviour
{

    private static TGiOSAdManager s_instance;
    public Action onAntiSuccessHander;
    public static TGiOSAdManager Instance
    {
        get
        {
            if (s_instance == null)
            {
                GameObject TGGameObject = new GameObject
                {
                    name = "AdObject"
                };
                s_instance = TGGameObject.AddComponent<TGiOSAdManager>();
                DontDestroyOnLoad(TGGameObject);
            }
            return s_instance;
        }
    }

#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
    [DllImport("__Internal")]
    private static extern void initSDK(string appID, string appKey);
    [DllImport("__Internal")]
    private static extern void loadNativeBannerWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern bool isReadyNativeBannerWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void showNativeBannerWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void setNativeBannerAlign(string align);
    [DllImport("__Internal")]
    private static extern void hideNativeBanner();
    [DllImport("__Internal")]
    private static extern void removeNativeBanner();
    [DllImport("__Internal")]
    private static extern void loadBannerWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern bool isReadyBannerWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void showBannerWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void setBannerAlign(string align);
    [DllImport("__Internal")]
    private static extern void hideBanner();
    [DllImport("__Internal")]
    private static extern void removeBanner();
    [DllImport("__Internal")]
    private static extern void loadInterstitialWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern bool isReadyInterstitialWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void showInterstitialWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void loadNativeWithUnitId(string unitId, float width, float height);
    [DllImport("__Internal")]
    private static extern bool isReadyNativeWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void layoutNativeWithFrame(float x, float y, float width, float height);
    [DllImport("__Internal")]
    private static extern void showNativeWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void removeNative();
    [DllImport("__Internal")]
    private static extern void loadRewardVideoWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern bool isReadyRewardVideoWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void showRewardVideoWithUnitId(string unitId);
    [DllImport("__Internal")]
    private static extern void setLogEnable(bool enable);
    [DllImport("__Internal")]
    private static extern void updateConversionValue(string value);
#if IOS_CN
    [DllImport("__Internal")]
    private static extern void showAntiView();
#endif
    
    
#endif

    //实名认证弹框
    public void ShowAntiView()
    {

#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK && IOS_CN
        
        showAntiView();
#endif
    }

    //实名认证回调
    // 用户授权回调
    [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
    public delegate void antiSuccessDelegate();
    [AOT.MonoPInvokeCallback(typeof(antiSuccessDelegate))]
    static void antiSuccessHandle()
    {
        Instance.onAntiSuccessHander?.Invoke();
    }

    //设置log开关
    public void SetLogEnable(bool enable)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        setLogEnable(enable);
#endif
    }

    public void InitSDK(string appID, string appKey)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        initSDK(appID, appKey);
#endif
    }

    #region native banner
    public void LoadNativeBannerWithUnitId(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        loadNativeBannerWithUnitId(unitId);
#endif
    }

    public bool IsReadyNativeBannerWithUnitId(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        return isReadyNativeBannerWithUnitId(unitId);
#else
        return false;
#endif
    }

    public void SetNativeBannerAlign(string align)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        setNativeBannerAlign(align);
#endif
    }

    public void ShowNativeBannerWithUnitId(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        showNativeBannerWithUnitId(unitId);
#endif
    }

    public void HideNativeBanner()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        hideNativeBanner();
#endif
    }

    public void RemoveNativeBanner()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        removeNativeBanner();
#endif
    }
    #endregion


    #region banner
    public void LoadBannerWithUnitId(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        loadBannerWithUnitId(unitId);
#endif
    }

    public bool IsReadyBannerWithUnitId(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        return isReadyBannerWithUnitId(unitId);
#else
        return false;
#endif
    }

    public void SetBannerAlign(string align)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        setBannerAlign(align);
#endif
    }

    public void ShowBannerWithUnitId(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        showBannerWithUnitId(unitId);
#endif
    }

    public void HideBanner()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        hideNativeBanner();
#endif
    }

    public void RemoveBanner()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        removeNativeBanner();
#endif
    }
    #endregion

    #region interstitial
    public void Loadinterstitial(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        loadInterstitialWithUnitId(unitId);
#endif
    }

    public bool IsReadyinterstitial(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        return isReadyInterstitialWithUnitId(unitId);
#else
        return false;
#endif
    }

    public void Showinterstitial(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        showInterstitialWithUnitId(unitId);
#endif
    }
    #endregion


    #region native
    public void LoadNative(string unitId, float width, float height)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        loadNativeWithUnitId(unitId, width, height);
#endif
    }

    public bool IsReadNative(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        return isReadyNativeWithUnitId(unitId);
#else
        return false;
#endif
    }

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

#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        layoutNativeWithFrame(x, y, width, height);
        showNativeWithUnitId(unitId);
#endif
    }

    public void RemoveNative()
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        removeNative();
#endif

    }
    #endregion

    #region rewardVideo
    public void LoadVideo(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        loadRewardVideoWithUnitId(unitId);
#endif
    }

    public bool IsReadyVideo(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        return isReadyRewardVideoWithUnitId(unitId);
#else
        return false;
#endif
    }

    public void ShowVideo(string unitId)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        showRewardVideoWithUnitId(unitId);
#endif
    }
    #endregion

    #region
    public void UpdateConversionValue(string value)
    {
#if UNITY_IPHONE && !UNITY_EDITOR && !NO_SDK
        updateConversionValue(value);
#endif
    }
    #endregion

    #region

    public void AntiSuccessCallback()
    {
        onAntiSuccessHander?.Invoke();
    }
    #endregion

    #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)
    {
        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


}


