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

/// <summary>
/// 广告SDK内部处理
/// </summary>
public class ToukaSDKAdapter : IToukaAdInterface
{
    /// <summary>
    /// banner 位置
    /// </summary>
    public enum ToukaBannerAlign:int
    {
        BannerCenterBottomAlign = 34,
        BannerCenterTopAlign = 10,
    }

    // banner是否已展示
    private bool m_bannerHasShowed = false;
    // banner是否已加载
    private bool m_bannerLoaded = false;

    private System.Action<bool> m_bannerLoadCallback;       // banner加载回调
    private System.Action<bool> m_intersitialLoadCallback;  // 插屏加载回调
    private System.Action<bool> m_videoLoadCallback;        // 激励视频加载回调
    private System.Action<bool> m_nativeLoadCallback;       // native加载回调

    private System.Action<ToukaVideoStatus> m_videoShowCallback;        // 激励视频展示回调
    private System.Action<ToukaIntersitialStatus> m_intersitialShowCallback;        // 插屏展示回调

    private static ToukaSDKAdapter s_instance;
    public static ToukaSDKAdapter Instance
    {
        get
        {
            if (s_instance == null)
            {
                s_instance = new ToukaSDKAdapter();
            }
            return s_instance;
        }
    }

    /// <summary>
    /// init
    /// </summary>
    public void Init()
    {
#if UNITY_IOS && !NO_SDK
        Debug.Log("[ToukaSDKAdapter] init iOS");
        ToukaCallbackInit();
        TGiOSAdManager.Instance.InitSDK(StaticStringsKey.TOUKA_SDK_APPID, StaticStringsKey.TOUKA_SDK_APPKEY);
#elif UNITY_ANDROID 
        Debug.Log("[ToukaSDKAdapter] init android");
        ToukaCallbackInit();
        TGAndroidAdManager.Instance.InitSDK(StaticStringsKey.TOUKA_SDK_APPID,StaticStringsKey.TOUKA_SDK_APPKEY);
#endif
    }

    /// <summary>
    /// sdk 回调
    /// </summary>
    private void ToukaCallbackInit()
    {
        TGUniversalFunc.onSetNativeBannerListener_onAdLoaded += onNativeBannerAdLoaded;
        TGUniversalFunc.onSetNativeBannerListener_onAdLoadFail += onNativeBannerAdLoadFail;

        TGUniversalFunc.onSetBannerListener_onAdLoaded += onBannerAdLoad;
        TGUniversalFunc.onSetBannerListener_onAdFailedToLoad += onBannerAdLoadFail;

        TGUniversalFunc.onSetNativeListener_onAdLoaded += onNativeAdLoaded;
        TGUniversalFunc.onSetNativeListener_onAdFailedToLoad += onNativeAdLoadFail;

        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdClosed += onRewardedVideoAdPlayClosed;
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdFailedToLoad += onRewardedVideoAdLoadFail;
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdLoaded += onRewardedVideoAdLoaded;
        TGUniversalFunc.onSetRewardListener_onRewardedVideoAdPlayFail += onRewardedVideoAdPlayFail;
        TGUniversalFunc.onSetRewardListener_onRewardedVideoStarted += onRewardedVideoAdPlayStart;


        TGUniversalFunc.onSetInterstitialListener_onAdClosed += onInterstitialAdClose;
        TGUniversalFunc.onSetInterstitialListener_onAdFailedToLoad += onInterstitialAdLoadFail;
        TGUniversalFunc.onSetInterstitialListener_onAdLoaded += onInterstitialAdLoad;
        TGUniversalFunc.onSetInterstitialListener_onShowFailed += onInterstitialAdFailedToShow;
        TGUniversalFunc.onSetInterstitialListener_onShowSuccess += onInterstitialAdShow;
    }

    #region 广告 IsReady 接口

    /// <summary>
    /// 插屏 IsReady
    /// </summary>
    /// <returns></returns>
    public bool IsReadyIntersitial()
    {
#if UNITY_IOS && !NO_SDK
        return TGiOSAdManager.Instance.IsReadyinterstitial(StaticStringsKey.TOUKA_SDK_INTERID);
#elif UNITY_ANDROID
        return TGAndroidAdManager.Instance.IsReadyinterstitial(StaticStringsKey.TOUKA_SDK_INTERID);
#endif
        return false;
    }

