package com.sptg.gateway.controller;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Menu;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * title: RootController
 * package: com.sptg.gateway.controller
 * description: 根路径-控制器
 *
 *                  
 *                 
 */
@RestController
@AllArgsConstructor
public class RootController {

    private SecurityServiceInterface<Account> securityServiceInterface;

    /**
     * 功能描述: 首页
     * @return ModelAndView
     *                  
     *                       
     */
    @Security
    @GetMapping(value = "/")
    public ModelAndView index(){
        //  获取当前在线用户信息
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        if (onLineUser.getHadUpdatePwd() == null || !onLineUser.getHadUpdatePwd().equals(1)){
            return new ModelAndView("redirect:/permissions/authentication/modifyPwd");
        }

        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("onLineUser",onLineUser);

        List<Menu> menuList = onLineUser.getMenuList();
        List<Menu> subMenuList = null;
        for (Menu menu : menuList){
            subMenuList = menu.getSubList().stream().filter(item -> !item.getType().equals(2)).collect(Collectors.toList());
            for (Menu subMenu : subMenuList){
                List<Menu> subList = subMenu.getSubList();
                if (subList != null){
                    subMenu.setSubList(subList.stream().filter(item -> !item.getType().equals(2)).collect(Collectors.toList()));
                }
            }
            menu.setSubList(subMenuList);
        }
//        menuList.
//        onLineUser.setMenuList(subMenuList);
        return modelAndView;
    }

    /**
     * 功能描述: 获取当前用户可用的子菜单
     * @param id 一级菜单Id
     * @return ResultBean<Menu>
     *                  
     *                       
     */
    @Security(format = DataFormatEnum.JSON)
    @GetMapping(value = "/home/getSubMenuList/{id}")
    public ResultBean<Menu> getSubMenuList(@PathVariable(value = "id") Long id){
        //  获取当前在线用户信息
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        List<Menu> menuList = onLineUser.getMenuList();
        List<Menu> subMenuList = null;
        for (Menu menu : menuList){
            if (menu.getId().equals(id)){
                subMenuList = menu.getSubList().stream().filter(item -> !item.getType().equals(2)).collect(Collectors.toList());
                for (Menu subMenu : subMenuList){
                    List<Menu> subList = subMenu.getSubList();
                    if (subList != null){
                        subMenu.setSubList(subList.stream().filter(item -> !item.getType().equals(2)).collect(Collectors.toList()));
                    }
                }
                break;
            }
        }
        return new ResultBean<>(ResultEnum.SUCCESS,subMenuList);
    }

}
