package com.fear1ess.reyunaditool.pangle;

import android.text.TextUtils;
import android.util.Log;

import com.fear1ess.reyunaditool.MLog;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

import static com.fear1ess.reyunaditool.adsfinder.Finder.TAG;
//--------------------------------------------------测试代码
public class pangleModule {
    private static String m_appID="";
    public static void hook_AppID(XC_LoadPackage.LoadPackageParam lpparam){

        XposedHelpers.findAndHookMethod("com.bytedance.sdk.openadsdk.TTAdConfig",
                lpparam.classLoader, "setAppId",String.class,
                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        m_appID= (String) param.args[0];
                        MLog.d( "测试：beforeHookedMethod: "+m_appID);
                    }
                }
        );
    }


    public static void hook_SlotID(XC_LoadPackage.LoadPackageParam lpparam){
        XposedHelpers.findAndHookMethod("com.bytedance.sdk.openadsdk.AdSlot$Builder",
                lpparam.classLoader, "setCodeId",String.class,
                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        String slotID= (String) param.args[0];
                        Map<String,String> map = new HashMap<>();
                        if(TextUtils.isEmpty(m_appID)){
                            m_appID=lpparam.packageName;
                        }
                        map.put("appID", m_appID);
                        map.put("slotID", slotID);
                        MLog.d("测试：beforeHookedMethod: "+map.toString());
                    }
                }
        );
    }


    public static void hook_doFinal(XC_LoadPackage.LoadPackageParam lpparam){
        XposedHelpers.findAndHookMethod("javax.crypto.Cipher",
                lpparam.classLoader, "doFinal",byte[].class,
                new XC_MethodHook() {
                    @Override
                    protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                        super.beforeHookedMethod(param);
                        try{
                            String content= new String((byte[]) param.args[0]);
                            if(content.contains("\"adslots\":[{\"id\":\"")){
//                                MLog.d(content);
                                JSONObject jsonObject=new JSONObject(content);
                                String ad_sdk_version=(String) jsonObject.get("ad_sdk_version");
                                JSONObject app= (JSONObject) jsonObject.get("app");
                                String appid= (String) app.get("appid");
                                String package_name= (String) app.get("package_name");
                                String version_code= (String) app.get("version_code");
                                String version= (String) app.get("version");

                                JSONArray adslots= (JSONArray) jsonObject.get("adslots");
//                                MLog.d("adslots-> "+adslots.toString());
                                JSONArray my_Adslots=new JSONArray();
                                for (int i = 0; i < adslots.length(); i++) {
                                    JSONObject item= (JSONObject) adslots.get(i);
                                    JSONObject object= new JSONObject();
                                    object.put("id",item.get("id"));
                                    object.put("adtype",item.get("adtype"));
                                    my_Adslots.put(object);
                                }

                                Map<String,String> map = new HashMap<>();
                                if(TextUtils.isEmpty(m_appID)){
                                    m_appID=lpparam.packageName;
                                }
                                map.put("appID", appid);
                                map.put("ad_sdk_version", ad_sdk_version);
                                map.put("package_name", package_name);
                                map.put("version_code", version_code);
                                map.put("version", version);
                                map.put("slotID", my_Adslots.toString());
                                MLog.d("测试：beforeHookedMethod: "+map.toString());
                            }
                        }catch (Exception e){
                           MLog.d(e.toString());
                        }


                    }
                }
        );
    }
}
