package com.yutou.qqbot.utlis;

import net.mamoe.mirai.utils.BotConfiguration;

import java.lang.reflect.Field;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;

public class FixProtocolVersion {
    public static void fix(){
        try {

            Class<?> MiraiProtocolInternal = Class.forName("net.mamoe.mirai.internal.utils.MiraiProtocolInternal");
            Field field = MiraiProtocolInternal.getFields()[0];
            Object companion = field.get(Object.class);

            EnumMap<BotConfiguration.MiraiProtocol, Object> protocols = (EnumMap<BotConfiguration.MiraiProtocol, Object>)companion.getClass().getMethod("getProtocols$mirai_core").invoke(companion);
            Object pad = protocols.get(BotConfiguration.MiraiProtocol.ANDROID_PAD);
        /*
        * apkId: String,
            id: Long,
            ver: String,
            sdkVer: String,
            miscBitMap: Int,
            subSigMap: Int,
            mainSigMap: Int,
            sign: String,
            buildTime: Long,
            ssoVersion: Int,
            canDoQRCodeLogin: Boolean = false,
        * */
            Class<?> padClass = pad.getClass();
            Map<String, Object> padData = new HashMap<String, Object>(){{
                put("id", 537152242);
                put("ver", "8.9.35.10440");
                put("sdkVer", "6.0.0.2535");
                put("buildTime", 1676531414L);
            }};
            for (Field f : padClass.getFields()) {
                f.setAccessible(true);
                if(padData.containsKey(f.getName())){
                    f.set(pad, padData.get(f.getName()));
                }
                f.setAccessible(false);
            }

            Object phone = protocols.get(BotConfiguration.MiraiProtocol.ANDROID_PHONE);
            Map<String, Object> phoneData = new HashMap<String, Object>(){{
                put("id", 537153294);
                put("ver", "8.9.35.10440");
                put("sdkVer", "6.0.0.2535");
                put("buildTime", 1676531414L);
            }};
            for (Field f : padClass.getFields()) {
                f.setAccessible(true);
                if(padData.containsKey(f.getName())){
                    f.set(phone, phoneData.get(f.getName()));
                }
                f.setAccessible(false);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

