package cn.stylefeng.team.view.modular.parser;

import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.dict.api.format.DictFormatProcess;
import cn.stylefeng.roses.kernel.file.api.format.FileUrlFormatProcess;
import cn.stylefeng.roses.kernel.sys.api.format.OrgNameFormatProcess;
import cn.stylefeng.roses.kernel.sys.api.format.PositionNameFormatProcess;
import cn.stylefeng.roses.kernel.sys.api.format.RoleNameFormatProcess;
import cn.stylefeng.roses.kernel.sys.api.format.UserNameFormatProcess;
import cn.stylefeng.team.form.api.parser.FormCityFormatFormat;
import cn.stylefeng.team.view.api.format.FormListProcess;
import cn.stylefeng.team.view.api.pojo.ParserDetail;
import cn.stylefeng.team.view.modular.format.FileUrlMultiFormatProcess;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

public class ParserContainer {

    public static final Map<String, ParserDetail> CONTAINER = new LinkedHashMap<>();

    static {

        ParserDetail dict = new ParserDetail("dict", "字典转化器", new DictFormatProcess(), "字典id", "字典的名称");
        CONTAINER.put(dict.getKey(), dict);

        ParserDetail fileUrl = new ParserDetail("fileUrl", "文件URL转化器", new FileUrlFormatProcess(), "文件id", "文件的URL字符串");
        CONTAINER.put(fileUrl.getKey(), fileUrl);

        ParserDetail fileUrlMulti = new ParserDetail("fileUrlMulti", "多文件URL转化器", new FileUrlMultiFormatProcess(), "字符串形式文件id数组", "文件的URL字符串");
        CONTAINER.put(fileUrlMulti.getKey(), fileUrlMulti);

        ParserDetail orgName = new ParserDetail("orgName", "组织机构名称", new OrgNameFormatProcess(), "组织机构id", "组织机构名称");
        CONTAINER.put(orgName.getKey(), orgName);

        ParserDetail positionName = new ParserDetail("positionName", "职务名称转化器", new PositionNameFormatProcess(), "职务id", "职务名称");
        CONTAINER.put(positionName.getKey(), positionName);

        ParserDetail roleName = new ParserDetail("roleName", "角色名称转化器", new RoleNameFormatProcess(), "角色id", "角色名称");
        CONTAINER.put(roleName.getKey(), roleName);

        ParserDetail userName = new ParserDetail("userName", "用户姓名转化器", new UserNameFormatProcess(), "用户id", "用户姓名");
        CONTAINER.put(userName.getKey(), userName);

        ParserDetail parserDetail = new ParserDetail("city", "【表单】省市联动转化器", new FormCityFormatFormat(), "省市联动组件的选择值", "区域码对应的具体中文名称");
        CONTAINER.put(parserDetail.getKey(), parserDetail);

        ParserDetail formList = new ParserDetail("form", "【表单】数组值转化", new FormListProcess(), "[{\"id\":\"1001\",\"name\":\"张三\"},{\"id\":\"1002\",\"name\":\"李四\"}]", "张三，李四");
        CONTAINER.put(formList.getKey(), formList);
    }

    public static Collection<ParserDetail> getTotalParser() {
        return CONTAINER.values();
    }

    public static Collection<ParserDetail> queryParser(String searchText) {
        if (ObjectUtil.isEmpty(searchText)) {
            return CONTAINER.values();
        }
        Map<String, ParserDetail> result = new LinkedHashMap<>();
        for (Map.Entry<String, ParserDetail> entry : CONTAINER.entrySet()) {
            if (entry.getValue().getKey().contains(searchText) || entry.getValue().getName().contains(searchText)) {
                result.put(entry.getKey(), entry.getValue());
            }
        }
        return result.values();
    }

    public static ParserDetail getParser(String parserName) {
        return CONTAINER.get(parserName);
    }

}
