package com.yutou.qqbot.utlis;

import com.alibaba.fastjson2.JSONObject;
import com.yutou.qqbot.QQBotManager;
import com.yutou.qqbot.bilibili.BiliBiliUtils;

import java.nio.charset.StandardCharsets;
import java.util.TreeMap;

public class BiliBiliWbiSign {
    private static final byte[] MIXIN_KEY_ENC_TAB = {
            46, 47, 18, 2, 53, 8, 23, 32, 15, 50, 10, 31, 58, 3, 45, 35, 27, 43, 5, 49, 33, 9, 42,
            19, 29, 28, 14, 39, 12, 38, 41, 13, 37, 48, 7, 16, 24, 55, 40, 61, 26, 17, 0, 1, 60,
            51, 30, 4, 22, 25, 54, 21, 56, 59, 6, 63, 57, 62, 11, 36, 20, 34, 44, 52
    };
    private static String rawWbiKey;

    public static TreeMap<String, String> getWbiSign(TreeMap<String, String> body) {
        if (rawWbiKey == null) {
            updateRawWbiKey();
            try {
                Thread.sleep(300);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return getWbiSign(body);
        }
        body.put("wts", String.valueOf(System.currentTimeMillis() / 1000));
        String params = HttpTools.toUrlParams(body);
        System.out.println("params = " + params);
        body.put("w_rid", AppTools.getMD5(params + genMixinKey(rawWbiKey)));
        return body;
    }

    private static String genMixinKey(String rawWbiKey) {
        byte[] rawBytes = rawWbiKey.getBytes(StandardCharsets.UTF_8);
        byte[] mixinKey = new byte[32];

        for (int i = 0; i < 32; i++) {
            mixinKey[i] = rawBytes[MIXIN_KEY_ENC_TAB[i]];
        }
        return new String(mixinKey);
    }

    public static void updateRawWbiKey() {
        JSONObject loginInfo = new BiliBiliUtils(QQBotManager.defQQ).getLoginInfo();
        if (loginInfo.getInteger("code") == -1) {
            rawWbiKey = null;
            return;
        }
        JSONObject wbi = loginInfo.getJSONObject("data").getJSONObject("wbi_img");
        String imgKey = wbi.getString("img_url");
        String subKey = wbi.getString("sub_url");
        imgKey = imgKey.substring(imgKey.lastIndexOf("/") + 1).replace(".png", "");
        subKey = subKey.substring(subKey.lastIndexOf("/") + 1).replace(".png", "");
        rawWbiKey = imgKey + subKey;
        System.out.println(rawWbiKey);
    }

    public static void main(String[] args) throws Exception {
        //System.out.println(genMixinKey("7cd084941338484aae1ad9425b84077c4932caff0ff746eab6f01bf08b70ac45"));
        rawWbiKey = "7cd084941338484aae1ad9425b84077c4932caff0ff746eab6f01bf08b70ac45";
        TreeMap<String, String> json = new TreeMap<>();
        json.put("bvid", "BV1L94y1H7CV");
        json.put("cid", "1335073288");
        json.put("up_mid", "297242063");
        json.put("web_location", "333.788");
        //updateRawWbiKey();
        TreeMap<String, String> sign = getWbiSign(json);
        System.out.println(sign);
//https://api.bilibili.com/x/web-interface/view/conclusion/get?bvid=BV1L94y1H7CV&cid=1335073288&up_mid=297242063&web_location=333.788&w_rid=a5d90f60ac6b6b6fc9d49be3ba3fee53&wts=1705394671
        //updateRawWbiKey();
    }
}
