using System.Collections.Generic;
using UnityEngine;

namespace Guru
{
    public static partial class Analytics
    {
	    #region 广告属性

	    //------ 广告额外的参数 --------
	    private static readonly string ParameterNetworkName = "network_name"; //NetworkName 
	    private static readonly string ParameterNetworkPlacement = "network_placement";	//NetworkPlacement
	    private static readonly string ParameterWaterfall = "waterfall";	//Waterfall
	    
	    //------ 默认值 -------
	    public const string DefaultCategory = "not_set";
	    public const string DefaultWaterfall = "unknown";
	    
	    
	    #endregion
	    
	    #region Ads
	    
	    private static Dictionary<string, object> BuildAdEventData(AdParams adParams, Dictionary<string, object> extra = null)
	    {
		    if (string.IsNullOrEmpty(adParams.category)) 
			    adParams.category = DefaultCategory;
		    if (string.IsNullOrEmpty(adParams.waterfallName)) 
			    adParams.waterfallName = DefaultWaterfall;
		    
		    var data =  new Dictionary<string, object>()
		    {
			    { ParameterItemCategory, adParams.category },
			    { ParameterItemName, adParams.adUnitId },
		    };

		    if (adParams.duration > 0) 
			    data[ParameterDuration] = adParams.duration;
		    if (adParams.errorCode != 0) 
			    data[ParameterErrorCode] = adParams.errorCode;
		    if (!string.IsNullOrEmpty(adParams.networkName)) 
			    data[ParameterNetworkName] = adParams.networkName;
		    if (!string.IsNullOrEmpty(adParams.networkPlacement)) 
			    data[ParameterNetworkPlacement] = adParams.networkPlacement;
		    if (!string.IsNullOrEmpty(adParams.waterfallName)) 
			    data[ParameterWaterfall] = adParams.waterfallName;
		    
		    if (extra != null && extra.Count > 0)
		    {
			    foreach (var k in extra.Keys)
			    {
				    data[k] = extra[k];
			    }
		    }
		    
		    return data;
	    }
	    
		//---------------------- BANNER -------------------------
	    public static void ADBadsLoad(AdParams adParams)
	    {
		    LogEvent(EventBadsLoad, BuildAdEventData(adParams));
	    }
	    public static void ADBadsLoaded(AdParams adParams)
	    {
		    LogEvent(EventBadsLoaded, BuildAdEventData(adParams));
	    }
	    public static void ADBadsFailed(AdParams adParams)
	    {
		    LogEvent(EventBadsFailed, BuildAdEventData(adParams));
	    }
	    /// <summary>
	    /// 广告点击
	    /// </summary>
		public static void ADBadsClick(AdParams adParams)
	    {
		    LogEvent(EventBadsClick, BuildAdEventData(adParams));
	    }
	    public static void ADBadsImp(AdParams adParams)
	    {
		    LogEvent(EventBadsImp, BuildAdEventData(adParams));
	    }
	    
	    public static void ADBadsHide( int loadedTimes, int failTimes)
	    {
		    var dict = new Dictionary<string, object>()
		    {
			    ["loaded_times"] = loadedTimes,
			    ["fail_times"] = failTimes
		    };
		    LogEvent(EventBadsHide, dict);
	    }
		//---------------------- INTERSTITIAL -------------------------
		/// <summary>
		/// 广告拉取
		/// </summary>
		public static void ADIadsLoad(AdParams adParams)
		{
			LogEvent(EventIadsLoad, BuildAdEventData(adParams));
		}

		/// <summary>
		/// 广告拉取成功
		/// </summary>
		public static void ADIadsLoaded(AdParams adParams)
		{
			LogEvent(EventIadsLoaded, BuildAdEventData(adParams));
		}

		/// <summary>
		/// 广告拉取失败
		/// </summary>
		public static void ADIadsFailed(AdParams adParams)
		{
			LogEvent(EventIadsFailed, BuildAdEventData(adParams));
		}

		/// <summary>
		/// 广告展示
		/// </summary>
		public static void ADIadsImp(AdParams adParams)
		{
			LogEvent(EventIadsImp, BuildAdEventData(adParams));
		}

		/// <summary>
		/// 广告点击
		/// </summary>
		public static void ADIadsClick(AdParams adParams)
		{
			LogEvent(EventIadsClick, BuildAdEventData(adParams));
		}

		/// <summary>
		/// 用户关闭广告
		/// </summary>
		public static void ADIadsClose(AdParams adParams)
		{
			LogEvent(EventIadsClose, BuildAdEventData(adParams));
		}
		
