package com.mortals.xhx.base.system.menu.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.JSONToken;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.util.ObjectUtils;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* 菜单信息业务前端映射树结构实体类
*
* @author zxfei
* @date 2024-09-06
*/
@Data
public class MenuTreeSelect  implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
    * 节点ID
    */
    private Long id;

    /**
    * 父节点ID
    */
    private Long parentId;

    /**
    * 节点名称
    */
    private String label;

    /**
     * 菜单类型 (0.目录，1.菜单，2.按钮)
     */
    private Integer menuType;

    /**
     * 菜单显示状态 (0.显示，1.隐藏)
     */
    private Integer visible;

    /**
     * 权限标识，多个逗号分割
     */
    private String perms;


    /**
     * 权限标识，多个逗号分割
     */
    private String resourceUrl;

    /**
     * 是否隐藏(0.否，1.是)
     */
    private Integer hideChildrenInMenu;

    /**
     * 是否选中，0为未选中，1选中。默认0
     */
    private Integer checked;

    /**
    * 子节点
    */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private List<MenuTreeSelect> children;

    public MenuTreeSelect() {};

    public MenuTreeSelect(MenuEntity entity) {
        this.id = entity.getId();
        this.parentId = entity.getParentId();
        this.label = entity.getName();
        this.menuType = entity.getMenuType();
        this.visible = entity.getVisible();
        this.perms = entity.getPerms();
        this.resourceUrl=entity.getResourceUrl();
        this.checked=entity.getChecked();
        this.hideChildrenInMenu = entity.getHideChildrenInMenu();
        if(!ObjectUtils.isEmpty(entity.getChildren())){
            this.children = entity.getChildren().stream().map(MenuTreeSelect::new).collect(Collectors.toList());
        }
    }

    // 反序列化器
    public  class Deserializer implements ObjectDeserializer {
        @Override
        public MenuTreeSelect deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
            MenuTreeSelect node = new MenuTreeSelect();
            JSONObject jsonObject = parser.parseObject();
            node.setId(jsonObject.getLong("id"));
            node.setLabel(jsonObject.getString("label"));
            node.setMenuType(jsonObject.getInteger("menuType"));
            node.setVisible(jsonObject.getInteger("visible"));
            node.setPerms(jsonObject.getString("perms"));
            node.setResourceUrl(jsonObject.getString("resourceUrl"));
            node.setChecked(jsonObject.getInteger("checked"));
            node.setHideChildrenInMenu(jsonObject.getInteger("hideChildrenInMenu"));
            JSONArray jsonArray = jsonObject.getJSONArray("children");
            List<MenuTreeSelect> children = new ArrayList<>();
            if(!ObjectUtils.isEmpty(jsonArray)){
                for (int i = 0; i < jsonArray.size(); i++) {
                    MenuTreeSelect child = JSON.parseObject(jsonArray.getJSONObject(i).toJSONString(), MenuTreeSelect.class);
                    children.add(child);
                }
            }
            node.setChildren(children);
            return node;
        }

        @Override
        public int getFastMatchToken() {
            return JSONToken.LBRACE;
        }
    }

}