package ink.wgink.login.app.controller.app.api.appsign;

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.login.app.pojo.vos.appsign.AppLoginDefaultVO;
import ink.wgink.login.app.pojo.vos.appsign.AppLoginPhoneVO;
import ink.wgink.login.app.service.appsign.IAppSignService;
import ink.wgink.pojo.result.ErrorResult;
import ink.wgink.pojo.result.SuccessResultData;
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;

/**
 * When you feel like quitting. Think about why you started
 * 当你想要放弃的时候，想想当初你为何开始
 *
 * @ClassName: AppSignController
 * @Description: 登录
 * @Author: wanggeng
 * @Date: 2021/4/7 3:55 下午
 * @Version: 1.0
 */
@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "登录")
@RestController
@RequestMapping(ISystemConstant.APP_PREFIX + "/sign")
public class AppSignAppController extends DefaultBaseController {

    @Autowired
    private IAppSignService appSignService;
    @Autowired(required = false)
    private ISmsBaseService smsBaseService;

    @ApiOperation(value = "APP用户名密码登录", notes = "APP用户名密码登录接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("default")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResultData<String> defaultSign(@RequestBody AppLoginDefaultVO appLoginDefaultVO) throws Exception {
        return new SuccessResultData<>(appSignService.defaultSign(appLoginDefaultVO));
    }

    @ApiOperation(value = "APP手机验证码登录", notes = "APP手机验证码登录接口")
    @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)})
    @PostMapping("phone")
    @CheckRequestBodyAnnotation
    public synchronized SuccessResultData<String> phone(@RequestBody AppLoginPhoneVO appLoginPhoneVO) throws Exception {
        if(smsBaseService == null) {
            throw new DependencyException("短信依赖未引入");
        }
        if (!RegexUtil.isPhone(appLoginPhoneVO.getUsername())) {
            throw new ParamsException("用户名非手机格式");
        }
        String verifyCode = smsBaseService.getVerifyCode(appLoginPhoneVO.getUsername());
        if (StringUtils.isBlank(verifyCode)) {
            throw new ParamsException("未发送验证码");
        }
        if (!StringUtils.equalsIgnoreCase(verifyCode, appLoginPhoneVO.getVerificationCode())) {
            throw new ParamsException("验证码错误");
        }
        return new SuccessResultData<>(appSignService.phoneSign(appLoginPhoneVO));
    }

}
