package com.roma.romaapi.service;

import com.roma.romaapi.dao.PageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class PageService {
    @Autowired
    PageDao pageDao;  // 引入dao层
    public Map getPageJsonByCode(String pageCode){
        Map ret = pageDao.dbQueryForMap(pageCode);

        return ret;
    }

    // 整理返回菜单
    public List menu(){
        List<Map<String, Object>> menuData = pageDao.getMenuForList(); // 获取全部菜单数据

        List<Map<String, Object>> renderMenu = this.renderMenu(menuData);// 根据选项渲染节点内容

        List<Map<String, Object>> treeMenu = this.treeMenu(renderMenu);  // 生成树结构

        return treeMenu;
    }

    // 渲染节点内容
    private List<Map<String, Object>> renderMenu(List<Map<String, Object>> menuData){
        List<Map<String, Object>> retList = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < menuData.size(); i++) {
            Map map = menuData.get(i);
            String menuType = (String) map.get("type");
            Map tempMap = new HashMap<>();
            tempMap.put("id", map.get("id"));
            tempMap.put("parent_id", map.get("parent_id"));
            tempMap.put("label", (String) map.get("label"));
            tempMap.put("icon", (String) map.get("icon"));
            boolean visible = true;
            if (!map.containsKey("visible")) {
            } else {
                Object objectVisible = map.get("visible");
                String stringVisible = objectVisible.toString();
                visible = stringVisible.equals("0") || stringVisible.equals("1");
            }
            tempMap.put("visible", visible);
            switch (menuType) {
                case "menu": // 菜单
                    // 判断,如果是menu,但是value是空的,即path.则表名为目录,且不需要添加url和path
                    String path = (String) map.get("path");
                    if(path == null || path.equals("")) {

                    } else {
                        tempMap.put("url", (String) map.get("url"));
                        tempMap.put("schemaApi", path);
                    }
                    break;
                case "dir":
                    break;
                case "link":
                    tempMap.put("link", (String) map.get("path"));
                    break;
            }
            retList.add(tempMap);
        }

        return retList;
    }

    // 转换tree结构数据
    private List<Map<String, Object>> treeMenu(List<Map<String, Object>> renderMenu){
        // 处理的数据存在
        List<Map<String, Object>> pList = new ArrayList<Map<String, Object>>();// eTree
        // 获取父节点
        for (Map<String, Object> tmp : renderMenu) {
            Object parentId =  tmp.get("parent_id");
            String stringParentId = parentId.toString();// 上级id
            Object dataId = tmp.get("id");
            String stringDataId = dataId.toString();// 主键id
            if (stringParentId.equals("0")) {
                // 组装子节点，开始循环去获取子节点
                List<?> children = menuChild(stringDataId, renderMenu);
                if(children == null || children.size() == 0) {
                    tmp.put("leaf", true); // 如果不存在子元素
                } else {
                    tmp.put("children", children); // 存在子元素,则添加
                }

                pList.add(tmp);
            }
        }

        return pList;
    }

    /**
     * 处理子节点
     * @param id
     * @param data
     * @return
     */
    private List<?> menuChild(String id, List<Map<String, Object>> data) {
        List<Object> lists = new ArrayList<Object>();
        for (Map<String, Object> map : data) {
            Object parentId =  map.get("parent_id");
            String stringParentId = parentId.toString(); // 上级id
            Object dataId = map.get("id");
            String stringDataId = dataId.toString(); // 主键id
            if (stringParentId.equals(id)) {
                // 组装子节点，开始循环去获取子节点
                List<?> children = menuChild(stringDataId, data);
                if(children == null || children.size() == 0) {
                    map.put("leaf", true); // 如果不存在子元素
                } else {
                    map.put("children", children); // 存在子元素,则添加
                }
                lists.add(map);
            }
        }
        return lists;
    }
}