    /// <summary>
    /// 激励视频 IsReady
    /// </summary>
    /// <returns></returns>
    public bool IsReadyVideo()
    {
#if UNITY_IOS && !NO_SDK         
        return TGiOSAdManager.Instance.IsReadyVideo(StaticStringsKey.TOUKA_SDK_REWARDID);
#elif UNITY_ANDROID
        return TGAndroidAdManager.Instance.IsReadyVideo(StaticStringsKey.TOUKA_SDK_REWARDID) ;
#endif
        return false;
    }

    /// <summary>
    /// banner IsReady
    /// </summary>
    /// <returns></returns>
    public bool IsReadyBanner()
    {
        return m_bannerLoaded;
    }

    /// <summary>
    /// native IsReady
    /// </summary>
    /// <returns></returns>
    public bool IsReadyNative()
    {
#if UNITY_IOS && !NO_SDK
        return TGiOSAdManager.Instance.IsReadNative(StaticStringsKey.TOUKA_SDK_NATIVEID);
#elif UNITY_ANDROID
        return TGAndroidAdManager.Instance.IsReadNative(StaticStringsKey.TOUKA_SDK_NATIVEID) ;
#endif
        return false;
    }

    #endregion

    #region 广告展示 Show 接口
    /// <summary>
    /// 展示插屏
    /// </summary>
    /// <param name="_callback"> 插屏播放回调 </param>
    public void ShowIntersitial(Action<ToukaIntersitialStatus> _callback)
    {
        m_intersitialShowCallback = _callback;
#if UNITY_IOS && !NO_SDK
        TGiOSAdManager.Instance.Showinterstitial(StaticStringsKey.TOUKA_SDK_INTERID);
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.Showinterstitial(StaticStringsKey.TOUKA_SDK_INTERID);
#endif
    }

    /// <summary>
    /// 展示激励视频
    /// </summary>
    /// <param name="_callback"> 激励视频播放回调 </param>
    public void ShowVideo(Action<ToukaVideoStatus> _callback)
    {
        m_videoShowCallback = _callback;
#if UNITY_IOS && !NO_SDK
        TGiOSAdManager.Instance.ShowVideo (StaticStringsKey.TOUKA_SDK_REWARDID);
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.ShowVideo(StaticStringsKey.TOUKA_SDK_REWARDID);
#endif
    }

    /// <summary>
    /// 展示banner
    /// </summary>
    public void ShowBanner()
    {
        if (m_bannerHasShowed == false)
        {
#if UNITY_IOS
#if NORMAL_BANNER
                    TGiOSAdManager.Instance.SetBannerAlign(ToukaBannerAlign.BannerCenterBottomAlign.ToString());
                    TGiOSAdManager.Instance.ShowBannerWithUnitId(StaticStringsKey.TOUKA_SDK_BANNERID);
                    m_bannerHasShowed = true;
#elif NATIVE_BANNER
                    TGiOSAdManager.Instance.SetNativeBannerAlign(((int)ToukaBannerAlign.BannerCenterBottomAlign).ToString());
                    TGiOSAdManager.Instance.ShowNativeBannerWithUnitId(StaticStringsKey.TOUKA_SDK_BANNERID);
                    m_bannerHasShowed = true;
#endif
#elif UNITY_ANDROID
            TGAndroidAdManager.Instance.ShowBannerWithUnitId(StaticStringsKey.TOUKA_SDK_BANNERID);
#endif
        }
    }

    /// <summary>
    /// 展示native
    /// </summary>
    /// <param name="x"> native x坐标 </param>
    /// <param name="y"> native y坐标 </param>
    /// <param name="width"> native width </param>
    /// <param name="height"> native height </param>
    public void ShowNative(float x, float y, float width, float height)
    {
#if UNITY_IOS && !NO_SDK
        TGiOSAdManager.Instance.ShowNative(x, y, width, height, StaticStringsKey.TOUKA_SDK_NATIVEID);
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.ShowNative(x, y, width, height, StaticStringsKey.TOUKA_SDK_NATIVEID);
#endif
    }

    #endregion

    #region 广告加载 Load 接口

