package com.microservices.zone.specialProject.domain;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.zone.specialProject.domain.vo.CommonTypeVo;
import io.swagger.annotations.ApiModel;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author OTTO
 */
@Getter
@ApiModel("专项项目关联类型")
public enum ZoneSpecialProjectRelevancyTypeEnum {
    PROJECT("PROJECT", "开源项目", true),
    PATENT("PATENT", "专利", false),
    PAPER("PAPER", "论文", false);

    private final String key;
    private final String name;
    private final Boolean isProject;

    ZoneSpecialProjectRelevancyTypeEnum(String key, String name, Boolean isProject) {
        this.key = key;
        this.name = name;
        this.isProject = isProject;
    }

    public static List<CommonTypeVo> getAllToList() {
        List<CommonTypeVo> list = new ArrayList<>();
        for (ZoneSpecialProjectRelevancyTypeEnum e : ZoneSpecialProjectRelevancyTypeEnum.values()) {
            list.add(new CommonTypeVo(e.key, e.name));
        }
        return list;
    }

    public static boolean isProject(String key) {
        if (StringUtils.isNotEmpty(key)) {
            for (ZoneSpecialProjectRelevancyTypeEnum typeEnum : ZoneSpecialProjectRelevancyTypeEnum.values()) {
                if (typeEnum.getKey().equalsIgnoreCase(key)) {
                    return typeEnum.getIsProject();
                }
            }
        }
        throw new ServiceException("该专项项目关联类型不存在(关联类型[%s])", key);
    }

    public static String getNameByKey(String key) {
        if (StringUtils.isNotEmpty(key)) {
            for (ZoneSpecialProjectRelevancyTypeEnum typeEnum : ZoneSpecialProjectRelevancyTypeEnum.values()) {
                if (typeEnum.getKey().equalsIgnoreCase(key)) {
                    return typeEnum.getName();
                }
            }
        }
        throw new ServiceException("该专项项目关联类型不存在(关联类型[%s])", key);
    }
}
