package com.microservices.pms.productLibrary.domain;

import com.microservices.common.core.exception.ServiceException;
import com.microservices.pms.productLibrary.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 ProductLibraryRepositoriesFormatEnum {
    MAVEN("maven", "jar,war,pom(maven2)"),
    NPM("npm", "npm"),
    RAW("raw", "raw", true),
    PYPI("pypi", "pypi"),
    GO("go", "go mod", true),
    CONDA("conda", "conda", true),
    DOCKER("docker", "docker"),
    XXXXX("xxxxx", "xxxxx", true),
    PRODUCT("product", "产品库", true, true);

    private final String key;
    private final String name;
    private final Boolean isRaw;
    private final Boolean hidden;

    ProductLibraryRepositoriesFormatEnum(String key, String name) {
        this.key = key;
        this.name = name;
        this.isRaw = false;
        this.hidden = false;
    }

    ProductLibraryRepositoriesFormatEnum(String key, String name, Boolean isRaw) {
        this.key = key;
        this.name = name;
        this.isRaw = isRaw;
        this.hidden = false;
    }

    ProductLibraryRepositoriesFormatEnum(String key, String name, Boolean isRaw, Boolean hidden) {
        this.key = key;
        this.name = name;
        this.isRaw = isRaw;
        this.hidden = hidden;
    }

    public static List<CommonTypeVo> getNoHiddenToList() {
        List<CommonTypeVo> list = new ArrayList<>();
        for (ProductLibraryRepositoriesFormatEnum e : ProductLibraryRepositoriesFormatEnum.values()) {
            if (!e.hidden) {
                list.add(new CommonTypeVo(e.key, e.name));
            }
        }
        return list;
    }

    public static ProductLibraryRepositoriesFormatEnum getNoHiddenByKey(String key) {
        ProductLibraryRepositoriesFormatEnum formatEnum = getByKey(key);
        if (formatEnum.hidden) {
            throw new ServiceException("该制品库格式不允许创建(制品库格式[%s])", key);
        }
        return formatEnum;
    }

    public static ProductLibraryRepositoriesFormatEnum getByKey(String key) {
        if ("maven2".equals(key)) {
            return ProductLibraryRepositoriesFormatEnum.MAVEN;
        }
        for (ProductLibraryRepositoriesFormatEnum formatEnum : ProductLibraryRepositoriesFormatEnum.values()) {
            if (formatEnum.key.equals(key)) {
                return formatEnum;
            }
        }
        throw new ServiceException("该制品库格式不存在(制品库格式[%s])", key);
    }

    public static String getNexusFormat(String format) {
        ProductLibraryRepositoriesFormatEnum formatEnum =
                ProductLibraryRepositoriesFormatEnum.getByKey(format);
        if (formatEnum.isRaw) {
            return RAW.key;
        }
        return format;
    }

    public static Boolean isNexusRaw(String format) {
        ProductLibraryRepositoriesFormatEnum formatEnum =
                ProductLibraryRepositoriesFormatEnum.getByKey(format);
        return formatEnum.isRaw;
    }

    public static Boolean isProduct(String format) {
        return PRODUCT.getKey().equals(format);
    }
}
