package com.izouma.jmrh.service;

import com.izouma.jmrh.domain.MailCode;
import com.izouma.jmrh.exception.BusinessException;
import com.izouma.jmrh.repo.MailCodeRepo;
import com.izouma.jmrh.utils.DateTimeUtils;
import com.izouma.jmrh.utils.MailUtils;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@AllArgsConstructor
@Slf4j
public class MailCodeService {
    private MailCodeRepo mailCodeRepo;

    public void sendCode(String mail) {
        mailCodeRepo.expire(mail);
        String code = RandomStringUtils.randomNumeric(4);
        mailCodeRepo.save(MailCode.builder()
                .mail(mail)
                .code(code)
                .expired(false)
                .expireAt(LocalDateTime.now().plusMinutes(20))
                .build());
        try {
            MailUtils.sendEmail("您的账户正在申请验证", "<style>\n" +
                    "        .container {\n" +
                    "            width: 775px;\n" +
                    "            background: linear-gradient(180deg, #FFCC72 0%, #FFF7EA 72%, #FFFFFF 100%);\n" +
                    "        }\n" +
                    "\n" +
                    "        .top {\n" +
                    "            height: 150px;\n" +
                    "            position: relative;\n" +
                    "        }\n" +
                    "\n" +
                    "        .logo {\n" +
                    "            position: absolute;\n" +
                    "            left: 0;\n" +
                    "            right: 0;\n" +
                    "            top: 0;\n" +
                    "            bottom: 50px;\n" +
                    "            margin: auto;\n" +
                    "            width: 500px;\n" +
                    "        }\n" +
                    "\n" +
                    "        .content {\n" +
                    "            padding: 50px 100px;\n" +
                    "            font-size: 28px;\n" +
                    "            color: #666666;\n" +
                    "            font-weight: bold;\n" +
                    "        }\n" +
                    "\n" +
                    "        .content .code {\n" +
                    "            color: #BF1616;\n" +
                    "            font-size: 50px;\n" +
                    "            background: rgba(191, 22, 22, 0.1);\n" +
                    "            text-align: center;\n" +
                    "            line-height: 100px;\n" +
                    "            margin: 30px 0;\n" +
                    "        }\n" +
                    "    </style>\n" +
                    "    <table align=\"center\">\n" +
                    "        <tr>\n" +
                    "            <td>\n" +
                    "                <div class=\"container\">\n" +
                    "                    <div class=\"top\"> <img class=\"logo\"\n" +
                    "                            src=\"https://jxjmrh.oss-cn-hangzhou.aliyuncs.com/image/pc_logo%402x.png\" />\n" +
                    "                    </div>\n" +
                    "                    <div class=\"content\">您的验证码是\n" +
                    "                        <div class=\"code\">" + code + "</div>\n" +
                    DateTimeUtils.format(LocalDateTime.now().plusMinutes(20), "yyyy-MM-dd HH:mm:ss") +
                    "前有效，<br>\n" +
                    "                        请不要将验证码透露给他人\n" +
                    "                    </div>\n" +
                    "                </div>\n" +
                    "            </td>\n" +
                    "        </tr>\n" +
                    "    </table>", mail);
        } catch (Exception e) {
            log.error("发送邮件失败", e);
        }
    }

    public void verify(String mail, String code) {
        MailCode mailCode = mailCodeRepo.findLastByMailAndExpireAtAfterAndExpiredFalse(mail, LocalDateTime.now())
                .orElseThrow(new BusinessException("验证码错误"));
        if (!code.equals(mailCode.getCode())) {
            throw new BusinessException("验证码错误");
        }
    }
}
