package com.njcn.msgpush.module.push.client.sender.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.dm20151123.Client;
import com.aliyun.dm20151123.models.SingleSendMailRequest;
import com.aliyun.dm20151123.models.SingleSendMailResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.njcn.msgpush.module.push.client.sender.EmailSender;
import com.njcn.msgpush.module.push.client.setting.impl.AliYunMailSetting;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author caozehui
 * @data 2026-02-11
 */
@Slf4j
public class AliyunEmailSender implements EmailSender {
    public static final String ACCOUNT_NAME = "accountName";
    public static final String REPLY_TO_ADDRESS = "replyToAddress";
    public static final String SUBJECT = "subject";
    public static final String HTML_BODY = "htmlBody";
    public static final String TEXT_BODY = "textBody";

    private Client emailClient;

    private final ThreadPoolExecutor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
            5,
            5,
            1000,
            TimeUnit.MILLISECONDS,
            new java.util.concurrent.ArrayBlockingQueue<>(1000),
            r -> {
                Thread thread = new Thread(r);
                thread.setName("AliYunSmsClient-Pool-" + thread.getId());
                thread.setDaemon(false);
                return thread;
            },
            new ThreadPoolExecutor.CallerRunsPolicy()
    );

    public AliyunEmailSender(AliYunMailSetting aliYunMailSetting) throws Exception {
        if (ObjectUtil.isNotNull(aliYunMailSetting)) {
            Config config = new Config()
                    .setAccessKeyId(aliYunMailSetting.getAccessKeyId())
                    .setAccessKeySecret(aliYunMailSetting.getAccessKeySecret())
                    .setRegionId(aliYunMailSetting.getRegionId())
                    .setEndpoint(aliYunMailSetting.getEndpoint());

            try {
                this.emailClient = new com.aliyun.dm20151123.Client(config);
            } catch (Exception e) {
                log.error("阿里云-邮件服务初始化失败，请检查配置信息");
                throw e;
            }
        }
    }

    @Override
    public boolean sendEmail(Map<String, Object> params, List<String> toAddressList) {
        RuntimeOptions runtimeOptions = new RuntimeOptions();
        runtimeOptions.autoretry = true;
        SingleSendMailRequest request = new SingleSendMailRequest()
                .setAccountName(params.get(ACCOUNT_NAME).toString())
                .setAddressType(1)
                .setReplyToAddress((boolean) params.get(REPLY_TO_ADDRESS))
                .setToAddress(StrUtil.join(StrUtil.COMMA, toAddressList))
                .setSubject(params.get(SUBJECT).toString())
                .setHtmlBody(params.get(HTML_BODY).toString())
                .setTextBody(params.get(TEXT_BODY).toString());

        try {
            SingleSendMailResponse response = this.emailClient.singleSendMailWithOptions(request, runtimeOptions);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
//        if(OK.equals(response.)){
//            return true;
//        }else {
//            return false;
//        }
        return false;
    }

}
