package com.microservices.pms.utils;

import com.microservices.common.core.utils.StringUtils;
import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum;
import lombok.Data;

/**
 * @author OTTO
 */
@Data
public class NexusRequestUrl {
    private String path;
    private RequestType requestType;

    protected static NexusRequestUrl getNexusRequestUrl(RequestType requestType, String path) {
        NexusRequestUrl nexusRequestUrl = new NexusRequestUrl();
        nexusRequestUrl.setRequestType(requestType);
        nexusRequestUrl.setPath(path);
        return nexusRequestUrl;
    }

    protected static NexusRequestUrl getNexusRequestUrl(RequestType requestType, String path, Object... pathParams) {
        NexusRequestUrl nexusRequestUrl = new NexusRequestUrl();
        nexusRequestUrl.setRequestType(requestType);
        nexusRequestUrl.setPath(String.format(path, pathParams));
        return nexusRequestUrl;
    }

    private static String getNexusFormat(String format) {
        return ProductLibraryRepositoriesFormatEnum.getNexusFormat(format);
    }

    /**
     * 新增制品库
     *
     * @param type   类型
     * @param format 格式
     * @return 请求地址
     */
    public static NexusRequestUrl ADD_REPOSITORIES(String type, String format) {

        return getNexusRequestUrl(RequestType.POST, "/service/rest/v1/repositories/%s/%s", getNexusFormat(format), type);
    }

    /**
     * Nexus通用操作（类似GraphQL）
     *
     * @return 请求地址
     */
    public static NexusRequestUrl DO_OPERATION() {
        return getNexusRequestUrl(RequestType.POST, "/service/extdirect");
    }

    /**
     * 上传制品
     *
     * @param nexusRepoName 制品库名称
     * @return 请求地址
     */
    public static NexusRequestUrl UPLOAD_COMPONENTS(String nexusRepoName) {
        return getNexusRequestUrl(RequestType.POST_FORM_DATA, "/service/rest/v1/components?repository=%s", nexusRepoName);
    }

    /**
     * 更新制品库
     *
     * @param type          类型
     * @param format        格式
     * @param nexusRepoName 制品库名称
     * @return 请求地址
     */
    public static NexusRequestUrl UPDATE_REPOSITORIES(String type, String format, String nexusRepoName) {
        return getNexusRequestUrl(RequestType.PUT, "/service/rest/v1/repositories/%s/%s/%s", getNexusFormat(format), type, nexusRepoName);
    }

    /**
     * 删除制品库
     *
     * @param nexusRepoName 制品库名称
     * @return 请求地址
     */
    public static NexusRequestUrl DELETE_REPOSITORIES(String nexusRepoName) {
        return getNexusRequestUrl(RequestType.DELETE, "/service/rest/v1/repositories/%s", nexusRepoName);
    }

    /**
     * 获取制品库
     *
     * @param type          类型
     * @param format        格式
     * @param nexusRepoName 制品库名称
     * @return 请求地址
     */
    public static NexusRequestUrl GET_REPOSITORIES(String type, String format, String nexusRepoName) {
        return getNexusRequestUrl(RequestType.GET, "/service/rest/v1/repositories/%s/%s/%s", getNexusFormat(format), type, nexusRepoName);
    }

    /**
     * 获取制品库中制品文件列表
     *
     * @param nexusRepoName     制品库名称
     * @param continuationToken 分页标记
     * @return 请求地址
     */
    public static NexusRequestUrl GET_ASSETS(String nexusRepoName, String continuationToken) {
        String path = String.format("/service/rest/v1/assets?repository=%s", nexusRepoName);
        if (StringUtils.isNotEmpty(continuationToken)) {
            path = path + "&continuationToken=" + continuationToken;
        }
        return getNexusRequestUrl(RequestType.GET, path);
    }

    /**
     * 获取制品库中制品列表
     *
     * @param nexusRepoName     制品库名称
     * @param continuationToken 分页标记
     * @return 请求地址
     */
    public static NexusRequestUrl GET_COMPONENTS(String nexusRepoName, String continuationToken) {
        String path = String.format("/service/rest/v1/components?repository=%s", nexusRepoName);
        if (StringUtils.isNotEmpty(continuationToken)) {
            path = path + "&continuationToken=" + continuationToken;
        }
        return getNexusRequestUrl(RequestType.GET, path);
    }

    public enum RequestType {
        POST,
        POST_FILE,
        POST_FORM_DATA,
        GET,
        DELETE,
        PUT,
        PATCH
    }
}
