package com.dsk.web.controller.common;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.captcha.AbstractCaptcha;
import cn.hutool.captcha.generator.CodeGenerator;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.dsk.common.constant.CacheConstants;
import com.dsk.common.constant.Constants;
import com.dsk.common.constant.GlobalConstants;
import com.dsk.common.core.domain.R;
import com.dsk.common.enums.CaptchaType;
import com.dsk.common.exception.user.CaptchaException;
import com.dsk.common.exception.user.CaptchaExpireException;
import com.dsk.common.helper.LoginHelper;
import com.dsk.common.utils.DingTalkUtil;
import com.dsk.common.utils.MessageUtils;
import com.dsk.common.utils.StringUtils;
import com.dsk.common.utils.email.MailUtils;
import com.dsk.common.utils.redis.RedisUtils;
import com.dsk.common.utils.reflect.ReflectUtils;
import com.dsk.common.utils.spring.SpringUtils;
import com.dsk.framework.config.properties.CaptchaProperties;
import com.dsk.framework.config.properties.MailProperties;
import com.dsk.system.domain.SysUser;
import com.dsk.system.mapper.SysUserMapper;
import com.dsk.system.service.ISysConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.entity.SmsResponse;
import org.dromara.sms4j.core.factory.SmsFactory;
import org.dromara.sms4j.provider.enumerate.SupplierType;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.time.Duration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 验证码操作处理
 *
 * @author Lion Li
 */
@SaIgnore
@Slf4j
@Validated
@RequiredArgsConstructor
@RestController
public class CaptchaController {

    private final CaptchaProperties captchaProperties;
    private final ISysConfigService configService;
    private final MailProperties mailProperties;
    private final SysUserMapper userMapper;

    @Value("${captcha.enable}")
    private boolean captchaEnabled;
    /**
     * 发送短信验证码
     *
     * @param phonenumber 用户手机号
     */
    @GetMapping("/captchaSms")
    public R<Void> smsCaptcha(@NotBlank(message = "{user.phonenumber.not.blank}") String phonenumber,String uid,String captchaCode,String templateId) {
        String key = GlobalConstants.CAPTCHA_CODE_KEY + phonenumber;
        // 验证码开关
        if (captchaEnabled) {
            validateCaptcha(phonenumber, captchaCode, uid);
        }
        String code = RandomUtil.randomNumbers(6);
        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        // 验证码模板id 自行处理 (查数据库或写死均可)
        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("code", code);
//        SmsBlend smsBlend = SmsFactory.createSmsBlend(SupplierType.ALIBABA);
//        SmsResponse smsResponse = smsBlend.sendMessage(phonenumber, templateId, map);
//        if (!"OK".equals(smsResponse.getCode())) {
//            log.error("验证码短信发送异常 => {}", smsResponse);
//            return R.fail(smsResponse.getMessage());
//        }
        DingTalkUtil.sendDingTalkMsg("短信通知："+phonenumber+"登录验证码："+code+"，请尽快输入验证码完成验证，勿将验证码透露给他人，如非本人操作，请忽略本条短信。");

        return R.ok();
    }

    private void validateCaptcha(String phonenumber, String captchaCode, String uuid) {
        String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + StringUtils.defaultString(uuid, "");
        String captcha = RedisUtils.getCacheObject(verifyKey);
        RedisUtils.deleteObject(verifyKey);
        if (captcha == null) {
//            recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire"));
            throw new CaptchaExpireException();
        }
        if (!captchaCode.equalsIgnoreCase(captcha)) {
//            recordLogininfor(username, Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.error"));
            throw new CaptchaException();
        }
    }

    /**
     * 开发需要,临时发送短信验证码
     */
    @GetMapping("/getTempSmsCode")
    public R<Void> getTempSmsCode() {
        SysUser sysUser = userMapper.selectUserById(LoginHelper.getUserId());
        String key = GlobalConstants.CAPTCHA_CODE_KEY + sysUser.getPhonenumber();
        String code = RandomUtil.randomNumbers(6);
        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        return R.ok("验证码:" + code);
    }

    /**
     * 开发需要,临时校验短信验证码
     *
     * @param smsCode     验证码
     */
    @PostMapping("/checkSmsCode")
    public R<Boolean> checkSmsCode(String smsCode) {
        SysUser sysUser = userMapper.selectUserById(LoginHelper.getUserId());
        String key = GlobalConstants.CAPTCHA_CODE_KEY + sysUser.getPhonenumber();
        String code = RedisUtils.getCacheObject(key);
        if (StringUtils.isBlank(code)) {
            throw new CaptchaExpireException();
        }
        if (code.equals(smsCode)) {
            RedisUtils.deleteObject(key);
            return R.ok("校验成功");
        }
        return R.fail("验证码错误");
    }

    /**
     * 发送邮箱验证码
     *
     * @param email 邮箱
     */
    @GetMapping("/captchaEmail")
    public R<Void> emailCode(@NotBlank(message = "{user.email.not.blank}") String email) {
        if (!mailProperties.getEnabled()) {
            return R.fail("当前系统没有开启邮箱功能！");
        }
        String key = GlobalConstants.CAPTCHA_CODE_KEY + email;
        String code = RandomUtil.randomNumbers(4);
        RedisUtils.setCacheObject(key, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        try {
            MailUtils.sendText(email, "登录验证码", "您本次验证码为：" + code + "，有效性为" + Constants.CAPTCHA_EXPIRATION + "分钟，请尽快填写。");
        } catch (Exception e) {
            log.error("验证码短信发送异常 => {}", e.getMessage());
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    /**
     * 生成验证码
     */
    @GetMapping("/captchaImage")
    public R<Map<String, Object>> getCode() {
        Map<String, Object> ajax = new HashMap<>();
        boolean captchaEnabled = captchaProperties.getEnable();
        ajax.put("captchaEnabled", captchaEnabled);
        if (!captchaEnabled) {
            return R.ok(ajax);
        }
        // 保存验证码信息
        String uuid = IdUtil.simpleUUID();
        String verifyKey = GlobalConstants.CAPTCHA_CODE_KEY + uuid;
        // 生成验证码
        CaptchaType captchaType = captchaProperties.getType();
        boolean isMath = CaptchaType.MATH == captchaType;
        Integer length = isMath ? captchaProperties.getNumberLength() : captchaProperties.getCharLength();
        CodeGenerator codeGenerator = ReflectUtils.newInstance(captchaType.getClazz(), length);
        AbstractCaptcha captcha = SpringUtils.getBean(captchaProperties.getCategory().getClazz());
        captcha.setGenerator(codeGenerator);
        captcha.createCode();
        String code = captcha.getCode();
        if (isMath) {
            ExpressionParser parser = new SpelExpressionParser();
            Expression exp = parser.parseExpression(StringUtils.remove(code, "="));
            code = exp.getValue(String.class);
        }
        log.info("图形验证码：" + code);
        RedisUtils.setCacheObject(verifyKey, code, Duration.ofMinutes(Constants.CAPTCHA_EXPIRATION));
        ajax.put("uuid", uuid);
        ajax.put("img", captcha.getImageBase64());
        return R.ok(ajax);
    }

}
