using System;
using System.Collections.Generic;
using System.Globalization;
// using AppsFlyerSDK;
using DG.Tweening;
using IgnoreOPS;
using SGModule.Net;
using SGModule.NetKit;
using UnityEngine;
using UnityEngine.Events;
using Random = UnityEngine.Random;
// using AppsFlyerSDK;

// using AppsFlyerSDK;


namespace ChillConnect
{
    public class MaxADKit
    {
        public static string SDKKey =
            "mmVXU9O_ESeur52h0VwzCGfbI4wB8LxeBfydReplRMm2JmN8eNMVgSuiKy2bmTJ00m4DHSKFxhaH1pnpEME5bB";

        public static string interstitialADUnitID = "bb957e86a7b5ae7c";


        public static string rewardedADUnitID = "100870cb72cf50ca";
        public static string AppOpenAdUnitId = "b8a4fccf2f151b0d";

        private const float RevenueThreshold = 0.1f;

        public static Dictionary<string, string> adCallbackInfo;

        public static void Init()
        {
#if !UNITY_EDITOR
            // // 注册 ab事件，0或1，0为自然量版本，1为激励版本
            // NCWWA6A0SDK_Manager.Instance.RegistIosParam(i =>
            // {
            //     SuperApplication.Instance.attribution = i == 0 ? "organic" : "non_organic";
            //     Debug.Log($"ios ab param : {i}  attribution=== {SuperApplication.Instance.attribution}");
            //     NetworkDispatcher.Instance.Dispatch(NetworkMsg.Login);
            //     
            // });
            //
            // void GameConfig(bool result, string config)
            // {
            //     Debug.Log($"************* game config result : {result}, config : {config}");
            // }
            // // SDK初始化方法
            // NCWWA6A0SDK_Manager.Instance.Init(null, null, GameConfig);
            
#endif 
        }
        
        
        // public static bool CheckInterstitialReady()
        // {
        //     return NCWWA6A0SDK_Manager.Instance.IsInterReady();
        // }
        #region 插屏广告相关
        public static UnityAction<bool> onInterstitialAdCompleted;
        
        public static void ShowInterstitial(string placement = "DefaultInterstitial",
            UnityAction<bool> onCompleted = null)
        {
            // if (CheckInterstitialReady())
            // {
            //     Debug.Log($"广告已经准备好，播放");
            //     NCWWA6A0SDK_Manager.Instance.ShowInter(placement, () =>
            //     {
            //         DOVirtual.DelayedCall(0.1f, () =>
            //         {
            //             onCompleted?.Invoke(true);
            //         });
            //     });   
            // }
            // else
            // {
            //     Debug.Log($"广告未准备好，不播放");
            //     onCompleted?.Invoke(false);
            // }
        }

        static int retryAttemptInterstitial;

        #endregion
        //开屏相关
        public static UnityAction<bool> onAppOpenAdCompleted;
        private static void LoadAppOpenAd()
        {
            Debug.Log("LOAD APP. AD");
            MaxSdk.LoadAppOpenAd(AppOpenAdUnitId);
        }


        public static void ShowOpenAdIfReady(UnityAction<bool> onCompleted = null)
        {
            // #if UNITY_EDITOR
            //             onCompleted?.Invoke(true);
            // #else
            // onAppOpenAdCompleted = onCompleted;
            // if (!IsLoaded)
            // {
            //     MaxSdkCallbacks.AppOpen.OnAdLoadedEvent += ShowOpenAd;
            //
            //
            // }
            // else
            // {
            //     // bool isFinished = NetworkManager.isWatchedAD;
            //     // onCompleted?.Invoke(isFinished);
            //     ShowOpenAd(null, null);
            // }
            // #endif
        }
        public static void ShowOpenAd(string adUnitId, MaxSdkBase.AdInfo adInfo)
        {
            MaxSdk.ShowAppOpenAd(AppOpenAdUnitId);
        }
        

        #region 激励视频广告相关

        public static UnityAction<bool> onVideoAdCompleted;
        private static string _placement = "";
        // public static bool CheckRewardedReady()
        // {
        //     return NCWWA6A0SDK_Manager.Instance.IsVideoReady();
        // }
        public static void ShowVideo(string placement = "DefaultVideo", UnityAction<bool> onCompleted = null)
        {
            onVideoAdCompleted = onCompleted;
            _placement = placement;
#if UNITY_EDITOR || !GAME_RELEASE
             onVideoAdCompleted?.Invoke(true);
#else
            TrackKit.SendEvent(Property.adEvent,Property.watch_ad_people);
            TrackKit.SendEvent(Property.adEvent,Property.Rewarded_videos_trigger_number);

            // if (CheckRewardedReady())
            // {
            //     NCWWA6A0SDK_Manager.Instance.ShowRewardVideo(_placement, b =>
            //     {
            //         DOVirtual.DelayedCall(0.1f, () =>
            //         {
            //             onVideoAdCompleted?.Invoke(b);
            //         });
            //     }, ()=>
            //     {
            //         DOVirtual.DelayedCall(0.25f, () =>
            //         {
            //             Debug.Log($"激励广告关闭");
            //             if (GameHelper.IsGiftSwitch() && !SaveData.GetSaveObject().is_get_removead && (UnityEngine.Random.Range(0, 100) < GameHelper.GetCommonModel().rewardinsertion))
            //             {
            //                 TrackKit.SendEvent(Property.adEvent, Property.AfterRewardAdShow);
            //
            //                 GameHelper.ShowInterstitial("AfterReward");
            //             }
            //         });
            //     });
            // }
            // else
            // {
            //     Debug.Log($"广告未准备好，不播放");
            //     onVideoAdCompleted?.Invoke(false);
            // }
#endif

        }
        #endregion
    }
}