package com.diagbot.facade;

import com.diagbot.dto.ImgVerCreatDTO;
import com.diagbot.entity.ImgVerInfo;
import com.diagbot.entity.SwsVerInfo;
import com.diagbot.entity.User;
import com.diagbot.enums.VisibleIdTypeEnum;
import com.diagbot.exception.CommonErrorCode;
import com.diagbot.exception.CommonException;
import com.diagbot.exception.ServiceErrorCode;
import com.diagbot.idc.VisibleIdCreater;
import com.diagbot.service.ImgVerService;
import com.diagbot.service.SmsVerService;
import com.diagbot.util.BeanUtil;
import com.diagbot.util.DateUtil;
import com.diagbot.util.RegexValidateUtil;
import com.diagbot.util.StringUtil;
import com.diagbot.vo.ImgVerVerVO;
import com.diagbot.vo.SmsVerCreatVO;
import com.diagbot.vo.SmsVerVerVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 用户验证业务层
 * @author: gaodm
 * @time: 2018/9/4 19:31
 */
@Component
public class VerFacade {

    public final static int SMS_MINS = 3000; //短信验证码间隔为3分钟 //TODO 时间间隔先调大了，之后改为3
    @Autowired
    private ImgVerService imgVerService;
    @Autowired
    private SmsVerService smsVerService;
    @Autowired
    private UserFacade userFacade;
    @Autowired
    private VisibleIdCreater visibleIdCreater;

    /**
     * 获取用户图片验证信息
     * @return 图片验证码信息
     */
    public ImgVerCreatDTO getImgVerification(){
        ImgVerCreatDTO imgVerCreatDTO = new ImgVerCreatDTO();
        String imgId = visibleIdCreater.getNextId(VisibleIdTypeEnum.IS_IMG_VER.getKey()).toString();
        ImgVerInfo imgVerInfo = new ImgVerInfo();
        imgVerInfo.setImgId(imgId);
        byte[] res = imgVerService.createImgVerification(imgVerInfo);
        imgVerCreatDTO.setImgId(imgId);
        imgVerCreatDTO.setImgByte(res);
        return imgVerCreatDTO;
    }

    /**
     * 验证用户图片验证码是否有效
     * @param imgVerVerVO 图片验证码验证输入信息
     * @return 验证是否成功
     */
    public Boolean verifyImgVerification(ImgVerVerVO imgVerVerVO){
        ImgVerInfo imgVerInfo = new ImgVerInfo();
        BeanUtil.copyProperties(imgVerVerVO, imgVerInfo);
        return imgVerService.verifyImgVerification(imgVerInfo);
    }

