package cn.ey.biz.module.system.controller.admin.permission;

import cn.ey.biz.common.enums.CommonStatusEnum;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.common.util.i18n.I18nContextHolder;
import cn.ey.biz.framework.datapermission.core.annotation.DataPermission;
import cn.ey.biz.module.system.controller.admin.permission.vo.menu.*;
import cn.ey.biz.module.system.convert.permission.MenuConvert;
import cn.ey.biz.module.system.dal.dataobject.permission.MenuDO;
import cn.ey.biz.module.system.service.i18n.I18nService;
import cn.ey.biz.module.system.service.permission.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static cn.ey.biz.common.exception.util.ServiceExceptionUtil.exception;
import static cn.ey.biz.common.pojo.R.success;
import static cn.ey.biz.module.system.enums.ErrorCodeConstants.ACCEPT_LANGUAGE_NOT_NULL;

@Api(tags = "管理后台 - 菜单")
@RestController
@RequestMapping("/system/menu")
@Validated
public class MenuController {

    @Resource
    private MenuService menuService;
    @Resource
    private I18nService i18nService;

    @PostMapping("/create")
    @ApiOperation("创建菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:create')")
    public R<Long> createMenu(@Valid @RequestBody MenuCreateReqVO reqVO) {
        Long menuId = menuService.createMenu(reqVO);
        return success(menuId);
    }

    @PutMapping("/update")
    @ApiOperation("修改菜单")
    @PreAuthorize("@ss.hasPermission('system:menu:update')")
    public R<Boolean> updateMenu(@Valid @RequestBody MenuUpdateReqVO reqVO) {
        menuService.updateMenu(reqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除菜单")
    @ApiImplicitParam(name = "id", value = "角色编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('system:menu:delete')")
    public R<Boolean> deleteMenu(@RequestParam("id") Long id) {
        menuService.deleteMenu(id);
        return success(true);
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取菜单列表", notes = "用于【菜单管理】界面")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public R<List<MenuRespVO>> getMenus(MenuListReqVO reqVO) {
        List<MenuDO> list = menuService.getMenus(reqVO);
        list.sort(Comparator.comparing(MenuDO::getSort));
        return success(MenuConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/list-all-simple")
    @ApiOperation(value = "获取菜单精简信息列表", notes = "只包含被开启的菜单，用于【角色分配菜单】功能的选项。" +
            "在多租户的场景下，会只返回租户所在套餐有的菜单")
    @ApiImplicitParam(name = "app", value = "所属应用", required = false, example = "system", dataTypeClass = String.class)
    public R<List<MenuSimpleRespVO>> getSimpleMenus(@RequestParam(required = false, value = "app") String app) {
        // 获得菜单列表，只要开启状态的
        MenuListReqVO reqVO = new MenuListReqVO();
        reqVO.setApp(app);
        reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<MenuDO> list = menuService.getTenantMenus(reqVO);
        // 多语言转化
        list.forEach(item -> {
            item.setName(i18nService.getLabel(String.valueOf(item.getId()),
                    I18nContextHolder.getLangId(),
                    "menu"));
        });
        // 排序后，返回给前端
        list.sort(Comparator.comparing(MenuDO::getSort));
        return success(MenuConvert.INSTANCE.convertList02(list));
    }

    @GetMapping("/get")
    @ApiOperation("获取菜单信息")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    public R<MenuRespVO> getMenu(Long id) {
        MenuRespVO menu = menuService.getMenu(id);
        return success(menu);
    }

    @GetMapping("/list-menu")
    @ApiOperation(value = "获取菜单列表", notes = "用于【菜单管理】界面")
    @PreAuthorize("@ss.hasPermission('system:menu:query')")
    @DataPermission
    public R<List<MenuRespVO>> getMenusList(MenuListReqVO reqVO) {
        String acceptLanguage = I18nContextHolder.getLangId();
        if (StringUtils.isEmpty(acceptLanguage)) {
            throw exception(ACCEPT_LANGUAGE_NOT_NULL);
        }
        reqVO.setAcceptLanguage(acceptLanguage);
        List<MenuRespVO> list = menuService.getMenusList(reqVO);
        return success(list);
    }

}
