package com.schisandra.wechat.controller;

import com.schisandra.wechat.handler.WeChatMsgFactory;
import com.schisandra.wechat.handler.WeChatMsgHandler;
import com.schisandra.wechat.utils.MessageUtil;
import com.schisandra.wechat.utils.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @Classname CallBackController
 * @BelongsProject: schisandra-cloud-storage
 * @BelongsPackage: com.schisandra.wechat.controller
 * @Author: schisandra
 * @CreateTime: 2024-02-21  16:41
 * @Description: TODO
 * @Version: 1.0
 */
@RestController
@Slf4j
public class CallBackController {

    @Resource
    private WeChatMsgFactory weChatMsgFactory;
    private static final String token = "LDQ20020618xxx";

    @RequestMapping("/test")
    public String test() {
        return "HELLO WORLD";
    }

    /**
     * @description: 回调消息校验
     * @param: [signature, timestamp, nonce, echostr]
     * @return: java.lang.String
     * @author schisandra
     * @date: 2024/2/21 17:16
     */
    @GetMapping("/callback")
    public String callback(@RequestParam("signature") String signature,
                           @RequestParam("timestamp") String timestamp,
                           @RequestParam("nonce") String nonce,
                           @RequestParam("echostr") String echostr) {
        log.info("get验签请求参数:signature: {}，timestamp: {}， nonce: {}，echostr: {}",
                signature, timestamp, nonce, echostr);

        String shaStr = SHA1.getSHA1(token, timestamp, nonce, "");
        if (signature.equals(shaStr)) {
            return echostr;
        }
        return "unKnown";
    }

    @PostMapping(value = "callback", produces = "application/xml;charset=UTF-8")
    public String callback(
            @RequestBody String requestBody,
            @RequestParam("signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam(value = "msg_signature", required = false) String msgSignature) {
        log.info("接收到微信消息：requestBody：{}", requestBody);
        Map<String, String> messageMap = MessageUtil.parseXml(requestBody);
        String msgType = messageMap.get("MsgType");
        String event = messageMap.get("Event") == null ? "" : messageMap.get("Event");
        log.info("msgType:{},event:{}", msgType, event);

        StringBuilder sb = new StringBuilder();
        sb.append(msgType);
        if (!StringUtils.isEmpty(event)) {
            sb.append(".");
            sb.append(event);
        }
        String msgTypeKey = sb.toString();
        WeChatMsgHandler weChatMsgHandler = weChatMsgFactory.getHandlerByMsgType(msgTypeKey);
        if (Objects.isNull(weChatMsgHandler)) {
            return "unknown";
        }
        String replyContent = weChatMsgHandler.dealMsg(messageMap);
        log.info("replyContent:{}", replyContent);
        return replyContent;
    }
}
