package com.brframework.commonwebadmin.service.admin;

import com.brframework.commoncms.core.cms.Select;
import com.brframework.commoncms.core.cms.Selector;
import com.brframework.commoncms.core.cms.TreeNode;
import com.brframework.commoncms.core.permission.Menu;
import com.brframework.commoncms.core.permission.Permission;
import com.brframework.commoncms.utils.MenuPermissionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author xu
 * @date 2018/3/12 0012 下午 8:01
 * 功能列表
 */
@Service
public class FunctionService implements Selector<String, Object> {

    public static final String SELECT_PERMISSION = "SELECT_PERMISSION";

    /**
     * 获取当前登录用户的功能列表
     *
     * @return
     */
    public List<Menu> getRoleMenus(List<String> roles) {
        if(roles.size() == 0) return Collections.EMPTY_LIST;

        List<Menu> menus = MenuPermissionUtils.getMenus();
        List<Menu> newMenus = Lists.newArrayList();

        for (Menu menu : menus) {
            Menu newMenu = Menu.builder()
                    .order(menu.getOrder())
                    .role(menu.getRole())
                    .title(menu.getTitle())
                    .url(menu.getUrl())
                    .icon(menu.getIcon())
                    .children(Lists.newArrayList())
                    .build();
            for (Menu child : menu.getChildren()) {
                if(hasRole(roles, child.getRole())){
                    newMenu.getChildren().add(child);
                }
            }
            if(newMenu.getChildren().size() != 0){
                newMenus.add(newMenu);
            }
        }
        return newMenus;
    }


    private boolean hasRole(List<String> roles, String hasRole){
        for (String role : roles) {
            if(("ROLE_" + hasRole).equals(role)){
                return true;
            }
        }
        return false;
    }



    /**
     * 获取所有的功能列表
     *
     * @return
     */
    public List<Menu> getMenus() {
        return MenuPermissionUtils.getMenus();
    }

    /**
     * 获取所有权限
     * @return
     */
    public List<Permission> getPermissions(){
        return MenuPermissionUtils.getPermissions();
    }

    @Override
    public Map<String, Object> getConfig() {
        Map<String, Object> selector = Maps.newTreeMap();

        TreeNode root = new TreeNode();
        root.setTitle("权限列表");
        root.setChildren(Lists.newArrayList());

        List<Permission> permissions = getPermissions();
        for (Permission permission : permissions) {
            root.getChildren().add(genTreeNode(permission));
        }
        selector.put(SELECT_PERMISSION, root);
        return selector;
    }

    private TreeNode genTreeNode(Permission permission){
        TreeNode node = new TreeNode();
        node.setKey(permission.getRole());
        node.setTitle(permission.getTitle());
        node.setChildren(Lists.newArrayList());
        if(permission.getChildren() != null){
            for (Permission child : permission.getChildren()) {
                node.getChildren().add(genTreeNode(child));
            }
        }

        return node;
    }
}
