package com.muchinfo.mtp3century.controller;


import com.muchinfo.mtp3century.service.IFuncmenulistService;
import com.muchinfo.mtp3century.utils.CenturyDecryptUtil;
import com.muchinfo.mtp3century.vo.TreeNode;
import com.muchinfo.mtp3common.enumtype.Constants;
import com.muchinfo.mtp3common.enumtype.MessageType;
import com.muchinfo.mtp3century.utils.AjaxResult;
import com.muchinfo.mtp3common.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 功能菜单表 前端控制器
 * </p>
 *
 * @author XKF
 * @since 2024-04-10
 */
@RestController
@RequestMapping("/funcmenulist")
@Api(value = "菜单API", description = "菜单管理")
public class FuncmenulistController {

    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private IFuncmenulistService iFuncmenulistService;

    /**
     * 获取菜单树
     *
     * @return
     */
    @ApiOperation("获取角色授权菜单列表信息")
    @ApiResponse(code = 200, message = "成功", response = TreeNode.class)
    @RequestMapping(method = RequestMethod.GET, value = "/getMenus")
    private AjaxResult<TreeNode> menuList(Integer menuType, HttpServletRequest request) {
        List<String> authorityNames = new ArrayList<>();
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            authorityNames.add(authority.getAuthority());
        }
        List<TreeNode> list = iFuncmenulistService.fingByAuthorities(authorityNames, menuType);
        return AjaxResult.success(Constants.Success_Code_Design, MessageType.MESSAGE_CODE_SYSTEM001.getCode(), request.getHeader("Accept-Language"), list);
    }

    @ApiOperation("处理菜单列表信息")
    @RequestMapping(method = RequestMethod.POST, value = "/dealMenus")
    private AjaxResult dealMenus(@RequestBody String treeNode, HttpServletRequest request) {
        try {
            TreeNode currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(treeNode, request), TreeNode.class);
            return iFuncmenulistService.dealMenus(currentVo, request);
        } catch (Exception e) {
            log.error("处理菜单列表信息失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("删除菜单列表信息")
    @RequestMapping(method = RequestMethod.POST, value = "/deleteMenus")
    private AjaxResult deleteMenus(@RequestBody String treeNode, HttpServletRequest request) {
        try {
            TreeNode currentVo = JsonUtils.toBean(CenturyDecryptUtil.decryptForJson(treeNode, request), TreeNode.class);
            return iFuncmenulistService.deleteMenus(currentVo, request);
        } catch (Exception e) {
            log.error("删除菜单列表信息失败，失败原因：" + Arrays.toString(e.getStackTrace()));
            return AjaxResult.error(Constants.Error_Code_Design, MessageType.MESSAGE_ERROR_CODE_SYSTEM002.getCode(), request.getHeader("Accept-Language"));
        }
    }

    @ApiOperation("获取所有菜单列表信息")
    @ApiResponse(code = 200, message = "成功", response = TreeNode.class)
    @RequestMapping(method = RequestMethod.GET, value = "/getAllMenus")
    private AjaxResult<TreeNode> menuAllList(Integer menuType, HttpServletRequest request) {
        List<TreeNode> list = iFuncmenulistService.menuAllList(menuType);
        return AjaxResult.success(Constants.Success_Code_Design, MessageType.MESSAGE_CODE_SYSTEM001.getCode(), request.getHeader("Accept-Language"), list);
    }
}

