package org.jebot.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.http.HttpHeaders;


public class PriceUtil {

    private static final String url = "https://www.okx.com/v3/c2c/tradingOrders/books?quoteCurrency=CNY&baseCurrency=USDT&side=sell&paymentMethod=%s&userType=all&receivingAds=false&t=%d";

    public static String QueryUsdtToCny(QueryType queryType) {
        // 将价格格式化为两位小数
        HttpRequest request = HttpUtil.createGet(String.format(url, queryType.getValue(), System.currentTimeMillis() / 1000));
        // 设置请求头
        request.header(HttpHeaders.HOST, "www.okx.com");
        request.header("sec-ch-ua", "\"Chromium\";v=\"136\", \"Microsoft Edge\";v=\"136\", \"Not.A/Brand\";v=\"99\"");
        request.header("sec-ch-ua-mobile", "?0");
        request.header("sec-ch-ua-platform", "\"Windows\"");
        request.header(HttpHeaders.USER_AGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0");
        request.header(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        request.header("Service-Worker-Navigation-Preload", "true");
        request.header("Sec-Fetch-Site", "none");
        request.header("Sec-Fetch-Mode", "navigate");
        request.header("Sec-Fetch-User", "?1");
        request.header("Sec-Fetch-Dest", "document");
        request.header(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, br, zstd");
        request.header(HttpHeaders.ACCEPT_LANGUAGE, "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        HttpResponse response = request.execute();
        if (response.getStatus() != 200) {
            return "请求失败";
        }
        String body = response.body();
        if (Strings.isEmpty(body)) {
            return "请求失败";
        }

        JSONObject result = JSONUtil.parseObj(body);
        String code = result.getStr("code");
        if (!"0".equals(code)) {
            return "请求失败";
        }
        JSONObject data = JSONUtil.parseObj(result.getStr("data"));
        JSONArray sell = JSONUtil.parseArray(data.getStr("sell"));
        StringBuffer buffer = new StringBuffer();
        buffer.append("欧易筛选: ");
        if (queryType == QueryType.ALIPAY) {
            buffer.append("支付宝\r\n");
        } else if (queryType == QueryType.WECHAT) {
            buffer.append("微信\r\n");
        } else if (queryType == QueryType.BANK) {
            buffer.append("银行\r\n");
        }
        int count = 0;
        for (Object o : sell) {
            if (count >= 10) {
                return buffer.toString();
            }
            JSONObject jsonObject = JSONUtil.parseObj(o);
            buffer.append(jsonObject.getStr("price"));
            buffer.append("        ");
            buffer.append(jsonObject.getStr("nickName"));
            buffer.append("\r\n");
            count++;
        }
        return buffer.toString();
    }
}
