using System.Threading.Tasks;
using UnityEngine;

namespace tysdk
{
    public partial class TYSdkFacade : MonoBehaviour
    {
        /*================================================

                                _  _____ _____ 
                               / \|_   _|_   _|
                              / _ \ | |   | |  
                             / ___ \| |   | |  
                            /_/   \_\_|   |_|  
                                               
      =================================================*/

        public bool IsAttAccepted()
        {
            return UnityBridgeFunc.GetATT() == 1;
        }

        public async Task<ATTInfo> RequestATT()
        {
#if UNITY_EDITOR || UNITY_ANDROID
            await Task.Yield();

            return new ATTInfo(){isAccepted = true};
#elif UNITY_IOS
            var taskSource = new TaskCompletionSource<ATTInfo>();
            callbacks.Add("RequestATT", (ITYSdkCallback callback) =>
            {
                taskSource.SetResult((ATTInfo)callback);
            });
            UnityBridgeFunc.RequestATT();
            return await taskSource.Task;
#endif
        }

        public void RequestATTResult(string r)
        {
            if (callbacks.TryGetValue("RequestATT", out var action))
            {
                action.Invoke(new ATTInfo(){isAccepted = r == "1"});
                callbacks.Remove("RequestATT");
            }
        }
    }
}