package com.example.demo.module.captcha;

import com.alibaba.fastjson.JSONObject;
import com.example.demo.common.CacheMapRoot;
import com.example.demo.common.MD5Tool;
import com.example.demo.common.RequestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.jc.util.JCLogger;

import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping("/emailValidate")
public class EmailValidateController {
    @Autowired
    JavaMailSender javaMailSender;
    //通过注解的方式获取application.properties的配置值
    @Value("${spring.mail.username}")
    String hostEmail;

    @PostMapping("/sendEmailValidateCode")
    public RequestResult sendEmailValidateCode(String email, int language) {
        int validateCode = new Random().nextInt(900000) + 100000;
        String from = hostEmail + "(青凤鸾)";
        if (language == 1) from = hostEmail + "(WONDER FITTER)";
        String subject = "邮箱验证码（10分钟内有效）";
        if (language == 1) subject = "Email verification code (Valid within 10 minutes)";
        String content = String.valueOf(validateCode);
        if (language == 1) content = String.valueOf(validateCode);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(email);
        message.setSubject(subject);
        message.setText(content);
        RequestResult requestResult = new RequestResult();
        try {
            javaMailSender.send(message);
            CacheMapRoot.validateEmail.put(email, validateCode, System.currentTimeMillis() + 10 * 60 * 1000);
            JCLogger.info("邮箱验证发送", email, validateCode);
        } catch (Exception e) {
            e.printStackTrace();
            requestResult.setFailCode();
        }
        return requestResult;
    }

    @PostMapping("/validateEmail")
    public RequestResult validateEmail(String email, int code) {
        Integer validateCode = CacheMapRoot.validateEmail.get(email, Integer.class);
        RequestResult requestResult = new RequestResult();
        if (validateCode == null || validateCode != code) {
            requestResult.setCode(-1);
            requestResult.setMsg("验证失败");
            return requestResult;
        }
        CacheMapRoot.validateEmail.remove(email, code);
        JSONObject data = new JSONObject();
        long timestamp = System.currentTimeMillis();
        data.put("email", email);
        data.put("timestamp", timestamp);
        data.put("sign", MD5Tool.makeMD5(email + "~happy~" + timestamp));
        requestResult.setData(data);
        requestResult.setMsg("验证成功");
        return requestResult;
    }
}
