package cn.reghao.oss.console.account.controller;

import cn.reghao.oss.console.account.model.dto.UpdatePasswordDto;
import cn.reghao.oss.console.account.model.dto.CreateAccountDto;
import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.jutil.jdk.result.WebResult;
import cn.reghao.oss.console.account.model.dto.AccountProfile;
import cn.reghao.oss.console.account.model.dto.AccountRole;
import cn.reghao.oss.console.account.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "用户接口")
@RequestMapping("/api/rbac/user")
@RestController
public class AccountProfileController {
    private final AccountService accountService;

    public AccountProfileController(AccountService accountService) {
        this.accountService = accountService;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "创建用户")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String createUser(@Validated CreateAccountDto createAccountDto) {
        Result result = accountService.createAccount(createAccountDto);
        return WebResult.result(result);
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "批量创建用户")
    @PostMapping(value = "/batch", produces = MediaType.APPLICATION_JSON_VALUE)
    public String batchAdd(MultipartFile file) {
        return WebResult.success();
    }

    @PostMapping(value = "/avatar", produces = MediaType.APPLICATION_JSON_VALUE)
    public String userAvatar(MultipartFile file) {
        return WebResult.success();
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping(value = "/modify", produces = MediaType.APPLICATION_JSON_VALUE)
    @Deprecated
    public String updateAccountProfile(@Validated AccountProfile accountProfile) {
        accountService.updateAccountProfile(accountProfile);
        return WebResult.success();
    }

    @ApiOperation(value = "修改用户信息")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String editUserInfo(@Validated AccountProfile accountProfile) {
        accountService.updateAccountProfile(accountProfile);
        return WebResult.success();
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @ApiOperation(value = "删除用户")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteAccount(@PathVariable("id") Integer userId) {
        accountService.deleteAccount(userId);
        return WebResult.success();
    }

    @ApiOperation(value = "修改用户密码")
    @PostMapping(value = "/passwd", produces = MediaType.APPLICATION_JSON_VALUE)
    public String modifyPassword(@NotNull Integer id, @NotNull String newPassword) {
        accountService.updateAccountPassword(id, newPassword);
        return WebResult.success();
    }

    @PostMapping(value = "/passwd/update", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String editPasswd(UpdatePasswordDto updatePasswordDto) {
        return WebResult.success();
    }

    @ApiOperation(value = "分配用户角色")
    @PostMapping(value = "/role", produces = MediaType.APPLICATION_JSON_VALUE)
    public String assignRole(@Validated AccountRole accountRole) {
        accountService.updateAccountRole(accountRole);
        return WebResult.success();
    }

    // TODO 暂不启用本功能
    @ApiOperation(value = "启用/禁用用户")
    @PostMapping(value = "/status/{enable}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateAccountStatus(@PathVariable("enable") Boolean enable,
                                @RequestParam(value = "ids") List<Integer> userIds) {
        userIds.forEach(userId -> accountService.updateAccountStatus(userId, enable));
        return WebResult.success();
    }
}
