package com.jiayue.ssi.controller;

import com.jiayue.ssi.annotation.InterfaceLimit;
import com.jiayue.ssi.constant.CacheConstants;
import com.jiayue.ssi.entity.SysUser;
import com.jiayue.ssi.service.SysUserService;
import com.jiayue.ssi.util.IdUtils;
import com.jiayue.ssi.util.LocalCache;
import com.jiayue.ssi.util.RandomUtil;
import com.jiayue.ssi.util.ResponseVO;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户登录控制类
 *
 * @author xsl
 * @since 2023/02/20
 */
@RestController
public class UserLoginController {
    @Autowired
    SysUserService sysUserService;
    @Autowired
    JavaMailSender javaMailSender;
    @Value("${spring.mail.username}")
    String fromMailAddress;

    /**
     * 生成验证码
     *
     * @param httpServletResponse
     * @throws IOException
     */
    @GetMapping("/getVerifyCode")
    @InterfaceLimit(value = 2,time = 1000)
    public ResponseVO getVerifyCode(HttpServletResponse httpServletResponse) throws IOException {
        // gif类型
        // GifCaptcha captcha = new GifCaptcha(130, 48);
        // 中文类型
        // ChineseCaptcha captcha = new ChineseCaptcha(130, 48);
        // 中文gif类型
        // ChineseGifCaptcha captcha = new ChineseGifCaptcha(130, 48);
        // 算术类型
        // ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 48);
        // png类型
        // 三个参数分别为宽、高、位数
         SpecCaptcha captcha = new SpecCaptcha(150, 40, 4);
        /**
         * 验证码字符类型 TYPE_DEFAULT 数字和字母混合 TYPE_ONLY_NUMBER 纯数字 TYPE_ONLY_CHAR 纯字母 TYPE_ONLY_UPPER 纯大写字母 TYPE_ONLY_LOWER
         * 纯小写字母 TYPE_NUM_AND_UPPER 数字和大写字母
         **/
        // 设置类型 数字和字母混合
        captcha.setCharType(Captcha.TYPE_DEFAULT);
        // 设置字体
        captcha.setCharType(Captcha.FONT_9);

        String uuid = IdUtils.simpleUUID();
        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + uuid;
        // uuid存入缓存，失效时间默认5分钟
        LocalCache.set(verifyKey, captcha.text());
        // 输出图片流
        // captcha.out(httpServletResponse.getOutputStream());
        String base64 = captcha.toBase64();
        int index = base64.indexOf(",");
        String substring = base64.substring(index + 1);

        Map<String, String> dataMap = new HashMap<>(16);
        dataMap.put("uuid", uuid);
        dataMap.put("imgBase64", substring);
        dataMap.put("captchaText", captcha.text());
        return ResponseVO.success(dataMap);
    }

    /**
     * 获取邮箱口令
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @throws IOException
     */
    @PostMapping("/getMailCode")
    @InterfaceLimit(time=5000)
    public ResponseVO getMailCode(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
        throws Exception {
        String username = httpServletRequest.getParameter("username");
        // 口令保存到服务器
        String mailKey = CacheConstants.MAIL_CODE_KEY + username;

        SysUser sysUser = sysUserService.queryUserName(username);
        if (sysUser != null) {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true, "utf-8");
            mimeMessageHelper.setFrom(fromMailAddress);
            mimeMessageHelper.setTo(sysUser.getMailBox());
            mimeMessageHelper.setSubject("邮箱验证码");
            // 生成6位邮箱口令
            String mailRandom = RandomUtil.mailRandom();
            // uuid存入缓存，失效时间4分钟
            LocalCache.set(mailKey, mailRandom, 60000 * 4);
            mimeMessageHelper.setText("口令：" + mailRandom + ",有效期4分钟。", true);
            javaMailSender.send(mimeMessage);
            System.out.println("邮箱发送成功。。。。。。");
        }
        return ResponseVO.success();
    }
}
