package com.org.hktshop.controller.Admin;

import com.org.hktshop.base.BaseResult;
import com.org.hktshop.constant.CommonConstant;
import com.org.hktshop.controller.HktBaseController;
import com.org.hktshop.dto.MailInfo;
import com.org.hktshop.service.user.impl.MailServiceImpl;
import com.org.hktshop.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/shop/mail")
@Api(tags = "邮件接口")
public class MailSmtpController extends HktBaseController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MailServiceImpl mailServiceImpl;

    /**
     * 发送邮件
     *
     * @param to
     * @return
     */
    @GetMapping("/sendMail")
    @ApiOperation(value = "发送邮件", notes = "发送邮件")
    @ApiImplicitParam(name = "to", value = "接收人", required = true, paramType = "query")
    public BaseResult sendMail(@RequestParam("to") String to) {
        try {
            if (!MailFormateCheckUtil.emailFormat(to)) {
                return BaseResult.fail(HttpCode.MAIL_FORMATE_CHECK_ERROR.getCode(), HttpCode.MAIL_FORMATE_CHECK_ERROR.getMsg());
            }
            String code = MathUtil.CreateRamdomNums(6);
            MailInfo mailInfo = new MailInfo();
            mailInfo.setCode(code);
            mailInfo.setMail(to);
            redisUtil.set(CommonConstant.HKT_SHOP_USER + userInfo.getId(), mailInfo, 30, TimeUnit.MINUTES);
            mailServiceImpl.sendMail("您的验证码为：", to, String.valueOf(code));
            return BaseResult.success();
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("/confirmBinding")
    @ApiOperation(value = "邮件绑定确认", notes = "邮件绑定确认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "to", value = "接收人", required = true, paramType = "query"),
            @ApiImplicitParam(name = "code", value = "验证码", required = true, paramType = "query")
    })
    public BaseResult phoneCode(@RequestParam("to") String to, @RequestParam("code") String code) {
        try {
            MailInfo mailInfo = (MailInfo) redisUtil.get(CommonConstant.HKT_SHOP_USER + userInfo.getId());
            if (to.equals(mailInfo.getMail()) && code.equals(mailInfo.getCode())) {
                return BaseResult.success();
            }
            return BaseResult.fail(HttpCode.MAIL_OR_CODE_ERROR.getCode(), HttpCode.MAIL_OR_CODE_ERROR.getMsg());
        } catch (Exception ex) {
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode(), HttpCode.INTERNAL_SERVER_ERROR.getMsg());
        }
    }
}