	    //---------------------- REWARDS -------------------------
		/// <summary>
		/// 广告开始加载
		/// </summary>
		public static void ADRadsLoad(AdParams adParams)
		{
			LogEvent(EventRadsLoad, BuildAdEventData(adParams));
		}
		/// <summary>
		/// 广告拉取成功
		/// </summary>
		public static void ADRadsLoaded(AdParams adParams)
		{
			LogEvent(EventRadsLoaded, BuildAdEventData(adParams));
		}
		/// <summary>
		/// 广告拉取失败
		/// </summary>
		public static void ADRadsFailed(AdParams adParams)
		{
			LogEvent(EventRadsFailed, BuildAdEventData(adParams));
		}
		/// <summary>
		/// 广告展示
		/// </summary>
		public static void ADRadsImp(AdParams adParams)
		{
			LogEvent(EventRadsImp, BuildAdEventData(adParams));
		}
		/// <summary>
		/// 广告完成观看发奖励
		/// </summary>
		public static void ADRadsRewarded(AdParams adParams)
		{
			var data = BuildAdEventData(adParams);
			LogEvent(EventRadsRewarded, data);
			
			if (RadsRewardCount == 0)
			{
				RadsRewardCount = 1;
				ADRadsFirstRewarded(data);
			}
		}
		

		private static int RadsRewardCount
		{
			get => PlayerPrefs.GetInt(nameof(RadsRewardCount), 0);
			set => PlayerPrefs.SetInt(nameof(RadsRewardCount), value);
		}
		
		/// <summary>
		/// 用户首次完成观看
		/// </summary>
		public static void ADRadsFirstRewarded(Dictionary<string, object> data)
		{
			LogEvent(EventFirstRadsRewarded, data);
		}

		/// <summary>
		/// 广告点击
		/// </summary>
		public static void ADRadsClick(AdParams adParams)
		{
			LogEvent(EventRadsClick, BuildAdEventData(adParams));
		}

		/// <summary>
		/// 用户关闭广告
		/// </summary>
		public static void ADRadsClose(AdParams adParams)
		{
			LogEvent(EventRadsClose, BuildAdEventData(adParams));
		}
	    #endregion
        	
	    #region Ads-ATT

#if UNITY_IOS

        /// <summary>
        /// ATT 结果打点
        /// </summary>
        /// <param name="status"></param>
        /// <param name="channel"></param>
        /// <param name="others"></param>
        public static void AttResult(string status, string type = "custom", string others = "")
        {
	        SetAttProperty(status);
	        Debug.Log($"{TAG} AttResult: {status}    type:{type}    others:{others}");
	        var dict = new Dictionary<string, dynamic>()
	        {
		        { ParameterItemCategory, status },
		        { "type", type }
	        };
	        if(!string.IsNullOrEmpty(others)) dict[ParameterItemName] = others;
            LogEvent(EventATTResult, dict);
        }

        /// <summary>
        /// 上报 ATT 当前的属性
        /// </summary>
        /// <param name="status"></param>
        public static void SetAttProperty(string status)
        {
	        Debug.Log($"{TAG} SetAttProperty: {status}");
            SetUserProperty(PropertyATTStatus, status);
        }
        
#endif

	    #endregion
    }
    
    /// <summary>
    /// 广告打点上报参数
    /// </summary>
    public class AdParams
    {
	    public string category;
	    public string networkName;
	    public string networkPlacement;
	    public string waterfallName;
	    public string adUnitId;
	    public int errorCode = 0; 
	    public int duration = 0;


	    /// <summary>
	    /// 构造AD参数
	    /// </summary>
	    /// <param name="adInfo"></param>
	    /// <param name="adUnitId"></param>
	    /// <param name="category"></param>
	    /// <param name="duration"></param>
	    /// <param name="errorCode"></param>
	    /// <param name="waterfallName"></param>
	    /// <returns></returns>
	    public static AdParams Build(string adUnitId, MaxSdkBase.AdInfo adInfo = null, string category = "", 
		    int duration = 0, int errorCode = 0, string waterfallName = "")
	    {
		    if (string.IsNullOrEmpty(adUnitId) && adInfo != null) adUnitId = adInfo.AdUnitIdentifier;
		    var networkName = "";
		    var networkPlacement = "";
		    if (adInfo != null)
		    {
			    networkName = adInfo.NetworkName;
			    networkPlacement = adInfo.NetworkPlacement;

			    if (string.IsNullOrEmpty(waterfallName))
				    waterfallName = adInfo.WaterfallInfo?.Name ?? "";
		    }

		    var p = new AdParams()
		    {
			    adUnitId = adUnitId,
			    duration = duration,
			    networkName = networkName,
			    networkPlacement = networkPlacement,
			    waterfallName = waterfallName,
			    category = category,
			    errorCode = errorCode,
		    };
		    return p;
	    }
    }
    
}