package com.lightinit.hsdataplatform.controller;

import com.lightinit.hsdataplatform.common.MenuUtils;
import com.lightinit.hsdataplatform.common.PermissionUtilsPro;
import com.lightinit.hsdataplatform.entity.Menu;
import com.lightinit.hsdataplatform.entity.MenuDataEntity;
import com.lightinit.hsdataplatform.entity.RoleModule;
import com.lightinit.hsdataplatform.service.IMenuService;
import com.lightinit.hsdataplatform.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mr.Yao on 2017/4/10.
 */
@Controller
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;
    @Autowired
    private PermissionUtilsPro permissionUtilsPro;
    @Autowired
    private IRoleService roleService;

    @RequestMapping(value = "view/list.action")
    public String print_menu_view_list(){
        return "menu/module";
    }

    @ResponseBody
    @RequestMapping(value = "list.action",method = RequestMethod.GET)
    public List<MenuDataEntity> print_Json_Menu(){
        List<MenuDataEntity> list=new ArrayList<MenuDataEntity>();
        if(permissionUtilsPro.IsSuperPermission()) {
            list.addAll(MenuUtils.InitSystemTopMenu());
        }
        List<Menu> menuList=new ArrayList<Menu>();
        List<RoleModule> roleModuleList=new ArrayList<RoleModule>();
        if(permissionUtilsPro.IsSuperPermission()) {
            menuList=menuService.QueryList(1,0,null);
        }else{
            menuList=menuService.QueryList(permissionUtilsPro.CurrentName());
            roleModuleList=roleService.QueryUserModuleList(permissionUtilsPro.CurrentName());
        }
        for (Menu item:menuList) {
            MenuDataEntity _item= MenuUtils.MenuConverter(item,roleModuleList,permissionUtilsPro.IsSuperPermission());
            if(_item!=null)
                list.add(_item);
        }
        list.addAll(MenuUtils.InitSystemBottomMenu());
        return list;
    }
    @ResponseBody
    @RequestMapping(value = "json/module/list.action",method = RequestMethod.GET)
    public List<Menu> print_Json_Module_Menu() {
        List<Menu> list = new ArrayList<Menu>();
        List<MenuDataEntity> topList = MenuUtils.InitSystemTopMenu();
        for (MenuDataEntity topItem:topList) {
            Menu _topMenuItem = MenuUtils.MenuDataConverter(topItem);
            if (_topMenuItem != null)
                list.add(_topMenuItem);
        }
        List<Menu> centerList = menuService.QueryList(1, 1000, null);
        if (centerList != null && centerList.size() > 0)
            list.addAll(centerList);
        List<MenuDataEntity> bottomList = MenuUtils.InitSystemBottomMenu();
        for (MenuDataEntity bottomItem:bottomList) {
            Menu _bottomMenuItem = MenuUtils.MenuDataConverter(bottomItem);
            if (_bottomMenuItem != null)
                list.add(_bottomMenuItem);
        }
        return list;
    }
}