    /// <summary>
    /// 加载插屏
    /// </summary>
    /// <param name="_callback"></param>
    public void LoadInterstitial(System.Action<bool> _callback)
    {
        m_intersitialLoadCallback = _callback;
#if UNITY_IOS && !NO_SDK
        TGiOSAdManager.Instance.Loadinterstitial(StaticStringsKey.TOUKA_SDK_INTERID);
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.Loadinterstitial(StaticStringsKey.TOUKA_SDK_INTERID);
#endif
    }

    /// <summary>
    /// 加载激励视频
    /// </summary>
    /// <param name="_callback"></param>
    public void LoadVideo(System.Action<bool> _callback)
    {
        m_videoLoadCallback = _callback;
#if UNITY_IOS && !NO_SDK
        TGiOSAdManager.Instance.LoadVideo(StaticStringsKey.TOUKA_SDK_REWARDID);
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.LoadVideo(StaticStringsKey.TOUKA_SDK_REWARDID);
#endif
    }

    /// <summary>
    /// 加载banner
    /// </summary>
    /// <param name="_callback"></param>
    public void LoadBanner(System.Action<bool> _callback)
    {
        m_bannerLoadCallback = _callback;
#if UNITY_IOS
#if NORMAL_BANNER
            TGiOSAdManager.Instance.LoadBannerWithUnitId(StaticStringsKey.TOUKA_SDK_BANNERID);
#elif NATIVE_BANNER
            TGiOSAdManager.Instance.LoadNativeBannerWithUnitId(StaticStringsKey.TOUKA_SDK_BANNERID);
#endif
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.LoadBannerWithUnitId(StaticStringsKey.TOUKA_SDK_BANNERID);
#endif
    }

    /// <summary>
    /// 加载native
    /// </summary>
    /// <param name="_callback"></param>
    public void LoadNative(System.Action<bool> _callback)
    {
        Debug.Log("[ToukaSDKAdapter]  LoadNative -------");
        m_nativeLoadCallback = _callback;
#if UNITY_IOS && !NO_SDK
        // if (!ToukaUtils.FloatEqual(ToukaAdManager.Instance.NativeWidth, 0) && !ToukaUtils.FloatEqual(ToukaAdManager.Instance.NativeHeight, 0))
        // {
            Debug.Log("native width, native height != 0, nativeWidth : " + ToukaAdManager.Instance.NativeWidth + " , nativeHeight : " + ToukaAdManager.Instance.NativeHeight);
            TGiOSAdManager.Instance.LoadNative(StaticStringsKey.TOUKA_SDK_NATIVEID, ToukaAdManager.Instance.NativeWidth, ToukaAdManager.Instance.NativeHeight);     // 不需要了，传0就可以
        // }

#elif UNITY_ANDROID

#endif
    }

    #endregion

    #region 广告移除 Remove /隐藏 Hide 接口

    /// <summary>
    /// 移除banner
    /// </summary>
    /// <param name="_clean"> true:清除 flse:隐藏</param>
    public void RemoveBanner(bool _clean)
    {
        Debug.Log("in ToukaSDKAdapter, RemoveBanner , _clean : " + _clean);
#if UNITY_IOS
        if (_clean) {
            Debug.Log("in ToukaSDKAdapter, RemoveBanner ,removeBanner. 01");
#if NORMAL_BANNER
                TGiOSAdManager.Instance.RemoveBanner ();
#elif NATIVE_BANNER
                TGiOSAdManager.Instance.RemoveNativeBanner();
#endif
        }
        else
        {
            Debug.Log("in ToukaSDKAdapter, RemoveBanner ,removeBanner. 02");
#if NORMAL_BANNER
                TGiOSAdManager.Instance.HideBanner ();
#elif NATIVE_BANNER
                TGiOSAdManager.Instance.HideNativeBanner();
#endif
        }

        m_bannerHasShowed = false;
#elif UNITY_ANDROID
        TGAndroidAdManager.Instance.HideBanner();
#endif
    }

    /// <summary>
    /// 移除native
    /// </summary>
    /// <param name="_isClean"> true:清除 false:隐藏 </param>
    public void RemoveNative(bool _isClean)
    {
#if UNITY_IOS && !NO_SDK         
        TGiOSAdManager.Instance.RemoveNative();
#elif UNITY_ANDROID
        
#endif
    }

    #endregion

    #region 广告回调

    #region TKInitListener

    public void initSuccess() {
        Debug.Log("[ToukaSDKAdapter]: sdk init succ");
    }

