package com.cool.store.enums;

import com.aliyun.openservices.shade.org.apache.commons.lang3.StringUtils;
import com.cool.store.constants.CommonConstants;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.StringJoiner;

/**
 * @Author: WangShuo
 * @Date: 2025/02/11/16:06
 * @Version 1.0
 * @注释:
 */
@Slf4j
public enum QWMessageEnum {
    MESSAGE_1("common","您有一个待办任务，请尽快处理。", ""),
    ;

    private String target;

    private String title;

    private String content;
    QWMessageEnum(String target,String title, String content) {
        this.target = target;
        this.title = title;
        this.content = content;

    }

    public String getTitle() {
        return title;
    }

    public String getTarget() {
        return target;
    }

    public String getContent() {
        return content;
    }

    public String getContent(Map<String, String> paramMap) {
        String result = this.content;
        for (String key : paramMap.keySet()) {
            result = result.replace("${" + key + "}", paramMap.get(key));
        }
        return result;
    }

    public String getQWMessageUrl(String qywxUrl2, String corpId, Map<String, String> paramMap) {
        if (StringUtils.isAnyBlank(qywxUrl2, corpId) ) {
            return "";
        }
        String messageUrl = null;
        try {
            String noticeUrl = String.format(qywxUrl2, corpId, "qw_self_dkf",this.target);
            String finalUrl = buildUrlWithParams(noticeUrl, paramMap);
            log.info("finalUrl:{}", finalUrl);
            messageUrl = URLEncoder.encode(finalUrl, "UTF-8");
            messageUrl = String.format(CommonConstants.WX_SELF_AUTH_URL, corpId, messageUrl);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return messageUrl;
    }
    /**
     * 拼接参数
     * @param url
     * @param paramMap
     * @return
     */
    public static  String buildUrlWithParams(String url, Map<String, String> paramMap) {
        // 检查 URL 是否已经包含参数
        StringJoiner urlWithParams = new StringJoiner("&", url.contains("?") ? "&" : "?", "");

        // 遍历 Map，拼接参数
        for (Map.Entry<String, String> entry : paramMap.entrySet()) {
            urlWithParams.add(entry.getKey() + "=" + entry.getValue());
        }

        // 返回拼接后的 URL
        return url + urlWithParams.toString();
    }
}
