

namespace Guru
{
    using System;
    using UnityEngine;
    
    public class AnalyticsAgentAndroid: IAnalyticsAgent
    {
        
#if UNITY_ANDROID
        
        public static readonly string AnalyticsClassName = "com.guru.unity.analytics.Analytics";
        private static AndroidJavaClass _classAnalytics;
        private static AndroidJavaClass ClassAnalytics => _classAnalytics ??= new AndroidJavaClass(AnalyticsClassName);

#endif
        private static bool _isDebug = false;
        public static bool UseWorker = true;
        public static bool UseCronet = false;
        public static string BaseUrl = "";
            
        #region 工具方法

        

        /// <summary>
        /// 调用静态方法
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="args"></param>
        private static void CallStatic(string methodName, params object[] args)
        {
#if UNITY_ANDROID
            try
            {
                if (ClassAnalytics != null)
                {
                    ClassAnalytics.CallStatic(methodName, args);
                    if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static :: {methodName}");
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
#endif    
        }
        
        /// <summary>
        /// 调用静态方法
        /// </summary>
        /// <param name="methodName"></param>
        /// <param name="args"></param>
        /// <typeparam name="T"></typeparam>
        private static T CallStatic<T>(string methodName, params object[] args)
        { 
#if UNITY_ANDROID
            try
            {
                if (ClassAnalytics != null)
                {
                    if(_isDebug) Debug.Log($"{GuruAnalytics.Tag} Android call static <{typeof(T).ToString()}> :: {methodName}");
                    return ClassAnalytics.CallStatic<T>(methodName, args);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e.Message);
            }
#endif       
            return default(T);  
        }
        
        #endregion

        #region 接口实现

        public void Init(string appId, string deviceInfo, bool isDebug = false)
        {
            _isDebug = isDebug;
            string bundleId = Application.identifier;
            CallStatic("init", appId, deviceInfo, bundleId, UseWorker, isDebug, UseCronet, BaseUrl); // 调用接口   
        }

        public void SetScreen(string screenName)
        {
            if (string.IsNullOrEmpty(screenName)) return;
            CallStatic("setScreen", screenName);
        }
        public void SetAdId(string id)
        {
            if (string.IsNullOrEmpty(id)) return;
            CallStatic("setAdId", id);
        }

        public void SetUserProperty(string key, string value)
        {
            if (string.IsNullOrEmpty(key) || string.IsNullOrEmpty(value)) return;
            CallStatic("setUserProperty", key, value);
        }
        public void SetFirebaseId(string id)
        {
            if (string.IsNullOrEmpty(id)) return;
            CallStatic("setFirebaseId", id);
        }

        public void SetAdjustId(string id)
        {
            if (string.IsNullOrEmpty(id)) return;
            CallStatic("setAdjustId", id);
        }

        public void SetDeviceId(string deviceId)
        {
            if (string.IsNullOrEmpty(deviceId)) return;
            CallStatic("setDeviceId", deviceId);
        }

        public void SetUid(string uid)
        {
            if (string.IsNullOrEmpty(uid)) return;
            CallStatic("setUid", uid);
        }

        public bool IsDebug => CallStatic<bool>("isDebug");
        public void LogEvent(string eventName, string parameters) => CallStatic("logEvent", eventName, parameters);
        public void ReportEventSuccessRate() => CallStatic("reportEventRate");
        public void SetTch02Value(double value) => CallStatic("setTch02Value", value);
        public void InitCallback(string objName, string method) => CallStatic("initCallback", objName, method);
        
        private bool _enableErrorLog;
        public bool EnableErrorLog
        {
            get => _enableErrorLog;
            set
            {
                _enableErrorLog = value;
                CallStatic("setEnableErrorLog", _enableErrorLog);  
            }
        }
        #endregion
        
    }
}