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 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 : 游戏授权
 */
@RestController
@RequestMapping("/game/auth")
@Slf4j
public class GameAuthController {

    @Autowired
    private IGameAuthService gameAuthService;

    @Autowired
    private IGameAuthRoleService gameAuthRoleService;

    @PostMapping(value = "/add")
    @PreAuthorize(permissionKey = "manage:gameAuth:add")
    public ResultVO<Boolean> gameAuthAdd(@Validated @RequestBody GameAuthAddParam param) {
        return ResultVO.ok(gameAuthService.gameAuthAdd(param));
    }

    @PostMapping(value = "/update")
    @PreAuthorize(permissionKey = "manage:gameAuth:update")
    public ResultVO<Boolean> gameAuthUpdate(@Validated @RequestBody GameAuthUpdateParam param) {
        return ResultVO.ok(gameAuthService.gameAuthUpdate(param));
    }

    @PostMapping(value = "/list")
    @PreAuthorize(permissionKey = "manage:gameAuth:list")
    public ResultVO<IPage<GameAuthVO>> listOfPage(@Validated @RequestBody GameAuthListParam param) {
        return ResultVO.ok(gameAuthService.listOfPage(param));
    }

    @DeleteMapping(value = "/delete")
    @PreAuthorize(permissionKey = "manage:gameServer:delete")
    public ResultVO<Boolean> deleteById(@RequestParam Long id) {
        return ResultVO.ok(gameAuthService.deleteById(id));
    }

    @PostMapping(value = "/add/role")
    @PreAuthorize(permissionKey = "manage:gameAuth:addRole")
    public ResultVO<Boolean> gameAuthRoleAdd(@Validated @RequestBody GameAuthRoleAddParam param) {
        return ResultVO.ok(gameAuthRoleService.gameAuthRoleAdd(param));
    }

    @PostMapping(value = "/update/role")
    @PreAuthorize(permissionKey = "manage:gameAuth:updateRole")
    public ResultVO<Boolean> gameAuthRoleUpdate(@Validated @RequestBody GameAuthRoleUpdateParam param) {
        return ResultVO.ok(gameAuthRoleService.gameAuthRoleUpdate(param));
    }

    @DeleteMapping(value = "/delete/role")
    @PreAuthorize(permissionKey = "manage:gameServer:deleteRole")
    public ResultVO<Boolean> roleDeleteById(@RequestParam Long id) {
        return ResultVO.ok(gameAuthRoleService.deleteById(id));
    }

    @PostMapping(value = "/list/role")
    @PreAuthorize(permissionKey = "manage:gameAuth:listRole")
    public ResultVO<IPage<GameAuthRoleVO>> roleListOfPage(@Validated @RequestBody GameAuthRoleListParam param) {
        return ResultVO.ok(gameAuthRoleService.listOfPage(param));
    }

    @GetMapping(value = "/role/auth/type")
    @PreAuthorize(permissionKey = "manage:gameServer:roleAuthType")
    public ResultVO<Map<String, String>> getGameAuthMap() {
        return ResultVO.ok(GameAuthEnum.getGameAuthMap());
    }

    @GetMapping(value = "/role/auth/user")
    @PreAuthorize(permissionKey = "manage:gameServer:roleAuthUser")
    public ResultVO<Map<Long, String>> getUserByAuthType(@RequestParam String authType) {
        return ResultVO.ok(gameAuthRoleService.getUserByAuthType(authType));
    }
}
