package cn.yyxx.commsdk.core.impl;

import android.content.Context;

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

import cn.yyxx.commsdk.base.utils.Logger;

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

    public static Object initSdkProxyManager(Context context) {
        if (sdkProxy != null) {
            return sdkProxy;
        }

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

        return sdkProxy;
    }

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

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

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