    /**
     * 获取短信验证码
     * @param smsVerCreatVO 获取短信验证码输入参数
     * @return 获取短信验证码是否成功
     */
    public Boolean getSmsVerification(SmsVerCreatVO smsVerCreatVO){
        //参数验证
        if (StringUtil.isBlank(smsVerCreatVO.getImgId())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "图片验证码唯一标志不能为空!");
        }
        if (StringUtil.isBlank(smsVerCreatVO.getCode())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "图片验证码不能为空!");
        }
        if (StringUtil.isBlank(smsVerCreatVO.getMobile())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "用户电话不能为空!");
        }
        if (!RegexValidateUtil.checkMobileNumber(smsVerCreatVO.getMobile())){
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR,
                    "请输入正确的手机号!");
        }
        //验证图片验证码
        ImgVerInfo imgVerInfo = new ImgVerInfo();
        BeanUtil.copyProperties(smsVerCreatVO, imgVerInfo);
        Boolean imgVerRes = imgVerService.verifyImgVerification(imgVerInfo);
        if (imgVerRes){
            User bean = userFacade.getUserInfo(smsVerCreatVO.getMobile());
            if (bean != null){
                throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "该号码已经被注册!");
            }

            //验证是否超过三分钟
            SwsVerInfo swsVerInfoCheck = smsVerService.getSmsVerification(smsVerCreatVO.getMobile());
            if (null != swsVerInfoCheck){
                if (DateUtil.after(DateUtil.addMinutes(swsVerInfoCheck.getCreateTime(), VerFacade.SMS_MINS),
                        DateUtil.now())){
                    throw new CommonException(ServiceErrorCode.SMS_SEND_ERROR,
                            "同一用户三分钟之内不能重复发送验证短信，请三分钟之后再次发送!");
                }
            }
        } else {
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "输入图形验证码有误!");
        }

        //发送短信
        String smsTemplateCode = "SMS_143861902";
        String code = smsVerService.smsSend(smsVerCreatVO.getMobile(), smsTemplateCode);

        //创建用户短信验证信息，缓存到redis
        SwsVerInfo swsVerInfo = new SwsVerInfo();
        swsVerInfo.setMobile(smsVerCreatVO.getMobile());
        swsVerInfo.setCode(code);
        return smsVerService.createSmsVerification(swsVerInfo);
    }



    /**
     * 忘记密码获取短信验证码
     * @param smsVerCreatVO 获取短信验证码输入参数
     * @return 获取短信验证码是否成功
     */
    public Boolean getSmsWithResetPassword(SmsVerCreatVO smsVerCreatVO){
        //参数验证
        if (StringUtil.isBlank(smsVerCreatVO.getImgId())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "图片验证码唯一标志不能为空!");
        }
        if (StringUtil.isBlank(smsVerCreatVO.getCode())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "图片验证码不能为空!");
        }
        if (StringUtil.isBlank(smsVerCreatVO.getMobile())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "用户电话不能为空!");
        }
        if (!RegexValidateUtil.checkMobileNumber(smsVerCreatVO.getMobile())){
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR,
                    "请输入正确的手机号!");
        }
        User bean = userFacade.getUserInfo(smsVerCreatVO.getMobile());
        if (bean == null){
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "手机号未注册!");
        }

        //验证是否超过三分钟
        SwsVerInfo swsVerInfoCheck = smsVerService.getSmsVerification(smsVerCreatVO.getMobile());
        if (null != swsVerInfoCheck){
            if (DateUtil.after(DateUtil.addMinutes(swsVerInfoCheck.getCreateTime(), 3000), //TODO 方便测试先加大时间间隔
                    DateUtil.now())){
                throw new CommonException(ServiceErrorCode.SMS_SEND_ERROR,
                        "同一用户三分钟之内不能重复发送验证短信，请三分钟之后再次发送!");
            }
        }

        //发送短信
        String smsTemplateCode = "SMS_143861902";
        String code = smsVerService.smsSend(smsVerCreatVO.getMobile(), smsTemplateCode);

        //创建用户短信验证信息，缓存到redis
        SwsVerInfo swsVerInfo = new SwsVerInfo();
        swsVerInfo.setMobile(smsVerCreatVO.getMobile());
        swsVerInfo.setCode(code);
        return smsVerService.createSmsVerification(swsVerInfo);
    }


    /**
     * 验证短信码
     * @param smsVerVerVO 短信验证码验证输入信息
     * @return 校验短信验证码是否通过
     */
    public Boolean verifySmsVerification(SmsVerVerVO smsVerVerVO){
        //参数验证
        if (null == smsVerVerVO){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "短信验证码验证输入信息不能为空!");
        }
//        if (StringUtil.isBlank(smsVerVerVO.getImgId())){
//            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
//                    "图片验证码唯一标志不能为空!");
//        }
//        if (StringUtil.isBlank(smsVerVerVO.getCode())){
//            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
//                    "图片验证码不能为空!");
//        }
        if (StringUtil.isBlank(smsVerVerVO.getMobile())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "用户电话不能为空!");
        }
        if (StringUtil.isBlank(smsVerVerVO.getSmsCode())){
            throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
                    "短信验证码不能为空!");
        }
        if (!RegexValidateUtil.checkMobileNumber(smsVerVerVO.getMobile())){
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR,
                    "请输入正确的手机号!");
        }
        //验证图片验证码
//        ImgVerInfo imgVerInfo = new ImgVerInfo();
//        BeanUtil.copyProperties(smsVerVerVO, imgVerInfo);
//        Boolean imgVerRes = imgVerService.verifyImgVerification(imgVerInfo);
//        if (imgVerRes){
//            User bean = userFacade.getUserInfo(smsVerVerVO.getMobile());
//            if (bean != null){
//                throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "该号码已经被注册!");
//            }
//        } else {
//            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "输入图形验证码有误!");
//        }
        //验证用户短信信息
        SwsVerInfo swsVerInfo = new SwsVerInfo();
        swsVerInfo.setMobile(smsVerVerVO.getMobile());
        swsVerInfo.setCode(smsVerVerVO.getSmsCode());
        Boolean smsVer = smsVerService.verifySmsVerification(swsVerInfo);
        if (!smsVer){
            throw new CommonException(CommonErrorCode.PARAM_IS_ERROR, "短信验证码输入有误，请重新输入!");
        }
        return true;
    }

}
