using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace AD.VosacoSDK
{
    public class VosacoAdSDK
    {
        private static AndroidJavaClass _adClass;

        // 初始化SDK
        public static void Initialize(string host, string token, string sid, string appId, Action<bool, string> callback)
        {
            if (Application.platform != RuntimePlatform.Android)
            {
                LogD("SDK only works on Android platform");
                return;
            }

            try
            {
                // 创建Java回调对象
                var initCallback = new ISDKInitCallback
                {
                    onInitResult = callback
                };

                // 调用Java方法
                GetAdClass().CallStatic("init", host, token, sid, appId, initCallback);
            }
            catch (Exception e)
            {
                LogE($"Initialize failed: {e.Message}");
            }
        }

        // 其他接口封装
        public static void SetDebug(bool enable) => GetAdClass().CallStatic("setDebug", enable);

        public static void SetGDPRConsent(bool consent) => GetAdClass().CallStatic("setSubjectToGDPR", consent);

        public static void SetUserConsent(string consent) => GetAdClass().CallStatic("setUserConsent", consent);

        public static void SetUSPrivacy(string privacy) => GetAdClass().CallStatic("subjectToUSPrivacy", privacy);

        public static void SetExtraParameters(Dictionary<string, object> parameters)
        {
            using AndroidJavaObject map = new AndroidJavaObject("java.util.HashMap");
            foreach (var kv in parameters)
            {
                map.Call<AndroidJavaObject>("put", kv.Key, kv.Value);
            }
            GetAdClass().CallStatic("setExtraParameters", map);
        }

        // 获取网络信息
        public static string GetNetworkName() => GetAdClass().CallStatic<string>("getNetWorkName");
        public static string GetNetworkVersion() => GetAdClass().CallStatic<string>("getNetWorkVersion");

        public static void LogD(string msg)
        {
            GetAdClass().CallStatic("LogD", msg);
        }

        public static void LogE(string msg)
        {
            GetAdClass().CallStatic("LogE", msg);
        }

        private static AndroidJavaClass GetAdClass()
        {
            if (_adClass == null)
            {
                _adClass = new AndroidJavaClass("com.rixengine.unity_plugin.RiEngineAd");
            }
            return _adClass;
        }
    }
}
