package org.jeecg.modules.utils;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class SmsClient {

    private static final String SMS_API_URL = "http://agent.izjun.com:8001/sms/api/sendMessageOne";

    public static  String send(String phone,String code) {
        SmsClient client = new SmsClient();
        String response = client.sendSms("tairoudj", Arrays.asList(
                new Message(phone, "【泰柔到家】您的验证码为："+code)
        ), System.currentTimeMillis(), "evXRctaIoX22");
        System.out.println(response);
        return response;
    }

    public  String sendSms(String userName, Iterable<Message> messages, long timestamp, String password) {
        HttpClient client = HttpClients.createDefault();
        HttpPost postRequest = new HttpPost(SMS_API_URL);

        try {
            Map<String, Object> payload = new HashMap<>();
            payload.put("userName", userName);
            payload.put("messageList", messages);
            payload.put("timestamp", timestamp);
            payload.put("sign", calculateSign(userName, timestamp, password));

            String json = new com.google.gson.Gson().toJson(payload);

            StringEntity entity = new StringEntity(json, StandardCharsets.UTF_8);
            postRequest.setHeader("Accept", "application/json");
            postRequest.setHeader("Content-Type", "application/json;charset=utf-8");
            postRequest.setEntity(entity);

            HttpResponse response = client.execute(postRequest);
            return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);

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

//    private String calculateSign(String userName, long timestamp, String password) throws Exception{
//        String toHash = userName + timestamp + java.security.MessageDigest.getInstance("MD5").digest(password.getBytes()).toString();
//        return java.security.MessageDigest.getInstance("MD5").digest(toHash.getBytes()).toString();
//    }

    static class Message {
        public String phone;
        public String content;

        public Message(String phone, String content) {
            this.phone = phone;
            this.content = content;
        }
    }


    public static String calculateSign(String userName, long timestamp, String password) {
        try {
            // 计算密码的MD5值
            String md5Password = md5(password);

            // 组合字符串
            String combinedString = userName + timestamp + md5Password;

            // 计算组合字符串的MD5值
            return md5(combinedString);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5 algorithm not found", e);
        }
    }

    private static String md5(String input) throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digest = md.digest(input.getBytes());
        return bytesToHex(digest);
    }

    private static String bytesToHex(byte[] bytes) {
        StringBuilder hexString = new StringBuilder();
        for (byte b : bytes) {
            String hex = Integer.toHexString(0xff & b);
            if (hex.length() == 1) {
                hexString.append('0');
            }
            hexString.append(hex);
        }
        return hexString.toString();
    }

}
