package com.izouma.nineth.web.mobile.open.v1;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.anji.captcha.util.StringUtils;

import com.izouma.nineth.aspect.CipherParam;
import com.izouma.nineth.aspect.RequestCipher;
import com.izouma.nineth.domain.User;
import com.izouma.nineth.security.JwtTokenUtil;
import com.izouma.nineth.security.JwtUserFactory;
import com.izouma.nineth.service.UserRegisterService;
import com.izouma.nineth.service.UserService;
import com.izouma.nineth.web.BaseController;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@AllArgsConstructor
@RestController
@RequestMapping("/auth/v1")
public class AuthenticationControllerOpenV1 extends BaseController {
    private final JwtTokenUtil          jwtTokenUtil;
    private final UserService           userService;
    private final UserRegisterService userRegisterService;

    @PostMapping("/phoneLogin")
    @ApiOperation(value = "手机号验证码登录")
    @RequestCipher
    public String phoneLogin(@CipherParam String encrytorData) {
        JSONObject jsonObject = JSON.parseObject(encrytorData);
        String phone = jsonObject.getString("phone");
        String code = jsonObject.getString("code");
        User user = userRegisterService.loginByPhone(phone, code);
        String appClientId = jsonObject.getString("appClientId");
        //设置app客户端唯一ID
        if(StringUtils.isNotEmpty(appClientId)){
            userService.updateAppClientId(user.getId(), appClientId);
            user.setAppClientId(appClientId);
        }

        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/phonePwdLogin")
    @ApiOperation(value = "手机号密码登录")
    @RequestCipher
    public String phonePwdLogin(@CipherParam String encrytorData) {
        JSONObject jsonObject = JSON.parseObject(encrytorData);
        String phone = jsonObject.getString("phone");
        String password = jsonObject.getString("password");
        User user = userService.loginByPhonePwd(phone, password);
        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

    @PostMapping("/phoneRegister")
    @ApiOperation(value = "手机号密码注册")
    @RequestCipher  
    public String phoneRegister(@CipherParam String encrytorData) {
        JSONObject jsonObject = JSON.parseObject(encrytorData);
        String phone = jsonObject.getString("phone");
        String password = jsonObject.getString("password");
        String code = jsonObject.getString("code");
        String inviteCode = jsonObject.getString("inviteCode");
        Long invitor = jsonObject.getLong("invitor");
        User user = userRegisterService.phoneRegister(phone, code, password, inviteCode, invitor);
        String appClientId = jsonObject.getString("appClientId");
        //设置app客户端唯一ID
        if(StringUtils.isNotEmpty(appClientId)){
            userService.updateAppClientId(user.getId(), appClientId);
            user.setAppClientId(appClientId);
        }

        return jwtTokenUtil.generateToken(JwtUserFactory.create(user));
    }

}
