package cn.yyxx.commsdk.base;

import android.content.Context;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * @author #Suyghur.
 * Created on 2021/12/25
 */
final class SdkBridgeManager {
    static Object sdkBridge = null;
    static Class<?> clzBridge = null;

    public static Object initSdkBridgeManager(Context context) {
        if (sdkBridge != null) {
            return sdkBridge;
        }

        try {
            clzBridge = Class.forName("cn.yyxx.commsdk.core.SdkBridge");
            //获取构造函数的构造器
            Constructor<?> constructor = clzBridge.getDeclaredConstructor(Context.class);
            sdkBridge = constructor.newInstance(context);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sdkBridge;
    }

    public static void call(String methodName, Class<?>[] types, Object[] values) {
        if (sdkBridge == null) {
            return;
        }
        try {

            Method method = clzBridge.getMethod(methodName, types);
            method.invoke(sdkBridge, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object callback(String methodName, Class<?>[] types, Object[] values) {
        if (sdkBridge == null) {
            return null;
        }
        try {
            Method method = clzBridge.getMethod(methodName, types);
            return method.invoke(sdkBridge, values);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
