package com.cyksj.common.util;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.cyksj.common.exception.BusinessRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 项目名: yhlxj2
 * 文件名: TicketCodeCommonUtil
 * 创建者: JavaZou
 * 创建时间:2024/5/24 15:31
 */

public class TicketCodeCommonUtil {
	private static final Logger LOGGER = LoggerFactory.getLogger(TicketCodeCommonUtil.class);
	/**
	 * 车票邮箱验证码
	 */
	public static String getTicketCodeStr(String body, Long goodsId) {
		if (StrUtil.isEmpty(body)) {
			return body;
		}
		String str = getCodeStartStr(goodsId, body);
		if (StrUtil.isEmpty(str)) {
			return null;
		}
		int index = body.indexOf(str);
		if (index == -1) {
			return null;
		}
		String code = StrUtil.subWithLength(body, index, 512);
		if (StrUtil.isNotBlank(code)) {
			code = code.replace(str, "");
		}
		int length = 4;
		if (goodsId == 33l || goodsId == 45l || goodsId == 18l) {
			length = 6;
		}
		code = StringUtil.getVerifyCodePattern(code, length);
		if (!Validator.isNumber(code)) {
			LOGGER.error("获取账号验证码失败:{}", StrUtil.subSufByLength(code, 512));
			throw BusinessRuntimeException.getInstance("获取验证码失败,请重新获取..");
		}
		return code.trim();
	}

	public static String extractUrl(String body, String prefixUrl) {
		String urlPattern = "(https?://\\S+)";
		Pattern pattern = Pattern.compile(urlPattern);
		Matcher matcher = pattern.matcher(body);

		// 查找匹配的URL
		while (matcher.find()) {
			String url = matcher.group();
			if (url.contains(prefixUrl)) {
				return url.replaceAll("\"", "");
			}
		}
		return null;
	}

	public static String getCodeStartStr(Long goodsId, String body) {
		String str = null;
		//PS、Adobe团队版全家桶
		if (goodsId == 33l || goodsId == 45l) {
			str = "color:#505050; font-family:adobe-clean, Helvetica Neue, Helvetica, Verdana, Arial, sans-serif;";
			if (!body.contains(str)) {
				str = "X-Report-Abuse: abuse@adobe.com";
			}
		}
		//迪士尼
		if (goodsId == 3l) {
			str = "letter-spacing:4px; line-height:38px; mso-line-height-rule: exactly";
			if (!body.contains(str)) {
				str = "Noto Sans Display', Helvetica, Arial, sans-serif; font-weight: 600;text-align: center;";
				if (!body.contains(str)) {
					str = "使用此密码验证 MyDisney 帐户关联的电子邮件地址。此密码将在 15 分钟后过期";
				}
			}
		}
		//GPT PLUS
		if (goodsId == 18l) {
			str = "X-OriginatorOrg: sct-15-20-7719-20-msonline-outlook-80345.templateTenant";
			if (!body.contains(str)) {
				str = "ChatGPT Log-in Code";
				if (!body.contains(str)) {
					str = "Enter this temporary verification code";
					if (!body.contains(str)) {
						str = "[1] Your Sora code is 462040";
					}
					if (!body.contains(str)) {
						str = "[1] Your ChatGPT code is";
					}
					if (!body.contains(str)) {
						str = "Your ChatGPT code is";
					}
					if (!body.contains(str)) {
						str = "Your Sora code is";
					}
				}
			}
		}
		return str;
	}
}
