package com.jeesharp.business.modules.pay.tlinx.test;

import com.jeesharp.business.modules.pay.tlinx.core.TLinx2Config;
import com.jeesharp.business.modules.pay.tlinx.dto.TLinx2PayOrderRequest;
import com.jeesharp.business.modules.pay.tlinx.dto.TLinx2PayOrderResponse;
import com.jeesharp.business.modules.pay.tlinx.dto.TLinx2PayStatusRequest;
import com.jeesharp.business.modules.pay.tlinx.dto.TLinx2RefundRequest;
import com.jeesharp.business.modules.pay.tlinx.service.TLinx2PayService;
import com.jeesharp.business.modules.pay.tlinx.service.impl.TLinx2PayServiceImpl;
import com.jeesharp.business.modules.pay.tlinx.utils.TLinx2Util;

import java.util.TreeMap;

/**
 * 浦发银行支付集成测试类
 * 用于验证集成的功能是否正常工作
 * 
 * @author wangmeng
 * @since 2026-03-16
 */
public class TLinx2IntegrationTest {
    
    public static void main(String[] args) {
        System.out.println("=== 浦发银行支付集成测试 ===");
        
        // 测试1: 配置参数验证
        testConfig();
        
        // 测试2: 工具类功能验证
        testUtils();
        
        // 测试3: DTO对象验证
        testDTO();
        
        // 测试4: 服务接口验证
        testService();
        
        System.out.println("=== 测试完成 ===");
    }
    
    /**
     * 测试配置参数
     */
    private static void testConfig() {
        System.out.println("\n--- 测试配置参数 ---");
        System.out.println("OPEN_ID: " + TLinx2Config.OPEN_ID);
        System.out.println("OPEN_KEY: " + TLinx2Config.OPEN_KEY);
        System.out.println("OPEN_URL: " + TLinx2Config.OPEN_URL);
        System.out.println("PAY_ORDER: " + TLinx2Config.PAY_ORDER);
        System.out.println("配置参数测试通过 ✓");
    }
    
    /**
     * 测试工具类功能
     */
    private static void testUtils() {
        System.out.println("\n--- 测试工具类功能 ---");
        
        try {
            // 测试时间戳生成
            String timestamp = TLinx2Util.getCurrentTimestamp();
            System.out.println("当前时间戳: " + timestamp);
            
            // 测试SHA1加密
            String sha1Result = TLinx2Util.sha1("test123");
            System.out.println("SHA1加密结果: " + sha1Result);
            
            // 测试MD5加密
            String md5Result = TLinx2Util.md5("test123");
            System.out.println("MD5加密结果: " + md5Result);
            
            // 测试AES加密解密
            String testData = "{\"out_no\":\"TEST123\",\"amount\":100}";
            String encrypted = TLinx2Util.encrypt(testData, TLinx2Config.OPEN_KEY);
            System.out.println("AES加密结果: " + encrypted);
            
            String decrypted = TLinx2Util.decrypt(encrypted, TLinx2Config.OPEN_KEY);
            System.out.println("AES解密结果: " + decrypted);
            
            // 测试签名功能
            TreeMap<String, String> testMap = new TreeMap<>();
            testMap.put("open_id", TLinx2Config.OPEN_ID);
            testMap.put("timestamp", timestamp);
            testMap.put("test_param", "test_value");
            
            String signature = TLinx2Util.sign(testMap);
            System.out.println("签名结果: " + signature);
            
            System.out.println("工具类功能测试通过 ✓");
            
        } catch (Exception e) {
            System.err.println("工具类功能测试失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 测试DTO对象
     */
    private static void testDTO() {
        System.out.println("\n--- 测试DTO对象 ---");
        
        // 测试下单请求DTO
        TLinx2PayOrderRequest orderRequest = new TLinx2PayOrderRequest();
        orderRequest.setOutNo("TEST" + System.currentTimeMillis());
        orderRequest.setPmtTag(TLinx2Config.PMT_TAG_WEIXIN);
        orderRequest.setOrdName("测试订单");
        orderRequest.setOriginalAmount(100);
        orderRequest.setTradeAmount(100);
        orderRequest.setRemark("测试支付");
        orderRequest.setNotifyUrl("http://localhost:8080/notify");
        
        System.out.println("下单请求DTO: " + orderRequest.toString());
        
        // 测试响应DTO
        TLinx2PayOrderResponse orderResponse = new TLinx2PayOrderResponse();
        orderResponse.setErrcode("0");
        orderResponse.setMsg("success");
        orderResponse.setTradeQrcode("https://qr.test.com/test");
        orderResponse.setOrdNo("ORD123456");
        
        System.out.println("下单响应DTO: " + orderResponse.toString());
        System.out.println("响应是否成功: " + orderResponse.isSuccess());
        
        // 测试状态查询请求DTO
        TLinx2PayStatusRequest statusRequest = new TLinx2PayStatusRequest();
        statusRequest.setOutNo("TEST123");
        
        System.out.println("状态查询请求DTO: " + statusRequest.toString());
        
        // 测试退款请求DTO
        TLinx2RefundRequest refundRequest = new TLinx2RefundRequest();
        refundRequest.setOutNo("TEST123");
        refundRequest.setRefundOutNo("RF" + System.currentTimeMillis());
        refundRequest.setRefundOrdName("退款测试");
        refundRequest.setRefundAmount(50);
        refundRequest.setRemark("测试退款");
        refundRequest.setShopPass(TLinx2Util.sha1("123456"));
        
        System.out.println("退款请求DTO: " + refundRequest.toString());
        
        System.out.println("DTO对象测试通过 ✓");
    }
    
    /**
     * 测试服务接口
     */
    private static void testService() {
        System.out.println("\n--- 测试服务接口 ---");
        
        try {
            TLinx2PayService payService = new TLinx2PayServiceImpl();
            
            // 由于需要网络连接，这里只做基本的实例化测试
            System.out.println("服务实例化成功: " + payService.getClass().getSimpleName());
            
            // 可以在这里添加mock测试或实际的网络测试
            System.out.println("服务接口测试通过 ✓ (基础实例化测试)");
            
        } catch (Exception e) {
            System.err.println("服务接口测试失败: " + e.getMessage());
            // 这里可能是由于缺少Spring上下文导致的，属于正常现象
            System.out.println("服务接口测试: 需要Spring上下文支持");
        }
    }
}