using System;
using System.Reflection;
using AppsFlyerSDK;
using UnityEngine;
using UnityEngine.UI;

public class OneLinkTest : MonoBehaviour
{

    [SerializeField] private RootCtx ctx;

    [SerializeField] private GameObject btnContainer;

    [SerializeField] private GameObject btn;

    [SerializeField] private Text message;

    void Start()
    {
        ctx.OnInited += OnContextInited;
    }

    // Callback from GContex when its ready
    private void OnContextInited()
    {
        Boost();
    }

    private async void Boost()
    {
        await Awaiters.NextFrame;

        TaInit();
        InitAppsFlyer();

        var aFInit = new AFInit();
        aFInit.text = message;
        aFInit.text.text = $"Init AF {AppsFlyer.CallBackObjectName}\n";;

        var methods = aFInit.GetType().GetMethods();
        foreach (var method in methods)
        {
            var btnAttr = method.GetCustomAttribute<BtnAttribute>();
            if (btnAttr == null)
            {
                continue;
            }
            var btnObj = Instantiate(btn, btnContainer.transform);
            btnObj.GetComponentInChildren<Text>().text = btnAttr.btnName;
            btnObj.GetComponent<Button>().onClick.AddListener(() => method.Invoke(aFInit, null));
            btnObj.SetActive(true);
        }

    }


    private static void TaInit()
    {
        var taAppId = GConstant.TA_APPID;
        var taUrl = GConstant.TA_URL;
        var autoTrackEventType = ThinkingData.Analytics.TDAutoTrackEventType.AppStart 
            | ThinkingData.Analytics.TDAutoTrackEventType.AppEnd 
            | ThinkingData.Analytics.TDAutoTrackEventType.AppInstall;

        ThinkingData.Analytics.TDAnalytics.Init(taAppId, taUrl);
        ThinkingData.Analytics.TDAnalytics.EnableThirdPartySharing(ThinkingData.Analytics.Utils.TDThirdPartyType.APPSFLYER);
        ThinkingData.Analytics.TDAnalytics.EnableAutoTrack(autoTrackEventType);
        ThinkingData.Analytics.TDAnalytics.EnableLog(false);

        Debug.Log("[GEvent::TaInit] ThinkingData inited");
    }

    private static void InitAppsFlyer()
    {
        var afKey = GConstant.AF_KEY;
        var afAppID = GConstant.AF_APPID;
        var afOneLinkID = GConstant.AF_OneLinkID;
        var afCustomerUserID = ThinkingData.Analytics.TDAnalytics.GetDistinctId();
        AFDeepLinkHelper.Instance.Init(afKey,afAppID,afCustomerUserID,afOneLinkID,true);
        Debug.Log("[GEvent::InitAppsFlyer] AppsFlyer inited");
    }
}

public class GConstant
{
    public const string TA_URL = "https://ta-event.bamboogames.fun";
    public const string TA_APPID = "9876bb56ee484b528064d1db40a57ec4";

    public const string AF_KEY = "rSySeWtvKabfbPZE7Lmx7C";
    public const string AF_APPID = "6505145935";
    public const string AF_OneLinkID = "jT1Q";
}


[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class BtnAttribute : Attribute
{
    public string btnName;
    public BtnAttribute(string btnName)
    {
        this.btnName = btnName;
    }
}
