package cn.yyxx.eyuancomm.comm.impl

import android.content.Context
import java.lang.reflect.Method

/**
 * @author #Suyghur.
 * Created on 2021/06/12
 */
object SdkProxyManager {

    var sdkProxy: Any? = null
    var clzProxy: Class<*>? = null

    fun initSdkProxyManager(context: Context): Any? {
        if (sdkProxy != null) {
            return sdkProxy
        }

        try {
            clzProxy = Class.forName("cn.yyxx.eyuancomm.impl.ImplSdkProxy")
            //获取构造函数的构造器
            val constructor = clzProxy!!.getDeclaredConstructor(Context::class.java)
            sdkProxy = constructor.newInstance(context)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return sdkProxy
    }

    fun call(methodName: String, types: Array<Class<*>?>, values: Array<Any?>) {
        try {
            clzProxy?.apply {
                val method: Method = this.getMethod(methodName, *types)
                method.invoke(sdkProxy, *values)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }


    fun callback(methodName: String, types: Array<Class<*>?>, values: Array<Any?>): Any? {
        try {
            clzProxy?.let {
                val method: Method = it.getMethod(methodName, *types)
                return method.invoke(sdkProxy, *values)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return null
    }
}