package com.m2pool.pool.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.m2pool.common.core.utils.DateUtils;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;

import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @Description TODO
 * @Date 2025/4/3 10:21
 * @Author 杜懿
 */
public class SocketDemo {

    public static void main(String[] args) {
        //try {
        //    //Socket socket = new Socket("10.168.2.167",21233);
        //    Socket socket = new Socket("10.168.2.167",12973);
        //}catch (UnknownHostException e){
        //    System.out.println("无法找到主机: "+e.getMessage());
        //} catch (IOException e) {
        //    System.out.println("连接失败: " + e.getMessage());
        //}
        //String url = "http://10.168.2.167:12973/blockflow/chain-info?fromGroup=0&toGroup=0";
        //HttpHeaders headers = new HttpHeaders();
        //headers.add("Content-Type","application/json");
        //headers.add("User-Agent", "Mozilla/5.0");
        //headers.add("apikey","0x09e220e226f2feb7a971a2b6f958e7d4b1c187c8");
        //HttpEntity<String> httpEntity = new HttpEntity<String>(null, headers);
        //String s = HttpUtil.get(url);
        //
        //JSONObject jsonObject = JSONObject.parseObject(s);
        //int currentHeight = jsonObject.getIntValue("currentHeight");

        //Date endDate = DateUtils.getPreviousHalfHourOrFullHour(new Date());
        //Date startDate = DateUtils.getOneMonthAgo(endDate);
        //System.out.println(startDate+"  "+endDate);
        //String textToTranslate = "<p _msthidden=\"3\"><span style=\"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);\" _msttexthash=\"84072040\" _msthash=\"1610\">API 中 coin 字段可目前仅取值支持：nexa</span></p><p _msthidden=\"3\"><span id=\"step\"></span></p><h3 style=\"text-align: start;\" _msttexthash=\"11282648\" _msthash=\"1609\">矿池信息</h3><h3 style=\"text-align: start;\" _msthidden=\"3\"><span style=\"font-size: 16px;\" _msttexthash=\"11308739\" _msthash=\"1608\">公共结构</span></h3><p style=\"text-align: start;\" _msthidden=\"3\"><strong _msttexthash=\"7939243\" _msthash=\"1607\">哈希率</strong></p><p style=\"text-align: start;\" _msttexthash=\"11425713\" _msthash=\"1606\">算力数据</p><p style=\"text-align: start;\" _msthidden=\"3\"><br/></p><table style=\"width: auto;\" _msthidden=\"3\"><tbody><tr><th colspan=\"1\" rowspan=\"1\" width=\"120\" _msttexthash=\"5204511\" _msthash=\"1596\">名称</th><th colspan=\"1\" rowspan=\"1\" width=\"99.03\" _msttexthash=\"5230641\" _msthash=\"1597\">类型</th><th colspan=\"1\" rowspan=\"1\" width=\"117\" style=\"text-align: center;\" _msttexthash=\"4973501\" _msthash=\"1598\">备注</th><th colspan=\"1\" rowspan=\"1\" width=\"186\" style=\"text-align: center;\" _msttexthash=\"7093697\" _msthash=\"1599\">解释</th></tr><tr><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\"><font _mstmutation=\"1\" _msttexthash=\"5119231\" _msthash=\"1600\">日期</font><br/></td><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\" _msttexthash=\"5119231\" _msthash=\"1601\">日期</td><td colspan=\"1\" rowspan=\"1\" width=\"auto\"></td><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\" _msttexthash=\"23246444\" _msthash=\"1602\">算力统计时间</td></tr><tr><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\"><font _mstmutation=\"1\" _msttexthash=\"5078437\" _msthash=\"1603\">算力</font><br/></td><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\"><font _mstmutation=\"1\" _msttexthash=\"1952132\" _msthash=\"1604\">双</font><br/></td><td colspan=\"1\" rowspan=\"1\" width=\"auto\"></td><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\"><font _mstmutation=\"1\" _msttexthash=\"5078437\" _msthash=\"1605\">算力</font><br/></td></tr></tbody></table><p _msthidden=\"3\"><span id=\"MinersList\"></span></p><p _msthidden=\"3\"><span style=\"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);\" _msttexthash=\"10586056\" _msthash=\"1595\">矿工名单</span></p><p _msthidden=\"3\"><span style=\"color: rgb(0, 0, 0); background-color: rgb(255, 255, 255);\" _msttexthash=\"20870291\" _msthash=\"1594\">矿工数量数据</span></p><table style=\"width: auto;\" _msthidden=\"3\"><tbody><tr><th colspan=\"1\" rowspan=\"1\" width=\"148.7\" style=\"text-align: center;\" _msttexthash=\"5204511\" _msthash=\"1581\">名称</th><th colspan=\"1\" rowspan=\"1\" width=\"99\" style=\"text-align: center;\" _msttexthash=\"5230641\" _msthash=\"1582\">类型</th><th colspan=\"1\" rowspan=\"1\" width=\"111\" style=\"text-align: center;\" _msttexthash=\"4973501\" _msthash=\"1583\">备注</th><th colspan=\"1\" rowspan=\"1\" width=\"153\" style=\"text-align: center;\" _msttexthash=\"7093697\" _msthash=\"1584\">解释</th></tr><tr><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\" _msttexthash=\"2241785\" _msthash=\"1585\">总</td><td colspan=\"1\" rowspan=\"1\" width=\"auto\" style=\"text-align: center;\" _msttexthash=\"5064124\" _msthash=\"1586\">整数</td><td colspan=\"1\" rowspan=\"1\" width=\"auto\">"; // 替换为实际要翻译的文本
        //String apiKey = ""; // 替换为实际的 API 密钥
        //String response = translate(textToTranslate, apiKey);
        //System.out.println(response);


        System.out.println(LocalDateTime.now().toLocalDate().atStartOfDay());

    }


    /**
     * 发送翻译请求
     * @param text 待翻译的文本
     * @param apiKey API 密钥
     * @return 响应结果字符串
     */
    public static String translate(String text, String apiKey) {
        // 构建请求体
        JSONObject requestBody = new JSONObject();
        requestBody.put("q", text);
        requestBody.put("source", "auto");
        requestBody.put("target", "zh");
        requestBody.put("format", "text");
        requestBody.put("alternatives", 3);
        requestBody.put("api_key", apiKey);

        // 发送 POST 请求
        String result = HttpRequest.post("https://libretranslate.com/translate")
                .header("Content-Type", "application/json")
                .body(requestBody.toString())
                .execute()
                .body();

        return result;
    }
}
