using System;
using AnyThinkAds.Api;
using Runtime.ADAggregator;
using UnityEngine;

public class ToponAdController : IAdController
{
    private Action<bool> _maskAction;
    private Action<string, string> _logEventAction;

    private ADConfig _adConfig;
    
    public void Init(ADConfig adConfig, object[] args)
    {
        _adConfig = adConfig;
        // ATSdkUtil.
        ATSDKAPI.setChannel(args[0].ToString());
        var isDebug = args.Length        > 1 && (bool)args[1];
        ATSDKAPI.setLogDebug(isDebug);
        ATSDKAPI.initSDK(adConfig.Id , adConfig.Key);
        if (isDebug)
        {
            ShowAndroidTest ();
        }
    }

    public void SetListener(Action<bool> adMaskAction ,Action<string, string> logEventAction)
    {
        _maskAction = adMaskAction;
        _logEventAction = logEventAction;
    }

    public ADPlayer CreateAdPlayer(AD_Type type)
    {
        switch (type)
        {
            case AD_Type.AwardVideo:
                var awardVideoPlayer = new AwardVideoPlayer();
                return awardVideoPlayer.Init(_adConfig.BaseAwardAdKeyValue.value);
            case AD_Type.Splash:
                break;
            case AD_Type.Interaction:
                var interactionPlayer = new InteractionPlayer();
                return interactionPlayer.Init(_adConfig.BaseInteractionAdKeyValue.value);
        }
        return null;
    }

    public void EventLog(string eventTable, string eventValue, string eventMessage = null)
    {
        _logEventAction?.Invoke(eventTable , eventValue);
    }

    public void SetMask(bool isOpen)
    {
        _maskAction?.Invoke(isOpen);
    }

    private void ShowAndroidTest ()
    {
        ATSDKAPI.showDebuggerUI ();
        // com.anythink.debug.api.ATDebuggerUITest.showDebuggerUI(this);
// #if UNITY_EDITOR
//         return;
// #elif UNITY_ANDROID
//             //获取Unity的Activity Class
//             using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
//             {
//                 //获取对应的实例化对象，这两句都是固定写法
//                 using (AndroidJavaObject activityContext = activityClass.GetStatic<AndroidJavaObject>("currentActivity"))
//                 {
//                     //拿到我自己的工具类并实例化
//                     var testUtils = new AndroidJavaClass("com.anythink.debug.api.ATDebuggerUITest");
//                     //向工具类里的Init方法传入Unity的activity对象用于初始化工具类
//                     testUtils.CallStatic("showDebuggerUI", activityContext);
//                 }
//             }
// #endif
    }
}