package com.cool.bang.modules.system.controller;

import com.alibaba.druid.util.StringUtils;
import com.cool.bang.common.base.BaseController;
import com.cool.bang.common.constant.ControllerConstant;
import com.cool.bang.common.constant.emuns.StatusType;
import com.cool.bang.common.web.response.Result;
import com.cool.bang.common.web.response.module.ResultTable;
import com.cool.bang.modules.system.domain.po.Menu;
import com.cool.bang.modules.system.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * system menu controller
 *
 * @author PeiZhi Guo
 * 2021-2-8 15:00
 */
@RestController
@RequestMapping(ControllerConstant.SYSTEM + "menu/v1")
public class MenuController extends BaseController {

    private IMenuService menuService;

    @Autowired
    public MenuController(IMenuService menuService) {
        this.menuService = menuService;
    }

    @GetMapping
    @PreAuthorize("hasPermission('','sys:menu:main')")
    public ResultTable main(Menu menu) {
        menu.setDelFlag(StatusType.ENABLE.ordinal());
        return treeTable(menuService.list(menu));
    }

    @GetMapping("{id}")
    @PreAuthorize("hasPermission('','sys:menu:main')")
    public Result get(@PathVariable Long id) {
        return success(menuService.queryById(id));
    }

    @GetMapping("selectTree")
    @PreAuthorize("hasPermission('','sys:menu:main')")
    public Result selectTree() {
        return success(menuService.querySelectTree());
    }

    @GetMapping("/owns")
    public Set<String> ownsPermission() {
        List<Menu> permissions = getCurrentUser().getMenuList();
        if (CollectionUtils.isEmpty(permissions)) {
            return Collections.emptySet();
        }
        return permissions.parallelStream().filter(p -> !StringUtils.isEmpty(p.getCode()))
                .map(Menu::getCode).collect(Collectors.toSet());
    }

    @PostMapping
    @PreAuthorize("hasPermission('','sys:menu:add')")
    public Result save(Menu menu) {
        if (menuService.checkMenuNameUnique(menu)) {
            return failure("新增菜单'" + menu.getTitle() + "'失败，菜单名称已存在");
        }
        menu.insertConfigure();
        return decide(menuService.save(menu));
    }

    @PutMapping
    @PreAuthorize("hasPermission('','sys:menu:edit')")
    public Result edit(Menu menu) {
        if (menuService.checkMenuNameUnique(menu)) {
            return failure("新增菜单'" + menu.getTitle() + "'失败，菜单名称已存在");
        }
        menu.updateConfigure();
        return decide(menuService.update(menu));
    }

    @PutMapping("switch")
    @PreAuthorize("hasPermission('','sys:menu:stop')")
    public Result editType(Integer id, Integer type) {
        if (menuService.hasChildByMenuId(id)) {
            return Result.failure("存在子菜单数据");
        }
        return decide(menuService.updateEnableById(id, type));
    }
}
