package com.hai.config;

import com.hai.model.TextMessage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @serviceName  .java
 * @author Sum1Dream
 * @version 1.0.0
 * @Description //TODO
 * @createTime 16:24 2022/9/2
 **/
@Configuration
public class WxConfig {


    /**
     * 微信客户端配置存储
     *
     * @return
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        // 公众号appId
        configStorage.setAppId(CommonSysConst.getSysConfig().getWxH5AppId());
        // 公众号appSecret
        configStorage.setSecret(CommonSysConst.getSysConfig().getWxH5AppSecret());
        return configStorage;
    }

    /**
     * @Author Sum1Dream
     * @Name wxMpService
     * @Description //TODO
     * @Date 16:24 2022/9/2
     * @Param []
     * @Return WxMpService
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }


    /**
     * 处理 subscribe 类型的event
     *
     * @param map
     * @param userOpenId
     * @return
     */
    private String handleEventSubscribe(Map<String, String> map, String userOpenId) {
        String resXmlStr = getReturnMsgSubscribe(map);
        return resXmlStr;
    }


    public String getReturnMsgSubscribe(Map<String, String> decryptMap) {
        TextMessage textMessage = new TextMessage();
        textMessage.setToUserName(decryptMap.get("FromUserName"));
        textMessage.setFromUserName(decryptMap.get("ToUserName"));
        textMessage.setCreateTime(System.currentTimeMillis());
        textMessage.setMsgType("text");
        textMessage.setContent("你好，欢迎关注XXX！\n" +
                "\n" +
                "关注XXX。立即登录PC端网址 \n" +
                " 即可完成注册！\n" +
                "\n" +
                "或，" +
                "<a href='https://hsgcs.dctpay.com/hsgH5?accountId=000000&gasKey=11000697&staffCode='>点击这里立即完成注册</a>");
        return getXmlString(textMessage);
    }

    public String getXmlString(TextMessage textMessage) {
        String xml = "";
        if (textMessage != null) {
            xml = "<xml>";
            xml += "<ToUserName><![CDATA[";
            xml += textMessage.getToUserName();
            xml += "]]></ToUserName>";
            xml += "<FromUserName><![CDATA[";
            xml += textMessage.getFromUserName();
            xml += "]]></FromUserName>";
            xml += "<CreateTime>";
            xml += textMessage.getCreateTime();
            xml += "</CreateTime>";
            xml += "<MsgType><![CDATA[";
            xml += textMessage.getMsgType();
            xml += "]]></MsgType>";
            xml += "<Content><![CDATA[";
            xml += textMessage.getContent();
            xml += "]]></Content>";
            xml += "</xml>";
        }
        return xml;
    }

}
