package com.iformall.controller.sys;

import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.po.MallPermission;
import com.iformall.domain.po.MallRole;
import com.iformall.domain.po.MallRolePermission;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.enums.EnumMenuType;
import com.iformall.enums.EnumPermissionType;
import com.iformall.exception.MallinkException;
import com.iformall.service.MallPermissionService;
import com.iformall.service.MallRolePermissionService;
import com.iformall.service.MallRoleService;
import com.iformall.service.MallUserInfoService;
import com.iformall.utils.Constant;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.condition.PatternsRequestCondition;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

/**
 * @author Stormeye Wu
 * @date 2019-04-20.
 */
@RestController
@RequestMapping("menu")
public class SysMenuController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MallPermissionService mallPermissionService;

    @Autowired
    private MallUserInfoService mallUserInfoService;

    @ApiOperation("导航菜单")
    @GetMapping("/nav")
    @SystemControllerLog(description = "菜单-导航菜单")
    public ResultData nav(){
        logger.debug("[" + getIpAddr() + "] MallPermissionController::nav");
        MallUserInfo user = getUser();
        List<MallPermission> menuList = mallPermissionService.getUserMenuList(user, 0L, true);
        Set<String> permissions = mallUserInfoService.getUserPermissions(user);
        Map map = new HashMap<>();
        map.put("menuList", menuList);
        map.put("permissions", permissions);
        return new ResultData(map);
    }

    @ApiOperation("所有菜单列表")
    @GetMapping("list")
    //@RequiresPermissions("sys:menu:list")
    @SystemControllerLog(description = "菜单-所有菜单列表")
    public ResultData getList() {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::list");
        MallUserInfo user = getUser();
        List<MallPermission> menuList = mallPermissionService.getUserMenuList(user, 0L, false);
        Set<String> permissions = mallUserInfoService.getUserPermissions(user);
        Map map = new HashMap<>();
        map.put("menuList", menuList);
        map.put("permissions", permissions);
        return new ResultData(map);
    }

    @ApiOperation("菜单信息")
    @GetMapping("/findById")
    //@RequiresPermissions("sys:menu:info")
    @SystemControllerLog(description = "菜单-权限查找")
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::findById");
        return new ResultData(Result.SUCCESS, "成功", mallPermissionService.getById(id));
    }

    @ApiOperation("获取父子菜单ID信息")
    @GetMapping("/getMenuIdsById")
    //@RequiresPermissions("sys:menu:info")
    @SystemControllerLog(description = "菜单-父子菜单查找")
    public ResultData getMenuIdsById(Long parentId) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::getMenusById");
        MallUserInfo user = getUser();
        return new ResultData(Result.SUCCESS, "成功", mallPermissionService.queryUserMenuIds(user, parentId, false));
    }

    @ApiOperation("获取父子菜单信息")
    @GetMapping("/getMenusById")
    //@RequiresPermissions("sys:menu:info")
    @SystemControllerLog(description = "菜单-父子菜单查找")
    public ResultData getMenusById(Long parentId) {
        logger.debug("[" + getIpAddr() + "] MallPermissionController::getMenusById");
        MallUserInfo user = getUser();
        return new ResultData(Result.SUCCESS, "成功", mallPermissionService.getUserMenuList(user, parentId, false));
    }

}
