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

import cn.reghao.jutil.jdk.result.Result;
import cn.reghao.oss.console.account.db.query.MenuQuery;
import cn.reghao.oss.console.account.model.dto.MenuDTO;
import cn.reghao.oss.console.account.model.po.Menu;
import cn.reghao.oss.console.account.service.impl.MenuServiceImpl;
import cn.reghao.jutil.jdk.result.WebResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "资源接口")
@RequestMapping("/api/rbac/menu")
@RestController
public class MenuController {
    private final MenuServiceImpl menuServiceImpl;
    private final MenuQuery menuQuery;

    public MenuController(MenuServiceImpl menuServiceImpl, MenuQuery menuQuery) {
        this.menuServiceImpl = menuServiceImpl;
        this.menuQuery = menuQuery;
    }

    @ApiOperation(value = "添加资源")
    @PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
    public String addMenu(@Validated Menu menu) {
        Result result = menuServiceImpl.addMenu(menu);
        return WebResult.result(result);
    }

    @ApiOperation(value = "修改资源")
    @PostMapping(value = "/edit", produces = MediaType.APPLICATION_JSON_VALUE)
    public String modifyMenu(@Validated MenuDTO menuDTO) {
        Result result = menuServiceImpl.updateMenu(menuDTO);
        return WebResult.result(result);
    }

    @ApiOperation(value = "修改资源状态")
    @PostMapping(value = "/status/{enabled}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String menuStatus(@PathVariable("enabled") boolean enabled, @RequestParam("ids") List<Integer> ids) {
        menuServiceImpl.updateMenusStatus(enabled, ids);
        return WebResult.success();
    }

    @ApiOperation(value = "删除资源")
    @DeleteMapping(value = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteMenu(@PathVariable("id") Integer menuId) {
        Result result = menuServiceImpl.deleteMenu(menuId);
        return WebResult.result(result);
    }

    @ApiOperation(value = "获取指定状态的菜单")
    @GetMapping(value = "/{isEnabled}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String list(@PathVariable(value = "isEnabled") Boolean isEnabled) {
        List<Menu> list = menuQuery.getSortedMenusByStatus(isEnabled).stream()
                .peek(menu -> menu.getRoles().forEach(role -> {
                    role.setMenus(null);
                }))
                .collect(Collectors.toList());
        return WebResult.success(list);
    }

    @ApiOperation(value = "对同一 pid 组内的资源进行排序")
    @GetMapping(value = "/sorted/{pid}/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String sortList(@PathVariable(value = "pid") int pid,
                           @PathVariable(value = "id", required = false) Menu menu) {
        Map<Integer, String> map = menuQuery.getSortedChildGroupByPid(pid);
        // 排除当前 menu
        if (menu != null) {
            map.remove(menu.getPos());
        }
        return WebResult.success(map);
    }
}
