
namespace Guru
{
    using System;
    using System.Collections;
    using Newtonsoft.Json;
    using UnityEngine;
    
    /// <summary>
    /// GuruConsent 流程封装
    /// </summary>
    public class GuruConsent
    {
        // Guru Consent Version
        public static string Version = "1.0.6";
        public static string Tag = "[GuruConsent]";

        #region 公用接口

        private static Action<int> onCompleteHandler = null;

        private static IConsentAgent _agent;
        private static IConsentAgent Agent
        {
            get
            {
                if (_agent == null)
                {
#if UNITY_EDITOR
                    _agent = new ConsentAgentStub();
#elif UNITY_ANDROID
                    _agent = new ConsentAgentAndroid();
#elif UNITY_IOS
                    _agent = new ConsentAgentIOS(); 
#endif
                    _agent?.Init(GuruSDKCallback.ObjectName, GuruSDKCallback.MethodName);
                }
                return _agent;
            }
        }
        
        private static string _dmaMapRule = "";
        private static bool _enableCountryCheck = true;

        /// <summary>
        /// 对外公开接口
        /// </summary>
        /// <param name="onComplete"></param>
        /// <param name="deviceId"></param>
        /// <param name="debugGeography"></param>
        /// <param name="dmaMapRule"></param>
        /// <param name="enableCountryCheck"></param>
        public static void StartConsent(Action<int> onComplete = null, 
            string deviceId = "", int debugGeography = -1,
            string dmaMapRule = "", bool enableCountryCheck = false)
        {
            Debug.Log($"{Tag} --- GuruConsent::StartConsent - deviceId:[{deviceId}]  debugGeography:[{debugGeography}]  dmaMapRule:[{dmaMapRule}]  enableCountryCheck:[{enableCountryCheck}]");

            _dmaMapRule = dmaMapRule;
            _enableCountryCheck = enableCountryCheck;
            onCompleteHandler = onComplete;
            // 初始化SDK对象
            GuruSDKCallback.AddCallback(OnSDKCallback);
            if (debugGeography == -1) debugGeography = DebugGeography.DEBUG_GEOGRAPHY_EEA;

            Agent?.RequestGDPR(deviceId, debugGeography);
        }

        
        
        /// <summary>
        /// 获取SDK回调
        /// </summary>
        /// <param name="msg"></param>
        private static void OnSDKCallback(string msg)
        {
            //-------- Fetch DMA status and report -----------
            var value = Agent?.GetPurposesValue() ?? "";
            GoogleDMAHelper.SetDMAStatus(value, _dmaMapRule, _enableCountryCheck);
            
            //------- message send to unity ----------
            Debug.Log($"{Tag} get callback msg:\n{msg}");

            var result = JsonConvert.DeserializeObject<ConsentResult>(msg);
            
            if (result != null && result.action == "gdpr")
            {
                if (result.data != null)
                {
                    Debug.Log($"{Tag} ---  status: {result.data.status}    msg: {result.data.msg}");
                    onCompleteHandler?.Invoke(result.data.status);
                    return;
                }
            }
           
            Debug.LogError($"{Tag} Parse callback Error");
            onCompleteHandler?.Invoke(StatusCode.UNKNOWN);
        }

        /// <summary>
        /// 上报异常
        /// </summary>
        /// <param name="ex"></param>
        public static void LogException(Exception ex)
        {
            Analytics.LogCrashlytics(ex);
        }

        #endregion
        
        #region 常量定义
        
        /// <summary>
        /// GDPR 状态对象
        /// </summary>
        [Serializable]
        internal class ConsentResult
        {
            public string action = "";
            public ConsentStatus data = null;
        }
        
        /// <summary>
        /// GDPR 状态对象
        /// </summary>
        [Serializable]
        internal class ConsentStatus
        {
            public int status;
            public string msg;
        }
        
        /// <summary>
        /// Consent 状态
        /// </summary>
        public static class StatusCode
        {
            public const int NOT_AVAILABLE = -100;
            public const int NOT_REQUIRED = 1;
            public const int OBTAINED = 3;
            public const int REQUIRED = 2;
            public const int UNKNOWN = 0;
        }

        /// <summary>
        /// DEBUG地理信息
        /// </summary>
        public static class DebugGeography
        {
            public const int DEBUG_GEOGRAPHY_DISABLED = 0;
            public const int DEBUG_GEOGRAPHY_EEA = 1;
            public const int DEBUG_GEOGRAPHY_NOT_EEA = 2;
        }

        #endregion
        
    }
    
}
