package ink.wgink.register.base.controller.app.api;

import ink.wgink.annotation.CheckRequestBodyAnnotation;
import ink.wgink.common.base.DefaultBaseController;
import ink.wgink.exceptions.DependencyException;
import ink.wgink.exceptions.ParamsException;
import ink.wgink.interfaces.consts.ISystemConstant;
import ink.wgink.interfaces.sms.ISmsBaseService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResult;
import ink.wgink.register.base.pojo.vos.RegisterDefaultVO;
import ink.wgink.register.base.pojo.vos.RegisterDefaultWithExpandInfoVO;
import ink.wgink.register.base.pojo.vos.RegisterPhoneVO;
import ink.wgink.register.base.pojo.vos.RegisterPhoneWithExpandInfoVO;
import ink.wgink.register.base.service.IRegisterService;
import ink.wgink.util.RegexUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: RegisterController
 * @Description: 注册
 * @Author: wanggeng
 * @Date: 2021/4/29 6:12 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "注册")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/register")
public class RegisterAppController extends DefaultBaseController {

    @Autowired(required = false)
    private ISmsBaseService smsBaseService;
    @Autowired
    private IRegisterService registerService;

    @ApiOperation(value = "默认注册", notes = "默认注册，用户名密码注册接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("default")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResult defaultRegister(@RequestBody RegisterDefaultVO registerDefaultVO) throws Exception {
        checkDefaultRegisterParams(registerDefaultVO);
        Map<String, Object> params = requestParams();
        registerService.registerDefault(registerDefaultVO, params);
        return new SuccessResult();
    }

    @ApiOperation(value = "手机注册", notes = "手机注册，手机 + 短信验证码接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("phone")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResult phone(@RequestBody RegisterPhoneVO registerPhoneVO) throws Exception {
        checkPhoneRegisterParams(registerPhoneVO);
        Map<String, Object> params = requestParams();
        registerService.registerPhone(registerPhoneVO, params);
        return new SuccessResult();
    }


    @ApiOperation(value = "默认注册（有拓展信息）", notes = "默认注册（有拓展信息），用户名密码注册接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("default-with-expand-info")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResult defaultRegisterWithExpandInfo(@RequestBody RegisterDefaultWithExpandInfoVO registerDefaultWithExpandInfoVO) throws Exception {
        checkDefaultRegisterParams(registerDefaultWithExpandInfoVO);
        if (registerDefaultWithExpandInfoVO.getExpandInfo() == null || registerDefaultWithExpandInfoVO.getExpandInfo().isEmpty()) {
            throw new ParamsException("拓展信息不能为空");
        }
        Map<String, Object> params = requestParams();
        registerService.registerDefaultWithExpandInfo(registerDefaultWithExpandInfoVO, params);
        return new SuccessResult();
    }

    @ApiOperation(value = "手机注册（有拓展信息）", notes = "手机注册（有拓展信息），手机 + 短信验证码接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("phone-with-expand-info")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResult phoneWithExpandInfo(@RequestBody RegisterPhoneWithExpandInfoVO registerPhoneWithExpandInfoVO) throws Exception {
        checkPhoneRegisterParams(registerPhoneWithExpandInfoVO);
        if (registerPhoneWithExpandInfoVO.getExpandInfo() == null || registerPhoneWithExpandInfoVO.getExpandInfo().isEmpty()) {
            throw new ParamsException("拓展信息不能为空");
        }
        Map<String, Object> params = requestParams();
        registerService.registerPhoneWithExpandInfo(registerPhoneWithExpandInfoVO, params);
        return new SuccessResult();
    }

    /**
     * 验证默认注册参数
     *
     * @param registerDefaultVO
     */
    private void checkDefaultRegisterParams(RegisterDefaultVO registerDefaultVO) {
        if (!RegexUtil.isUsername(registerDefaultVO.getUsername())) {
            throw new ParamsException("用户名只能包含字母、数字、-_!@#%.");
        }
        if (!StringUtils.equals(registerDefaultVO.getPassword(), registerDefaultVO.getPasswordSame())) {
            throw new ParamsException("两次密码不一致");
        }
        if (!RegexUtil.isPasswordMiddle(registerDefaultVO.getPassword())) {
            throw new ParamsException("密码强度不够, 密码6到16位，包含数字、字母、特殊字符，其中的任意两种");
        }
    }

    /**
     * 验证手机注册参数
     *
     * @param registerPhoneVO
     */
    private void checkPhoneRegisterParams(RegisterPhoneVO registerPhoneVO) {
        if (smsBaseService == null) {
            throw new DependencyException("短信依赖未引入");
        }
        String verifyCode = smsBaseService.getVerifyCode(registerPhoneVO.getPhone());
        if (StringUtils.isBlank(verifyCode)) {
            throw new ParamsException("验证码无效");
        }
        if (!StringUtils.equals(verifyCode, registerPhoneVO.getVerifyCode())) {
            throw new ParamsException("验证码错误");
        }
    }

}
