package com.hys.app.controller.erp.use;

import com.hys.app.framework.util.ScriptUtil;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.security.MessageDigest;
import java.util.Arrays;

@RestController
public class WeixinController {

    private static final Logger log = LoggerFactory.getLogger(ScriptUtil.class);

    private static final String TOKEN = "ryks2023weixin";  // 替换为您在微信公众平台设置的Token

    @GetMapping("/weixin")
    public String validateWeixinToken(
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam("echostr") String echostr) {
        System.out.println(echostr+"111111111111111111111111111111222222222222222222222222222-----------------------------------------");

//        String[] arr = {TOKEN, timestamp, nonce};
//        Arrays.sort(arr);
//        StringBuilder content = new StringBuilder();
//        for (String str : arr) {
//            content.append(str);
//        }
//
//        String generatedSignature = sha1(content.toString());
//
//        if (generatedSignature.equals(signature)) {
//            return echostr;
//        } else {
//            return "Failed to verify";
//        }
        return echostr;
    }

    @PostMapping("/receive_ticket")
    public String receiveComponentVerifyTicket(@RequestBody String requestBody,
                                               @RequestParam("msg_signature") String msgSignature,
                                               @RequestParam("timestamp") String timestamp,
                                               @RequestParam("nonce") String nonce) {

        log.info("验证来了啊 -----------------------------------------------------------11111111111111111---------");
        try {
            // 1. 验证消息的真实性
            if (!verifySignature(msgSignature, timestamp, nonce, requestBody)) {
                return "fail";
            }

            // 2. 解密消息
            String decryptedMsg = decryptMsg(requestBody);

            // 3. 解析 XML
            Document document = DocumentHelper.parseText(decryptedMsg);
            Element root = document.getRootElement();
            String componentVerifyTicket = root.elementText("ComponentVerifyTicket");

            // 4. 存储 ticket
            saveComponentVerifyTicket(componentVerifyTicket);

            // 5. 返回成功响应
            return "success";
        } catch (Exception e) {
           e.printStackTrace();
            return "fail";
        }
    }

    private String sha1(String data) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(data.getBytes());
            byte[] bytes = md.digest();
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (Exception e) {
            return "";
        }
    }



    public boolean verifySignature(String msgSignature, String timestamp, String nonce, String requestBody) {
        // 实现签名验证逻辑
        return true;
    }

    public String decryptMsg(String requestBody) {
        // 实现消息解密逻辑
        return null;
    }

    public void saveComponentVerifyTicket(String ticket) {
        // 实现存储 ticket 的逻辑
    }
}