package com.microservices.pms.enums;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.exception.ServiceException;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.html.EscapeUtil;
import io.swagger.annotations.ApiModel;
import lombok.Getter;

/**
 * @author OTTO
 */
@Getter
@ApiModel("产品库制品路径前缀")
public enum ProductLibraryComponentPathEnum {
    ALL("_All", "所有"),
    SOFTWARE("Software", "制品"),
    DOC("Document", "文档");

    private final String key;
    private final String name;

    ProductLibraryComponentPathEnum(String key, String name) {
        this.key = key;
        this.name = name;
    }


    public static ProductLibraryComponentPathEnum getByKey(String key) {
        for (ProductLibraryComponentPathEnum typeEnum : ProductLibraryComponentPathEnum.values()) {
            if (typeEnum.key.equals(key)) {
                return typeEnum;
            }
        }
        throw new ServiceException("该产品库制品路径前缀不存在(产品库制品路径前缀[%s])", key);
    }

    public static String getSoftwareFixedPath(String directory) {
        return EscapeUtil.removeExtraSlashOfUrl(SOFTWARE.key + "/" + directory);
    }

    public static String getDocFixedPath(String directory) {
        return EscapeUtil.removeExtraSlashOfUrl(DOC.key + "/" + directory);
    }

    public static String getChineseFixedPath(String path) {
        if (path.startsWith(SOFTWARE.key)) {
            return path.replaceFirst(SOFTWARE.key, SOFTWARE.name);
        }
        if (path.startsWith(DOC.key)) {
            return path.replaceFirst(DOC.key, DOC.name);
        }
        return path;
    }

    public static boolean pathIsNeedPackaged(String node, String path) {
        if (StringUtils.isNotEmpty(node)) {
            if (node.equals(SOFTWARE.key)) {
                return path.startsWith(SOFTWARE.key);
            }
            if (node.equals(DOC.key)) {
                return path.startsWith(DOC.key);
            }
        }

        return true;
    }

    public static String getPackagedFileJson(String node, String packagedFileIdentifiersJson, String fileIdentifier) {
        JSONObject packagedFileIdentifiersJsonObj;
        if (StringUtils.isNotEmpty(packagedFileIdentifiersJson) && JSON.isValid(packagedFileIdentifiersJson)) {
            packagedFileIdentifiersJsonObj = JSONObject.parseObject(packagedFileIdentifiersJson);
        } else {
            packagedFileIdentifiersJsonObj = new JSONObject();
        }
        if (StringUtils.isNotEmpty(node)) {
            if (SOFTWARE.key.equals(node)) {
                packagedFileIdentifiersJsonObj.put(SOFTWARE.key, fileIdentifier);
                return packagedFileIdentifiersJsonObj.toJSONString();
            }
            if (DOC.key.equals(node)) {
                packagedFileIdentifiersJsonObj.put(DOC.key, fileIdentifier);
                return packagedFileIdentifiersJsonObj.toJSONString();
            }
        }
        packagedFileIdentifiersJsonObj.put(ALL.key, fileIdentifier);
        return packagedFileIdentifiersJsonObj.toJSONString();
    }

    public static String getPackagedFileIdentifierFromJson(String node, JSONObject packagedFileIdentifiersJsonObj) {
        if (StringUtils.isNotEmpty(node)) {
            if (SOFTWARE.key.equals(node)) {
                return packagedFileIdentifiersJsonObj.getString(SOFTWARE.key);
            }
            if (DOC.key.equals(node)) {
                return packagedFileIdentifiersJsonObj.getString(DOC.key);
            }
        }
        return packagedFileIdentifiersJsonObj.getString(ALL.key);
    }

    public static String getPackagedFileTag(String node) {
        if (StringUtils.isNotEmpty(node)) {
            if (SOFTWARE.key.equals(node)) {
                return "(" + SOFTWARE.name + ")";
            }
            if (DOC.key.equals(node)) {
                return "(" + DOC.name + ")";
            }
        }
        return "";
    }

    public static String setPackagedFileIdentifierNull(String node, String packagedFileIdentifiersJson) {
        JSONObject packagedFileIdentifiersJsonObj;
        if (StringUtils.isNotEmpty(packagedFileIdentifiersJson) && JSON.isValid(packagedFileIdentifiersJson)) {
            packagedFileIdentifiersJsonObj = JSONObject.parseObject(packagedFileIdentifiersJson);
        } else {
            packagedFileIdentifiersJsonObj = new JSONObject();
        }
        if (StringUtils.isNotEmpty(node)) {
            if (SOFTWARE.key.equals(node)) {
                if (packagedFileIdentifiersJsonObj.containsKey(SOFTWARE.key)) {
                    packagedFileIdentifiersJsonObj.remove(SOFTWARE.key);
                }
                return packagedFileIdentifiersJsonObj.toJSONString();
            }
            if (DOC.key.equals(node)) {
                if (packagedFileIdentifiersJsonObj.containsKey(DOC.key)) {
                    packagedFileIdentifiersJsonObj.remove(DOC.key);
                }
                return packagedFileIdentifiersJsonObj.toJSONString();
            }
        }
        if (packagedFileIdentifiersJsonObj.containsKey(ALL.key)) {
            packagedFileIdentifiersJsonObj.remove(ALL.key);
        }
        return packagedFileIdentifiersJsonObj.toJSONString();
    }
}
