package com.hh.lib_ampe.utils;

import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.hh.lib_ampe.bean.BaseAmpeRequestBean;
import com.hh.lib_base.log.AppLogUtils;
import com.hh.lib_base.utils.GsonUtils;

import java.lang.reflect.Type;

/**
 * Description: AMPE 入参 泛型转换类
 * @author xwh
 * time 2024/8/29
 **/
public class MyTypeToken  {

    public static  <T> BaseAmpeRequestBean<T> toBaseAmpeRequestBean(BaseAmpeRequestBean originBean, Class<T> kClazz){
        if(originBean==null){
            return null;
        }
        if(TextUtils.isEmpty(originBean.getSourceParams())){
            return null;
        }
        try {

            Type type = TypeToken.getParameterized(BaseAmpeRequestBean.class,kClazz).getType();
            BaseAmpeRequestBean<T> data= GsonUtils.getGson().fromJson(originBean.getSourceParams(), type);
            if(data!=null){
                data.setSourceParams(originBean.getSourceParams());
                data.setBridgeCallback(originBean.getBridgeCallback());
            }
            return data;

        }catch (Exception e){
            AppLogUtils.e("onCalled--t" + e);
        }

       return null;
    }
}
