package com.dji.sample.common.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import com.dji.sample.configuration.MailConfiguration;
import com.dji.sample.manage.model.entity.DeviceLogsEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @description：发送邮件
 */
@Slf4j
@Component
public class EmailUtil {

    private static final String djEmail = "ryuiso@qq.com";

    private static final String title = "设备异常反馈";

    private static final String fj = "附件";

    private static final String jcRz = "机场日志";

    private static final String fxqRz = "飞行器日志";

    static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void sendHtmlDj(String url , String fjurl,String jcUrl, DeviceLogsEntity contnet) {
        executorService.submit(() -> {
            MailAccount mailAccount = createAccount();
            // 编写 HTML 内容
            String htmlContent = createContent(url,fjurl,jcUrl,contnet);
            MailUtil.send(mailAccount,djEmail,title,htmlContent,true,null);
        });
    }

    private static MailAccount createAccount(){
        MailAccount mailAccount = new MailAccount();
        mailAccount.setHost(MailConfiguration.host);
        mailAccount.setUser(MailConfiguration.user);
        mailAccount.setPass(MailConfiguration.pass);
        mailAccount.setSslEnable(MailConfiguration.sslEnable);
        mailAccount.setPort(MailConfiguration.port);
        mailAccount.setAuth(MailConfiguration.auth);
        mailAccount.setFrom(MailConfiguration.from);
        mailAccount.setStarttlsEnable(MailConfiguration.starttlsEnable);
        mailAccount.setTimeout(MailConfiguration.timeout);
        mailAccount.setConnectionTimeout(MailConfiguration.connectionTimeout);
        return mailAccount;
    }

    private static String createContent(String ossurl, String fjurl,String jcUrl,DeviceLogsEntity  content){
        // 处理 URL 并拼接成 HTML
        List<String> urls = new ArrayList<>();

        if (!StrUtil.isBlank(ossurl)) {
            urls.add(fj);
            urls.addAll(StrUtil.split(ossurl, ','));
        }
        if (!StrUtil.isBlank(jcUrl)) {
            urls.add(jcRz);
            urls.addAll(StrUtil.split(jcUrl, ','));
        }
        if (!StrUtil.isBlank(fjurl)) {
            urls.add(fxqRz);
            urls.addAll(StrUtil.split(fjurl, ','));
        }


        StringBuilder htmlContent = new StringBuilder("<html><body>");
        htmlContent.append("<h1>无人机异常邮件</h1>")
                .append("<p>异常描述："+content.getLogsInfo()+"</p>")
                .append("<p>发生时间："+ LocalDateTime.ofInstant(Instant.ofEpochMilli(content.getHappenTime()), ZoneId.systemDefault())+"</p>")
                .append("<p>联系电话："+content.getContactNumber()+"</p>")
                .append("<p>联系邮箱："+content.getContactEmail()+"</p>")
                .append("<ul>");
        for (int i = 0; i < urls.size(); i++) {
            String url = urls.get(i);
            if (StrUtil.isNotBlank(url)) {
                htmlContent.append("<li>")
                        .append(url)
                        .append("</li>");
                if (i < urls.size() - 1) {
                    htmlContent.append("<br>"); // 添加换行
                }
            }
        }
        htmlContent.append("</ul>")
                .append("</body></html>");
        return htmlContent.toString();
    }

    public static void main(String[] args) {
        String url = "www.baidu.com,www.baidu.com,www.baidu.com,www.baidu.com";
        String fjurl = "www.baidu.com,www.baidu.com,www.baidu.com";
        String jcurl = "www.baidu.com,www.baidu.com,www.baidu.com";
        DeviceLogsEntity deviceLogsEntity = new DeviceLogsEntity();
        deviceLogsEntity.setContactEmail("1111");
        deviceLogsEntity.setContactNumber("1222");
        deviceLogsEntity.setHappenTime(Long.parseLong("10"));
        deviceLogsEntity.setLogsInfo("测试");
        sendHtmlDj(url,fjurl,jcurl,deviceLogsEntity);
    }


}