using System.Runtime.InteropServices;

namespace Guru
{
    public class AnalyticsAgentIOS: IAnalyticsAgent
    {

        #region 属性定义

        private const string K_INTERNAL = "__Internal";
        
#if UNITY_IOS
        // ------------- U3DAnalytics.mm Interface -----------------
        [DllImport(K_INTERNAL)] private static extern void unityInitAnalytics(string appId, string deviceInfo, bool isDebug);
        [DllImport(K_INTERNAL)] private static extern void unitySetUserID(string uid);
        [DllImport(K_INTERNAL)] private static extern void unitySetScreen(string screenName);
        [DllImport(K_INTERNAL)] private static extern void unitySetAdId(string adId);
        [DllImport(K_INTERNAL)] private static extern void unitySetAdjustID(string adjustId);
        [DllImport(K_INTERNAL)] private static extern void unitySetFirebaseId(string fid);
        [DllImport(K_INTERNAL)] private static extern void unitySetDeviceId(string did);
        [DllImport(K_INTERNAL)] private static extern void unitySetUserProperty(string key, string value);
        [DllImport(K_INTERNAL)] private static extern void unityLogEvent(string key, string data);
        [DllImport(K_INTERNAL)] private static extern void unityReportEventRate();
        [DllImport(K_INTERNAL)] private static extern void unityInitException();
        [DllImport(K_INTERNAL)] private static extern void unityTestUnrecognizedSelectorCrash();
        [DllImport(K_INTERNAL)] private static extern void unitySetTch02Value(double value);
#endif
        
        private static bool _isDebug = false;    
            
        #endregion

        #region 接口实现

        public void Init(string appId, string deviceInfo, bool isDebug = false)
        {
            _isDebug = isDebug;
#if UNITY_IOS
            unityInitAnalytics(appId, deviceInfo, isDebug);    
            unityInitException(); // 初始化报错守护进程
#endif
        }

        public void SetScreen(string screenName)
        {
#if UNITY_IOS
            unitySetScreen(screenName);
#endif
        }

        public void SetAdId(string id)
        {
#if UNITY_IOS
            unitySetAdId(id);
#endif
        }

        public void SetUserProperty(string key, string value)
        {
#if UNITY_IOS
            unitySetUserProperty(key, value);
#endif
        }

        public void SetFirebaseId(string fid)
        {
#if UNITY_IOS
            unitySetFirebaseId(fid);
#endif
        }

        public void SetAdjustId(string id)
        {
#if UNITY_IOS
            unitySetAdjustID(id);
#endif
        }

        public void SetDeviceId(string deviceId)
        {
#if UNITY_IOS
            unitySetDeviceId(deviceId);
#endif
        }

        public void SetUid(string uid)
        {
#if UNITY_IOS
            unitySetUserID(uid);
#endif
        }

        public bool IsDebug => _isDebug;

        public void LogEvent(string eventName, string data)
        {
#if UNITY_IOS
            unityLogEvent(eventName, data);
#endif
        }
        
        public void ReportEventSuccessRate()
        {
#if UNITY_IOS
            unityReportEventRate();
#endif
        }
        
        public void SetTch02Value(double value)
        {
#if UNITY_IOS
            unitySetTch02Value(value);
#endif
        }
        
#if UNITY_IOS
        // iOS 测试用事件
        public static void TestCrashEvent()=> unityTestUnrecognizedSelectorCrash();
#endif
        
        #endregion
        
    }
}