package com.jxy.marketall.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.jxy.common.core.utils.DateUtils;
import com.jxy.marketall.dto.CirculatingSupplyDataDto;
import com.jxy.marketall.task.CSupplyTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.jxy.common.core.utils.DateUtils.getZeroTimeOfToday;

/**
 * @Description TODO
 * @Date 2025/5/16 09:42
 * @Author yyb
 */
public class TestCoingckoApi {

    private static final Logger log = LoggerFactory.getLogger(CSupplyTask.class);
    public static void main(String[] args) {
        //String id = "binance-usd";
        //HttpHeaders headers = new HttpHeaders();
        //headers.add("Content-Type","application/json");
        //headers.add("x-cg-demo-api-key","CG-kNuNBTKBcQREBSQKqgj2uua4");
        //headers.add("User-Agent", "Mozilla/5.0");
        //HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
        //StringBuffer paramsURL = new StringBuffer(API_URL+id);
        //String s = HttpUtil.get(paramsURL.toString());
        //
        //JSONObject jsonObject = JSONObject.parseObject(s);
        ////币种流通量字段
        //JSONObject marketData = jsonObject.getJSONObject("market_data");
        //Date date = null;
        //try {
        //    //币种流通量最后更新时间
        //    date = (Date) marketData.get("last_updated");
        //}catch (Exception e){
        //
        //}
        //double doubleValue = marketData.getDoubleValue("circulating_supply");
        //List<CirculatingSupplyDataDto> circulatingSupplyDataDtos = new ArrayList<>();
        //CirculatingSupplyDataDto circulatingSupplyDataDto = new CirculatingSupplyDataDto();
        //circulatingSupplyDataDto.setValue(BigDecimal.valueOf(doubleValue));
        //circulatingSupplyDataDto.setDate(date);
        //circulatingSupplyDataDtos.add(circulatingSupplyDataDto);
        //log.info("获取{}币流通量数据成功,最新时间{},流通量{}", id,  date, doubleValue);


        try {
            // 配置 HTTP 代理
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7897));

            // 创建 URL 对象
            URL url = new URL("https://api.coingecko.com/api/v3/coins/binance-usd");

            // 打开连接并设置代理
            HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);

            // 设置请求方法
            connection.setRequestMethod("GET");

            // 获取响应码
            int responseCode = connection.getResponseCode();
            System.out.println("Response Code: " + responseCode);

            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();
            JSONObject jsonObject = JSONObject.parseObject(response.toString());
            JSONObject marketData = jsonObject.getJSONObject("market_data");
            double doubleValue = marketData.getDoubleValue("circulating_supply");
            //String dateString = (String) marketData.get("last_updated");
            // 输出响应内容
            System.out.println("doubleValue: " + doubleValue + "date:"+getZeroTimeOfToday());

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

    private static final String[] COINS = {"tether", "usd-coin", "binance-usd"};
    private static final String[] SYMBOLS = {"USDT", "USDC", "BUSD"};
    private static final String API_URL = "https://api.coingecko.com/api/v3/coins/";


}