    public void initFail()
    {
        Debug.Log("[ToukaSDKAdapter]: sdk init Fail.");
    }

    #endregion

    #region InterstitialAdListener

    public void onInterstitialAdClick() { }

    public void onInterstitialAdClose()
    {
        Debug.Log("[ToukaSDKAdapter]: Interstitial ad close.");
        if (m_intersitialShowCallback != null) m_intersitialShowCallback(ToukaIntersitialStatus.CLOSE);
    }

    public void onInterstitialAdEndPlayingVideo() { }

    public void onInterstitialAdFailedToPlayVideo() { }

    public void onInterstitialAdLoad()
    {
        m_intersitialLoadCallback(true);
    }

    public void onInterstitialAdLoadFail()
    {
        m_intersitialLoadCallback(false);
    }

    public void onInterstitialAdShow()
    {
        Debug.Log("[ToukaSDKAdapter]: Interstitial ad show.");
        if (m_intersitialShowCallback != null) m_intersitialShowCallback(ToukaIntersitialStatus.SHOW);
    }

    public void onInterstitialAdStartPlayingVideo() { }

    public void onInterstitialAdFailedToShow()
    {
        if (m_intersitialShowCallback != null) m_intersitialShowCallback(ToukaIntersitialStatus.FAIL);
    }
    #endregion

    #region RewardedVideoListener

    public void onRewardedVideoAdLoaded () {
        if(m_videoLoadCallback != null)
        {
            m_videoLoadCallback(true);
        }
    }

    public void onRewardedVideoAdLoadFail () {
        if (m_videoLoadCallback != null)
        {
            m_videoLoadCallback(false);
        }
    }

    public void onRewardedVideoAdPlayStart()
    {
        if (m_videoShowCallback != null) m_videoShowCallback(ToukaVideoStatus.SHOW);
    }

    public void onRewardedVideoAdPlayEnd() { }

    public void onRewardedVideoAdPlayFail()
    {
        if (m_videoShowCallback != null) m_videoShowCallback(ToukaVideoStatus.FAIL);
    }

    public void onRewardedVideoAdPlayClosed(string _isReward)
    {
        Debug.Log("[ToukaSDKAdapter]: reward ad close, isReward : " + _isReward);
        bool isReward = _isReward == "True";
        if (m_videoShowCallback != null) m_videoShowCallback(ToukaVideoStatus.CLOSE);
        if (m_videoShowCallback != null) m_videoShowCallback(isReward ? ToukaVideoStatus.SUCCESS : ToukaVideoStatus.CANCEL);
    }

    public void onRewardedVideoAdPlayClicked() { }

    #endregion

    #region BannerAdListener
    public void onAdAutoRefresh() { }

    public void onAdAutoRefreshFail() { }

    public void onAdClick() { }

    public void onAdClose() { }

    public void onAdImpress() { }

    public void onBannerAdLoad()
    {
        m_bannerLoadCallback(true);
        m_bannerLoaded = true;
    }

    public void onBannerAdLoadFail()
    {
        m_bannerLoadCallback(false);
    }
    #endregion

    #region NativeBannerAdListener
    public void onNativeBannerAdLoaded()
    {
        m_bannerLoadCallback(true);
        m_bannerLoaded = true;
    }

    public void onNativeBannerAdLoadFail()
    {
        Debug.Log("[ToukaSDKAdapter] onNativeBannerAdLoadFail.");
        m_bannerLoadCallback(false);
    }

    public void onNativeBannerAdImpressed() { }

    public void onNativeBannerAdClicked() { }

    public void onNativeBannerAdAutoRefresh() { }

    public void onNativeBannerAdAutoRefreshFailure() { }

    public void onNativeBannerAdCloseButtonClicked() { }

    public void onNativeAdLoaded()
    {
        Debug.Log("[ToukaSDKAdapter] onNativeAdLoaded ----");
        if (m_nativeLoadCallback != null)
        {
            m_nativeLoadCallback(true);
        }
    }

    public void onNativeAdLoadFail()
    {
        Debug.Log("[ToukaSDKAdapter] onNativeAdLoadFail ----");
        if (m_nativeLoadCallback != null)
        {
            m_nativeLoadCallback(false);
        }
    }

    #endregion

    #endregion
}