package com.ruoyi.app.utils.zaloPay;

import com.ruoyi.app.utils.zaloPayCrypto.RSAUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

/**
 * 支出（取现）
 */
@Component
public class ZalopayDisbursement {

    public static long getCurrentTime() {
        Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT+7"));
        return cal.getTimeInMillis();
    }

    public static String  verifyAccount(String phone,long amount) throws Exception {
//        ZaloPayConfig zaloPayConfig = ZaloPayConfig.getZaloPayConfig("3");
//        String url = "https://sb-openapi.zalopay.vn//v2/disbursement/verify-account";
//        Map<String, Object> map = new HashMap<>();
//        map.put("app_id", zaloPayConfig.getApp_id());
//        map.put("disbursement_type", "WALLET");
//        map.put("phone", phone);
//        map.put("amount", amount);
//
//        String recieve_info = "{\"phone\":\"" + phone + "\"}";
//
//        String cryp_recieve_info = RSAUtil.encrypt(zaloPayConfig.getKey1(), recieve_info);
//        map.put("recieve_info", cryp_recieve_info);
//        map.put("time", getCurrentTime());
//        String data = map.get("app_id") + "|" + map.get("disbursement_type") + "|" + map.get("recieve_info") + "|" + map.get("amount")
//                + "|" + map.get("time");
//        map.put("mac", RSAUtil.encrypt( zaloPayConfig.getKey1(), data));
//        HttpPost post = ZaloPay.createPost(map, url);
//
//        // 添加日志和异常处理
//        try (CloseableHttpClient httpClient = HttpClients.createDefault();
//             CloseableHttpResponse res = httpClient.execute(post)) {
//
//            BufferedReader rd = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
//            StringBuilder resultJsonStr = new StringBuilder();
//            String line;
//
//            while ((line = rd.readLine()) != null) {
//                resultJsonStr.append(line);
//            }
//            String responseJson = resultJsonStr.toString();
//            System.out.println("Zalopay 验证账号输出: " + responseJson); // 日志输出
            return "";
//        } catch (Exception e) {
//            System.err.println("Zalopay 验证账号输出 Error: " + e.getMessage()); // 错误日志输出
//            throw e;
//        }
    }

}
