namespace Guru.Notification
{
    using UnityEngine;
    using System;
    
    
    
    /// 
    /// 消息管理器
    /// 
    public class NotificationService
    {
        // 服务版本号
        public const string Version = "0.0.1";
        // 初始化标志位
        private static bool _initOnce;
        private static string DEFAULT_USER_STATUS = "not_determined";
        #region 初始化
        private static INotificationAgent _agent;
        internal static INotificationAgent Agent
        {
            get
            {
                if (_agent == null)
                {
                    _agent = GetAgent();
                }
                return _agent;
            }
        }
        private static INotificationAgent GetAgent()
        {
#if UNITY_EDITOR
            return new NotificationAgentStub();
#elif UNITY_ANDROID
            return new NotificationAgentAndroid();
#elif UNITY_IOS
            return new NotificationAgentIOS();
#endif
            return null;
        }
        
        /// 
        /// 初始化
        /// 
        public static void Initialize()
        {
            if (_initOnce) return;
            _initOnce = true;
            Agent?.Init(); // 初始化代理
        }
        
        
        
        #endregion
        #region 接口
        
        /// 
        /// 拉起 Noti 请求
        /// 
        /// 
        public static void RequestPermission(Action callback = null)
        {
            if (Agent != null)
            {
                Agent.RequestPermission(callback);
                return;
            }
            Debug.LogError($"[SDK][Noti] --- Agent is missing, return default status: {DEFAULT_USER_STATUS}");
            callback?.Invoke(DEFAULT_USER_STATUS);
        }
        
        public static bool IsPermissionGranted()
        {
            return Agent?.IsAllowed() ?? false;
        }
        public static string GetStatus()
        {
            if(!_initOnce) Initialize();
            
            if(Agent != null) return Agent.GetStatus();
            
            Debug.LogError($"[SDK][Noti] --- Agent is missing, return default status: {DEFAULT_USER_STATUS}");
            return DEFAULT_USER_STATUS;
        }
        #endregion
        
    }
}