using System.Collections;
using UnityEngine;
using AppsFlyerSDK;
using System.Collections.Generic;
using Unity.Advertisement.IosSupport;
using BingoBrain;
using BingoBrain.Core;
using System;
using DG.Tweening;
using BingoBrain.HotFix;

public class AppsFlyerObjectScript1 : MonoBehaviour, IAppsFlyerConversionData
{
    public string appID = null;
    public bool is_init = false;
    public Coroutine m_Coroutine;

    void Start()
    {
        AddListener();

        AppsFlyer.setIsDebug(true);
#if UNITY_IOS && !UNITY_EDITOR
        appID = "6557062195";
        m_Coroutine = IsfvKit.StartCoroutine(loopWaitInitAf());

#endif


#if UNITY_EDITOR
        NetworkDispatcher.Instance.Dispatch(NetworkMsg.Login);
#endif


    }

     private void AddListener()
        {
            NetworkDispatcher.Instance.AddListener(NetworkMsg.NotNetwork, RequestLogin);
        }

        private void RemoveListener()
        {
            NetworkDispatcher.Instance.RemoveListener(NetworkMsg.NotNetwork, RequestLogin);
        }
        void RequestLogin(object obj = null)
        {
            if (GameHelper.IsConnect())
            {
                GameDispatcher.Instance.Dispatch(BingoInfo.Network_reconnection);
            }

            IsfvKit.StopCoroutine(m_Coroutine);

            m_Coroutine = IsfvKit.StartCoroutine(loopWaitInitAf());
        }

       void OnDestroy() {
            RemoveListener();
        }


    IEnumerator loopWaitInitAf()
    {
        if (!GameHelper.IsConnect())
        {
            Action action = () =>
            {
                DOVirtual.DelayedCall(0.5f, ()=>{
                    NetworkDispatcher.Instance.Dispatch(NetworkMsg.NotNetwork);
                });
            };
            UICtrlDispatcher.Instance.Dispatch(UICtrlMsg.TipsViewUI_Open,action);
            yield return null;
        }
            
        GameHelper.PostFunnelLogin("afSend");
        float a = 0;
        if (ATTrackingStatusBinding.GetAuthorizationTrackingStatus() ==
            ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED)
        {
            ATTrackingStatusBinding.RequestAuthorizationTracking();
        }


        while ((ATTrackingStatusBinding.GetAuthorizationTrackingStatus() ==
                ATTrackingStatusBinding.AuthorizationTrackingStatus.NOT_DETERMINED) && (a < 5.0f))
        {
            a += 0.5f;
            yield return new WaitForSeconds(0.5f);
        }

        AppsFlyer.initSDK("ds3rwfCqqVQYHAwt6ZJQSg", appID, this);
        AppsFlyer.startSDK();
        AppsFlyer.AFLog("8888888888888888888", ATTrackingStatusBinding.GetAuthorizationTrackingStatus().ToString());
        //yield return new WaitForSeconds(0.5f);

    }

    public void onConversionDataSuccess(string conversionData)
    {
        if (is_init) return;
        is_init = true;
        AppsFlyer.AFLog("onConversionDataSuccess", conversionData);
        var conversionDataDictionary = AppsFlyer.CallbackStringToDictionary(conversionData);
        var json = SerializeUtil.ToJsonIndented(conversionDataDictionary);

        BingoBea.Instance.attribution =
            conversionDataDictionary.TryGetValue("af_status", out var afStatus)
                ? afStatus.ToString().ToLower()
                : "organic";
        NetworkDispatcher.Instance.Dispatch(NetworkMsg.Login);
        GameHelper.PostFunnelLogin("afRecv", true);
    }

    public void onConversionDataFail(string error)
    {
        if (is_init) return;
        is_init = true;
        AppsFlyer.AFLog("onConversionDataFail", error);
        NetworkDispatcher.Instance.Dispatch(NetworkMsg.Login);
        GameHelper.PostFunnelLogin("afRecv", false);
    }

    public void onAppOpenAttribution(string attributionData)
    {
        AppsFlyer.AFLog("onAppOpenAttribution", attributionData);
        Dictionary<string, object> attributionDataDictionary =
            AppsFlyer.CallbackStringToDictionary(attributionData);
    }

    public void onAppOpenAttributionFailure(string error)
    {
        AppsFlyer.AFLog("onAppOpenAttributionFailure", error);
    }
}
 