package com.sonic.frog.controller.web;

import com.sonic.bear.lib.client.UserNicknamePoolClient;
import com.sonic.bear.lib.input.CompleteUserInfoInput;
import com.sonic.bear.lib.input.EditUserInfoInput;
import com.sonic.bear.lib.input.UserNicknameExistCheckInput;
import com.sonic.bear.lib.output.BaseUserInfoOutput;
import com.sonic.common.auth.domains.Session;
import com.sonic.common.rpc.Result;
import com.sonic.cow.lib.client.NsfwCheckClient;
import com.sonic.frog.domain.output.UserBaseInfoOutput;
import com.sonic.frog.domain.output.UserCreateCountOutput;
import com.sonic.frog.service.UserCreateCountStatService;
import com.sonic.frog.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
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.RestController;

import javax.validation.Valid;

/**
 * Web-用户管理
 *
 * @author code
 */
@RestController
@Slf4j
public class UserWeb {

    @Autowired
    private UserService userService;
    @Autowired
    private UserNicknamePoolClient userNicknamePoolClient;
    @Autowired
    private UserCreateCountStatService userCreateCountStatService;
    @Autowired
    private NsfwCheckClient nsfwCheckClient;

    @ApiOperation(value = "获取用户自己的基础信息", tags = {"Web-用户管理"})
    @PostMapping("/web/user/base-info")
    public Result<UserBaseInfoOutput> baseInfo(@ApiParam(hidden = true) Session session) {
        return Result.success(userService.getbaseUserInfo(session.getUserId()));
    }

    @ApiOperation(value = "完善用户基础信息", tags = {"Web-用户管理"})
    @PostMapping("/web/user/complete-user-info")
    public Result<BaseUserInfoOutput> completeUserInfo(@Valid @RequestBody CompleteUserInfoInput input, @ApiParam(hidden = true) Session session) {
        input.setUserId(session.getUserId());
        userService.completeUserInfo(input);
        return Result.success();
    }

    @ApiOperation(value = "修改用户基础信息", tags = {"Web-用户管理"})
    @PostMapping("/web/user/edit-user-info")
    public Result<Void> editUserInfo(@Valid @RequestBody EditUserInfoInput input, @ApiParam(hidden = true) Session session) {
        input.setUserId(session.getUserId());
        userService.editUserInfo(input);
        return Result.success();
    }

    @ApiOperation(value = "删除账号", tags = {"Web-用户管理"})
    @PostMapping("/web/user/del")
    public Result<Void> delAccount(@ApiParam(hidden = true) Session session) {
        userService.delAccount(session.getUserId());
        return Result.success();
    }

    @ApiOperation(value = "校验昵称是否已经存在", tags = {"Web-用户管理"})
    @PostMapping("/web/user/nickname-check")
    public Result<Boolean> nicknameCheck(@Valid @RequestBody UserNicknameExistCheckInput input,  @ApiParam(hidden = true) Session session) {
        //敏感词校验
        nsfwCheckClient.checkContent(input.getNickname());
        return Result.success(userNicknamePoolClient.userNicknameExistCheck(input.getExUserId() == null ? session.getUserId() : input.getExUserId(), input.getNickname()));
    }


    @ApiOperation(value = "获取用户相册创作次数", tags = {"Web-用户管理"})
    @PostMapping("/web/user/get-user-create-count")
    public Result<UserCreateCountOutput> getUserCreateCount(@ApiParam(hidden = true) Session session) {
        return Result.success(userCreateCountStatService.getUserCreateCount(session.getUserId()));
    }

}
