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

import cn.reghao.oss.console.account.db.query.MenuQuery;
import cn.reghao.oss.console.account.db.query.RoleQuery;
import cn.reghao.oss.console.account.model.dto.MenuDTO;
import cn.reghao.oss.console.account.model.po.Role;
import cn.reghao.oss.console.account.model.po.Menu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Api(tags = "资源页面")
@RequestMapping("/rbac/menu")
@Controller
public class MenuPageController {
    private final RoleQuery roleQuery;
    private final MenuQuery menuQuery;

    public MenuPageController(RoleQuery roleQuery, MenuQuery menuQuery) {
        this.roleQuery = roleQuery;
        this.menuQuery = menuQuery;
    }

    @ApiOperation(value = "资源列表页面")
    @GetMapping
    public String menuPage(@RequestParam(value = "enabled", required = false) Boolean enabled, Model model) {
        if (enabled == null) {
            enabled = true;
        }

        model.addAttribute("enabled", enabled);
        return "/rbac/menu/index";
    }

    @ApiOperation(value = "资源添加页面")
    @GetMapping({"/add", "/add/{pid}"})
    public String toAdd(@PathVariable(value = "pid", required = false) Integer pid, Model model) {
        // 父级菜单
        Menu pMenu = null;
        if (pid != null) {
            pMenu = menuQuery.findById(pid);
        }

        Set<Role> allRoles = new HashSet<>(roleQuery.findAll());
        Set<Role> menuRoles = Collections.emptySet();

        model.addAttribute("allRoles", allRoles);
        model.addAttribute("menuRoles", menuRoles);
        model.addAttribute("pMenu", pMenu);
        return "/rbac/menu/add";
    }

    @ApiOperation(value = "资源编辑页面")
    @GetMapping("/edit/{id}")
    public String toEdit(@PathVariable("id") Menu menu, Model model) {
        int pid = menu.getPid();
        Menu pMenu;
        if (pid == 0) {
            pMenu = new Menu(pid, "根菜单");
        } else {
            pMenu = menuQuery.findById(pid);
        }

        Set<Role> allRoles = new HashSet<>(roleQuery.findAll());
        //Set<Role> menuRoles = menu.getRoles();
        Set<Role> menuRoles = Collections.emptySet();

        model.addAttribute("allRoles", allRoles);
        model.addAttribute("menuRoles", menuRoles);
        model.addAttribute("menu", new MenuDTO(menu));
        model.addAttribute("pMenu", pMenu);
        return "/rbac/menu/edit";
    }

    // TODO Hibernate 会根据传入的 id 自动查找相应的 Menu
    @ApiOperation(value = "可访问资源的角色列表页面")
    @GetMapping("/roleList/{id}")
    public String roleListWithResource(@PathVariable("id") Menu menu, Model model) {
        List<Role> list = new ArrayList<>(menu.getRoles());
        model.addAttribute("list", list);
        return "/rbac/menu/roles";
    }

    @ApiOperation(value = "资源详细信息页面")
    @GetMapping("/detail/{id}")
    public String toDetail(@PathVariable("id") Menu menu, Model model) {
        model.addAttribute("menu", menu);
        return "/rbac/menu/detail";
    }
}
