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

import cn.axzo.maokai.api.vo.response.tree.ValueNode;
import cn.axzo.msg.center.domain.entity.Todo;
import cn.axzo.msg.center.message.service.group.GroupTemplates;
import cn.axzo.msg.center.message.service.group.NodeWrapper;
import cn.axzo.msg.center.service.enums.TodoType;
import lombok.RequiredArgsConstructor;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanglin
 */
@RequiredArgsConstructor
class LeafNodeStats {

    // count, type, template_code
    private final List<Todo> stats;
    private final GroupTemplates groupTemplates;

    Map<TodoType, Integer> collectStat(ValueNode<NodeWrapper> node) {
        HashMap<TodoType, Integer> type2Count = new HashMap<>();
        for (Todo stat : stats) {
            // 只统计叶子节点的数据, 显示列表不关心中间节点
            boolean leafNodeContainsTemplate = groupTemplates.groupContainsTemplate(
                    node.getValue().getNodeCode(), stat.getTemplateCode());
            if (leafNodeContainsTemplate) {
                Integer prevCount = type2Count.get(stat.getType());
                if (prevCount == null) prevCount = 0;
                Integer currentCount  = prevCount + stat.getCount();
                type2Count.put(stat.getType(), currentCount);
            }
        }
        for (TodoType type : TodoType.values()) {
            if (!type2Count.containsKey(type))
                type2Count.put(type, 0);
        }
        return type2Count;
    }

}
