package com.zanxiang.game.module.manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zanxiang.erp.security.annotation.PreAuthorize;
import com.zanxiang.game.module.base.pojo.enums.GameAuthEnum;
import com.zanxiang.game.module.manage.pojo.params.*;
import com.zanxiang.game.module.manage.pojo.vo.GameAuthRoleVO;
import com.zanxiang.game.module.manage.pojo.vo.GameAuthVO;
import com.zanxiang.game.module.manage.service.IGameAuthRoleService;
import com.zanxiang.game.module.manage.service.IGameAuthService;
import com.zanxiang.module.util.pojo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author : lingfeng
 * @time : 2023-08-17
 * @description : 游戏授权
 */
@Api(tags = {"游戏授权接口"})
@RestController
@RequestMapping("/game/auth")
@Slf4j
public class GameAuthController {

    @Autowired
    private IGameAuthService gameAuthService;

    @Autowired
    private IGameAuthRoleService gameAuthRoleService;

    @ApiOperation(value = "新增用户授权")
    @PostMapping(value = "/add")
    @PreAuthorize(permissionKey = "manage:gameAuth:add")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Boolean> gameAuthAdd(@Validated @RequestBody GameAuthAddParam param) {
        return ResultVO.ok(gameAuthService.gameAuthAdd(param));
    }

    @ApiOperation(value = "更新用户授权")
    @PostMapping(value = "/update")
    @PreAuthorize(permissionKey = "manage:gameAuth:update")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Boolean> gameAuthUpdate(@Validated @RequestBody GameAuthUpdateParam param) {
        return ResultVO.ok(gameAuthService.gameAuthUpdate(param));
    }

    @ApiOperation(value = "游戏授权用户列表")
    @PostMapping(value = "/list")
    @PreAuthorize(permissionKey = "manage:gameAuth:list")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = GameAuthVO.class)})
    public ResultVO<IPage<GameAuthVO>> listOfPage(@Validated @RequestBody GameAuthListParam param) {
        return ResultVO.ok(gameAuthService.listOfPage(param));
    }

    @ApiOperation(value = "游戏授权用户删除")
    @DeleteMapping(value = "/delete")
    @PreAuthorize(permissionKey = "manage:gameServer:delete")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Boolean> deleteById(@RequestParam Long id) {
        return ResultVO.ok(gameAuthService.deleteById(id));
    }

    @ApiOperation(value = "新增用户角色")
    @PostMapping(value = "/add/role")
    @PreAuthorize(permissionKey = "manage:gameAuth:addRole")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Boolean> gameAuthRoleAdd(@Validated @RequestBody GameAuthRoleAddParam param) {
        return ResultVO.ok(gameAuthRoleService.gameAuthRoleAdd(param));
    }

    @ApiOperation(value = "更新用户角色")
    @PostMapping(value = "/update/role")
    @PreAuthorize(permissionKey = "manage:gameAuth:updateRole")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Boolean> gameAuthRoleUpdate(@Validated @RequestBody GameAuthRoleUpdateParam param) {
        return ResultVO.ok(gameAuthRoleService.gameAuthRoleUpdate(param));
    }

    @ApiOperation(value = "删除用户角色")
    @DeleteMapping(value = "/delete/role")
    @PreAuthorize(permissionKey = "manage:gameServer:deleteRole")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Boolean> roleDeleteById(@RequestParam Long id) {
        return ResultVO.ok(gameAuthRoleService.deleteById(id));
    }

    @ApiOperation(value = "用户授权角色列表")
    @PostMapping(value = "/list/role")
    @PreAuthorize(permissionKey = "manage:gameAuth:listRole")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = GameAuthRoleVO.class)})
    public ResultVO<IPage<GameAuthRoleVO>> roleListOfPage(@Validated @RequestBody GameAuthRoleListParam param) {
        return ResultVO.ok(gameAuthRoleService.listOfPage(param));
    }

    @ApiOperation(value = "获取权限类型枚举")
    @GetMapping(value = "/role/auth/type")
    @PreAuthorize(permissionKey = "manage:gameServer:roleAuthType")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Map<String, String>> getGameAuthMap() {
        return ResultVO.ok(GameAuthEnum.getGameAuthMap());
    }

    @ApiOperation(value = "获取权限类型相关用户")
    @GetMapping(value = "/role/auth/user")
    @PreAuthorize(permissionKey = "manage:gameServer:roleAuthUser")
    @ApiResponses(value = {@ApiResponse(code = 200, message = "成功", response = Boolean.class)})
    public ResultVO<Map<Long, String>> getUserByAuthType(@RequestParam String authType) {
        return ResultVO.ok(gameAuthRoleService.getUserByAuthType(authType));
    }
}
