package com.red.circle.console.adapter.app.user;


import cn.hutool.core.util.ObjectUtil;
import com.red.circle.console.app.dto.clienobject.user.UserAuthDetailsCO;
import com.red.circle.console.app.service.app.user.UserAuthTypeService;
import com.red.circle.console.infra.annotations.OpsOperationReqLog;
import com.red.circle.framework.core.security.UserCredential;
import com.red.circle.framework.web.controller.BaseController;
import com.red.circle.other.inner.endpoint.user.user.AppUserAccountClient;
import com.red.circle.other.inner.model.cmd.user.account.ResetPasswordCmd;
import com.red.circle.other.inner.model.dto.user.UserAuthTypeDTO;
import com.red.circle.other.inner.model.dto.user.account.UserMobileUpdateCmd;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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;

/**
 * <p>
 * 用户认证信息 前端控制器
 * </p>
 *
 * @author pengliang
 * @since 2019-11-08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/user/auth/type", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserAuthTypeRestController extends BaseController {

  private final UserAuthTypeService userAuthTypeService;
  private final AppUserAccountClient appUserAccountClient;

  /**
   * 获取注册信息.
   */
  @GetMapping("/{userId}")
  public UserAuthTypeDTO updateUserInfo(@PathVariable("userId") Long userId) {
    return userAuthTypeService.updateUserInfo(userId);
  }

  /**
   * 用户登录令牌.
   */
  @GetMapping("/longin/token")
  public String token(Long userId) {
    UserCredential token = userAuthTypeService.token(userId);
    if(ObjectUtil.isNotEmpty(token)){
      return token.token();
    }
    return null;
  }

  @OpsOperationReqLog("用户登录令牌-删除")
  @DeleteMapping("/longin/token")
  public void tokenDel(Long userId) {
    userAuthTypeService.tokenDel(userId);
  }

  /**
   * 获得认证详情.
   */
  @GetMapping("/details/{userId}")
  public UserAuthDetailsCO getUserAuthDetails(@PathVariable("userId") Long userId) {
    return userAuthTypeService.getUserAuthDetails(userId);
  }

  /**
   * 修改绑定手机认证信息.
   */
  @OpsOperationReqLog("绑定手机认证信息-修改")
  @PostMapping("/edit/mobile-auth")
  public void editUserMobileAuth(@RequestBody @Validated
  UserMobileUpdateCmd param) {
    userAuthTypeService.editUserMobileAuth(param);
  }

  /**
   * 删除绑定手机认证信息.
   */
  @OpsOperationReqLog("绑定手机认证信息-删除")
  @GetMapping("/delete/mobile-auth/{userId}")
  public void deleteUserMobileAuth(@PathVariable("userId") Long userId) {
    userAuthTypeService.deleteUserMobileAuth(userId);
  }

  /**
   * 重置用户账号密码登录信息.
   */
  @OpsOperationReqLog("重置用户账号密码登录信息")
  @GetMapping("/init/account-auth/{userId}")
  public void initUserAccountLogin(@PathVariable("userId") Long userId) {
    userAuthTypeService.initUserAccountLogin(userId);
  }


  /**
   * 重置用户密码为默认密码
   *
   * @eo.name 重置用户密码
   * @eo.url /account/resetPassword
   * @eo.method post
   * @eo.request-type json
   */
  @OpsOperationReqLog("重置用户密码")
  @PostMapping("/account/resetPassword")
  public void resetPassword(@RequestBody @Validated ResetPasswordCmd cmd) {
    appUserAccountClient.resetPassword(cmd);
  }

  /**
   * 删除用户账号密码登录信息.
   */
  @OpsOperationReqLog("删除用户账号密码登录信息")
  @GetMapping("/delete/account-auth/{userId}")
  public void deleteUserAccountLogin(@PathVariable("userId") Long userId) {
    userAuthTypeService.deleteUserAccountLogin(userId);
  }

  /**
   * 重置用户支付密码.
   */
  @OpsOperationReqLog("重置用户账号密码登录信息")
  @GetMapping("/init/pay-auth/{userId}")
  public void initUserPayAuth(@PathVariable("userId") Long userId) {
    userAuthTypeService.initUserPayAuth(userId);
  }

  /**
   * 删除用户支付密码.
   */
  @OpsOperationReqLog("删除用户账号密码登录信息")
  @GetMapping("/delete/pay-auth/{userId}")
  public void deleteUserPayAuth(@PathVariable("userId") Long userId) {
    userAuthTypeService.deleteUserPayAuth(userId);
  }


}

