namespace Guru
{
    using UnityEngine;
    using System;
    
    
    public partial class GuruSDK
    {
        /// 
        /// 启动广告服务
        /// 
        public static void StartAds()
        {
            if (InitConfig.UseCustomConsent)
            {
                Debug.Log($"{Tag} --- Call StartAdsWithCustomConsent when you use custom consent, and pass the result (boolean) to the method.");
            }
            else
            {
                // 默认的启动顺序是先启动Consent后, 根据用户回调的结果来启动广告
                Instance.StartConsentFlow();
            }
        }
        /// 
        /// 使用自定义的Consent, 获取用户授权后, 调用此方法
        /// 
        /// 
        public static void StartAdsWithCustomConsent(bool userAllow = true)
        {
            if (userAllow)
            {
                StartAdService();
            }
            else
            {
                Debug.Log($"{Tag} --- User refuse to provide ads Id, Ads Service will be cancelled");
            }
        }
        #region Guru Consent
        
        /// 
        /// 启动Consent流程
        /// 
        private void StartConsentFlow()
        {
            LogI($"StartConsentFlow");
            GuruConsent.StartConsent(OnConsentOver);
        }
        private void OnConsentOver(int code)
        {
            Callbacks.ConsentFlow._onConsentResult?.Invoke(code);
            switch(code)
            {
                case GuruConsent.StatusCode.OBTAINED:
                case GuruConsent.StatusCode.NOT_AVAILABLE:
                    // 已获取授权, 或者地区不可用
#if UNITY_IOS
                    CheckATTStatus();
#else
                    StartAdService();
#endif
                    break;
            }
        }
#if UNITY_IOS
         /// 
        /// iOS 平台检查 ATT 状态
        /// 
        private void CheckATTStatus()
        {
            AttManager.Instance.CheckATTStatus(OnATTStatus);
        }
#endif
        
        #endregion
        #region Ad Services
        private static bool _initAdsCompleted = false;
        
        
        /// 
        /// 启动广告服务
        /// 
        public static void StartAdService()
        {
            LogI($"StartAdService");
            ADService.Instance.StartService(OnAdsInitComplete, 
                InitConfig.AutoLoadWhenAdsReady, IsDebugMode);
            
            //--------- Callbacks -----------
            ADService.OnBannerLoaded = OnBannerLoaded;
            ADService.OnInterstitialLoaded = OnInterstitialLoaded;
            ADService.OnInterstitialFailed = OnInterstitialFailed;
            ADService.OnRewardLoaded = OnRewardLoaded;
            ADService.OnRewardFailed = OnRewardFailed;
        }
        
        private static void OnBannerLoaded() 
            => Callbacks.Ads._onBannerADLoaded?.Invoke();
        private static void OnInterstitialLoaded() 
            => Callbacks.Ads._onInterstitialADLoaded?.Invoke();
        private static void OnInterstitialFailed()
            => Callbacks.Ads._onInterstitialADFailed?.Invoke();
        private static void OnRewardLoaded()
            => Callbacks.Ads._onRewardedADLoaded?.Invoke(); 
        private static void OnRewardFailed()
            => Callbacks.Ads._onRewardADFailed?.Invoke();
        private static void OnAdsInitComplete()
        {
            _initAdsCompleted = true;
            Callbacks.Ads._onAdsInitComplete?.Invoke();
        }
        private static bool CheckAdsReady()
        {
            if (!_initAdsCompleted)
            {
                LogE("Ads is not ready. Call  first.");
                return false;
            }
            return true;
        }
        /// 
        /// 显示Banner广告
        /// 
        /// 
        public static void ShowBanner(string placement = "")
        {
            if (!CheckAdsReady()) return;
            ADService.Instance.ShowBanner(placement);
        }
        /// 
        /// 隐藏Banner广告
        /// 
        public static void HideBanner()
        {
            if (!CheckAdsReady()) return;
            ADService.Instance.HideBanner();
        }
        public static void LoadInterstitialAd()
        {
            if (!CheckAdsReady()) return;
            ADService.Instance.RequestInterstitialAD();
        }
        public static bool IsInterstitialAdReady => ADService.Instance.IsInterstitialADReady();
        
        /// 
        /// 显示插屏广告
        /// 
        /// 
        /// 
        public static void ShowInterstitialAd(string placement = "", Action onDismissed = null)
        {
            if (!CheckAdsReady()) return;
            if (!ADService.Instance.IsInterstitialADReady())
            {
                LogE("Interstitial is not ready. Call  again.");
                LoadInterstitialAd();
                return;
            }
            ADService.Instance.ShowInterstitialAD(placement, onDismissed);
        }
        public static void LoadRewardAd()
        {
            if (!CheckAdsReady()) return;
            ADService.Instance.RequestRewardedAD();
        }
        
        public static bool IsRewardAdReady => ADService.Instance.IsRewardedADReady();
        
        /// 
        /// 显示激励视频广告
        /// 
        /// 
        /// 
        /// 
        public static void ShowRewardAd(string placement = "", Action onRewarded = null, Action onFailed = null)
        {
            if (!CheckAdsReady()) return;
            if (!ADService.Instance.IsRewardedADReady())
            {
                LogE("RewardAd is not ready. Call  again.");
                LoadRewardAd();
                return;
            }
            ADService.Instance.ShowRewardedAD(placement, onRewarded, onFailed);
        }
        #endregion
        #region MaxServices
        /// 
        /// 显示Max调试菜单
        /// 
        public static void ShowMaxDebugPanel()
        {
#if UNITY_EDITOR
            LogI($"Can not show Max Debug Panel in Editor, skipped.");
            return;
#endif
            if (!ADService.Instance.IsInitialized)
            {
                LogI($"ADService is not initialized, call  first.");
                return;
            }
            ADService.Instance.ShowMaxDebugPanel();
        }
        #endregion
    }
}