package com.zhgd.xmgl.util;

import com.zhgd.xmgl.modules.basicdata.dto.MenuTreeDto;
import com.zhgd.xmgl.modules.basicdata.entity.BaseAction;
import com.zhgd.xmgl.modules.basicdata.entity.BaseMenu;
import com.zhgd.xmgl.modules.basicdata.entity.BaseModule;
import lombok.experimental.UtilityClass;

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

@UtilityClass
public class TreeUtil {

    public List<MenuTreeDto> formatModule(List<BaseModule> baseModules) {
        List<MenuTreeDto> list = new ArrayList<>();
        MenuTreeDto node;
        for (BaseModule menu : baseModules) {
            node = new MenuTreeDto();
            node.setId(menu.getModuleId());
            node.setName(menu.getModuleName());
            node.setNormalIcon(menu.getModuleIconNormal());
            node.setCheckIcon(menu.getModuleIconChecked());
            node.setType(0);
            node.setParentId(0L);
            list.add(node);
        }
        return list;
    }

    public List<MenuTreeDto> formatMenu(List<BaseMenu> baseMenus) {
        List<MenuTreeDto> list = new ArrayList<>();
        MenuTreeDto node;
        for (BaseMenu menu : baseMenus) {
            node = new MenuTreeDto();
            node.setId(menu.getMenuId());
            node.setName(menu.getMenuName());
            node.setNormalIcon(menu.getIcon());
            node.setType(1);
            node.setParentId(menu.getParentId());
            node.setModuleId(menu.getModuleId());
            list.add(node);
        }
        return list;
    }

    public List<MenuTreeDto> formatAction(List<BaseAction> baseActions) {
        List<MenuTreeDto> list = new ArrayList<>();
        MenuTreeDto node;
        for (BaseAction menu : baseActions) {
            node = new MenuTreeDto();
            node.setId(menu.getActionId());
            node.setName(menu.getActionName());
            node.setType(2);
            node.setParentId(menu.getMenuId());
            list.add(node);
        }
        return list;
    }

    public List<MenuTreeDto> build(List<MenuTreeDto> menuTreeDtos, Integer type, Long parentId, Integer level) {
        List<MenuTreeDto> trees = new ArrayList<>();
        Integer iType = type;
        if (type == 0) {
            trees = menuTreeDtos.stream().filter(m -> m.getType().equals(type)).collect(Collectors.toList());
            iType = 1;
        } else {
            if (level == 2) {
                trees = menuTreeDtos.stream().filter(m -> m.getType().equals(type) && m.getModuleId().equals(parentId)).collect(Collectors.toList());
            } else {
                trees = menuTreeDtos.stream().filter(m -> m.getType().equals(type) && m.getParentId().equals(parentId)).collect(Collectors.toList());
                if (trees.size() == 0) {
                    iType = 2;
                    trees = menuTreeDtos.stream().filter(m -> m.getType().equals(2) && m.getParentId().equals(parentId)).collect(Collectors.toList());
                }
            }
        }
        if (iType != 2) {
            for (MenuTreeDto menuTreeDto : trees) {
                level = level + 1;
                menuTreeDto.setChildren(build(menuTreeDtos, iType, menuTreeDto.getId(), level));
            }
        }
        return trees;
    }
}
