package cn.axzo.msg.center.message.service.group;

import cn.axzo.maokai.api.vo.response.tree.NodeValue;
import cn.axzo.msg.center.domain.entity.MessageGroupNode;
import cn.axzo.msg.center.utils.TreeHelperUtil;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author yanglin
 */
@RequiredArgsConstructor
public class NodeWrapper implements NodeValue {

    private final MessageGroupNode node;
    private final Map<String, Long> code2Id;
    /**
     * cache path for better performance
     * visit via {@link RootNodeWrapper#getPath}
     */
    @Setter(AccessLevel.PACKAGE)
    @Getter(AccessLevel.PACKAGE)
    private String cachedPath;

    @Override
    public Long id() {
        return node.getId();
    }

    @Override
    public Long parentId() {
        if (isValueRoot())
            return 0L;
        return code2Id.get(node.getParentCode());
    }

    @Override
    public boolean isValueRoot() {
        return StringUtils.isBlank(node.getParentCode());
    }

    public boolean isLeaf() {
        return TreeHelperUtil.isLeafNodeCategory(node.getCategory());
    }

    public MessageGroupNode unwrap() {
        return node;
    }

    public String getNodeCode() {
        return node.getCode();
    }

}