
using UnityEngine;

namespace Guru
{
	using System.Collections.Generic;
    
    /// <summary>
    /// 通用的ADService
    /// </summary>
    public class ADService: ADServiceBase<ADService>
    {
        /// <summary>
        /// 版本号
        /// </summary>
        public static readonly string Version = "1.4.0";
        
        #region 初始化
        
        /// <summary>
		/// 初始化服务
		/// </summary>
        protected override void InitService()
        {
	        base.InitService();
	        InitChannels();  // 启动各广告渠道代理
        }
		
		#endregion
		
        #region 渠道管理
        
        private HashSet<IAdChannel> _adChannels;
        private AdChanelMax _chanelMax;
        private IAsyncRequestChannel _asyncLoader;
        
        /// <summary>
        /// 各渠道初始化
        /// </summary>
        private void InitChannels()
        {
        	_adChannels = new HashSet<IAdChannel>();
        	IAdChannel channel = null;
            _asyncLoader = null;
        	
        	_chanelMax = new AdChanelMax(); // 默认持有MAXChannel
            _chanelMax.Initialize();
            
        	//------------ 以下为扩展的广告渠道 ------------------
            // 请根据项目需求实现各渠道接入的逻辑
            // 开启渠道需要添加对应的宏
            
        	channel = new AdChanelAmazon();
        	channel.Initialize();
        	_adChannels.Add(channel); // Amazon
            _asyncLoader = channel as IAsyncRequestChannel;
            if (_asyncLoader != null)
            {
	            _asyncLoader.OnBannerRequestOver = (success, firstLoad) =>
	            {
		            Debug.Log($"--- [Amazon] Async banner response, success:{success} -> OnLoadMaxBanner");
		            // if (success && firstLoad) OnLoadMaxBanner();  // 广告频率足够快, 不需要再次调用 
	            };
	            _asyncLoader.OnInterstitialRequestOver = (success, firstLoad) =>
	            {
		            Debug.Log($"--- [Amazon] Async IV response, success:{success} -> OnLoadMaxIV");
		            // if (success && firstLoad) OnLoadMaxIV();
	            };
	            _asyncLoader.OnRewardRequestOver = (success, firstLoad) =>
	            {
		            Debug.Log($"--- [Amazon] Async RV response, success:{success} > OnLoadMaxRV");
		            // if (success && firstLoad) OnLoadMaxRV();
	            };
            }

        	channel = new AdChannelPubMatic();
        	channel.Initialize();
        	_adChannels.Add(channel);	// PubMatic

        	
        }
        #endregion

        #region Banner 广告

        
        /// <summary>
        /// 加载Banner广告
        /// </summary>
        public override void RequestBannerAD()
        {
	        if (!IsInitialized) return;
	        OnChannelLoadBannerAD(); // 调用各渠道请求Banner
        }
        
        
        /// <summary>
        /// 请求Banner广告
        /// </summary>
        private void OnChannelLoadBannerAD()
        {
	        IAsyncRequestChannel loader = null;
        	foreach (var channel in _adChannels)
        	{
        		channel?.LoadBannerAD();
            }

            // if (null == _asyncLoader) 
	        OnLoadMaxBanner();
        }
        
        private void OnLoadMaxBanner()
        {
	        _badsloadStartTime = Time.realtimeSinceStartup;
	        _chanelMax.LoadBannerAD();
        }

        #endregion

        #region Interstitial 广告


        public override void RequestInterstitialAD()
        {
	        if (!IsInitialized) return;
	        OnChannelLoadInterstitialAD();
        }


        /// <summary>
        /// 请求插屏广告
        /// </summary>
        private void OnChannelLoadInterstitialAD()
        {
        	foreach (var channel in _adChannels)
        	{
        		channel?.LoadInterstitialAD();
        	}
            // if (_asyncLoader == null) 
	        OnLoadMaxIV();
        }

        private void OnLoadMaxIV() {
	        _iadsLoadStartTime = Time.realtimeSinceStartup; // 更新计时器
	        _chanelMax.LoadInterstitialAD();
        }
        
        
        #endregion

        #region Reward 广告

        public override void RequestRewardedAD()
        {
	        if (!IsInitialized) return;
	        OnChannelLoadRewardAD();
        }
        
        /// <summary>
        /// 请求激励视频
        /// </summary>
        private void OnChannelLoadRewardAD()
        {
        	foreach (var channel in _adChannels)
        	{
        		channel?.LoadRewardAD();
        	}
            // if (_asyncLoader == null) 
	        OnLoadMaxRV();
        }
        
        private void OnLoadMaxRV()
        {
	        _radsLoadStartTime = Time.realtimeSinceStartup; // 更新计时器
	        _chanelMax.LoadRewardAD();
        }
    
        

        #endregion
        
    }
}