package com.brframework.commoncms.utils;

import com.brframework.commoncms.core.cms.TreeNode;
import com.brframework.commonweb.core.SpringContext;
import com.brframework.commonweb.exception.HandleException;
import com.google.common.collect.Maps;
import com.brframework.commoncms.core.cms.Selector;
import com.brframework.commoncms.core.cms.Select;
import lombok.Data;

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

/**
 * @author 揭光智
 * @date 2019/02/21
 */
@Data
public class SelectorManager {
    private Map<String, Select> totalSelects = Maps.newConcurrentMap();

    private Map<String, TreeNode> totalTreeViews = Maps.newConcurrentMap();

    @SuppressWarnings("unchecked")
    public void init() {
        List<Selector> beans = SpringContext.getBeansOfType(Selector.class);
        for (Selector config : beans) {
            Map map = config.getConfig();
            map.forEach((k, v) -> {
                if (k instanceof String && v instanceof Select) {
                    String k1 = (String) k;
                    Select v1 = (Select) v;
                    if (totalSelects.put(k1, v1) != null) {
                        throw new HandleException("注册cms配置时key发生了冲突:" + k1);
                    }
                }

                if (k instanceof String && v instanceof TreeNode) {
                    String k1 = (String) k;
                    TreeNode v1 = (TreeNode) v;
                    if (totalTreeViews.put(k1, v1) != null) {
                        throw new HandleException("注册cms配置时key发生了冲突:" + k1);
                    }
                }
            });
        }
    }
}
