package com.cyksj.service.mail.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import com.cyksj.common.util.Jsons;
import com.cyksj.common.util.StringUtil;
import com.cyksj.common.util.TicketCodeCommonUtil;
import com.cyksj.model.dto.CuiQiuEmailDetailDto;
import com.cyksj.model.dto.CuiQiuEmailDto;
import com.cyksj.model.dto.CuiQiuEmailResultDto;
import com.cyksj.service.mail.CuiQiuMailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目名: yhlxj2
 * 文件名: CuiQiuMailServiceImpl
 * 创建者: JavaZou
 * 创建时间:2024/4/25 15:57
 */
@Service
@Slf4j
public class CuiQiuMailServiceImpl implements CuiQiuMailService {
	private final static String CUI_QIU_TOKEN = "4ff0341ebf1747cd89a2d1a5a296b68b";

	//脆球域名id
	private final static String DOMAIN_ID = "1696913058045050661";

	//代收管理员邮件id
	private final static String ADMIN_MAIL_ID = "1696913058063659217";

	//PS 验证码官方
	private final static String PS_ADOBE = "Adobe";

	private final static String DISNEY = "Disney+";

	@Override
	public String getVerifyCode(String email, Long goodsId) throws Exception {
		//获取近二十封邮件
		String url = "https://domain-open-api.cuiqiu.com/v1/message/list";

		Map<String, Object> params = new HashMap<>();

		//获取近五分钟的代收邮件
		//维度是天
		DateTime now = DateTime.now();
		params.put("token", CUI_QIU_TOKEN);
		params.put("mail_id", ADMIN_MAIL_ID);
		params.put("start_time", DateUtil.offsetDay(now, -1).toDateStr());
		params.put("end_time", DateUtil.offsetDay(now, 1).toDateStr());
		params.put("to", email);
		params.put("limit", 20);

		HttpRequest post = HttpUtil.createPost(url);
		post.form(params);
		HttpResponse execute = post.execute();
		CuiQiuEmailResultDto resultDto = Jsons.parseObject(execute.body(), CuiQiuEmailResultDto.class);
		List<CuiQiuEmailDto> list = resultDto.getData().getList();
		//获取邮箱对应的邮件
		list = list.stream().filter(emailInfo -> {
			CuiQiuEmailDto.CuiQiuUser from = emailInfo.getFrom().get(0);
			CuiQiuEmailDto.CuiQiuUser cuiQiuUser = emailInfo.getTo().get(0);
			if (StrUtil.equalsIgnoreCase(cuiQiuUser.getAddress(), email)) {
				//PS
				if (goodsId == 33l && StrUtil.equals(from.getName(), PS_ADOBE)) {
					return true;
				}
				//Disney
				if (goodsId == 3l && StrUtil.equals(from.getName(), DISNEY)) {
					return true;
				}
			}
			return false;
		}).collect(Collectors.toList());

		if (CollUtil.isNotEmpty(list)) {
			for (CuiQiuEmailDto cuiQiuEmailDto : list) {
				Long messageId = cuiQiuEmailDto.getId();
				try {
					String code = getVerifyCodeByEmail(messageId, goodsId);
					if (StrUtil.isNotBlank(code)) {
						return code;
					}
				} catch (Exception e) {
				}
			}
		}
		return null;
	}

	public String getVerifyCodeByEmail(Long messageId, Long goodsId) throws Exception {
		String url = "https://domain-open-api.cuiqiu.com/v1/message/detail";
		Map<String, Object> params = new HashMap<>();
		params.put("token", CUI_QIU_TOKEN);
		params.put("mail_id", ADMIN_MAIL_ID);
		params.put("message_id", messageId);
		HttpRequest post = HttpUtil.createPost(url);
		post.form(params);
		HttpResponse execute = post.execute();
		String body = execute.body();
		CuiQiuEmailDetailDto cuiQiuEmailDetailDto = Jsons.parseObject(body, CuiQiuEmailDetailDto.class);
		CuiQiuEmailDetailDto.Content content = cuiQiuEmailDetailDto.getData().getContent();

		String contentBody = content.getBody();
		String code = TicketCodeCommonUtil.getTicketCodeStr(contentBody, goodsId);
		int length = 4;
		if (goodsId == 33l) {
			length = 6;
		}
		return StringUtil.getVerifyCodePattern(code, length);
	}
}
