package com.bocloud.ims.common.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * RackHD工具类
 * 
 * @author sxp
 * @since Nov 10, 2017
 */
public final class RackHdUtils {

    private static final String LEFT = " (";

    private static final String RIGHT = ")";

    private static final String PREFIX1 = "0x";

    private static final String PREFIX2 = "0X";

    /**
     * 返回最后一个斜线后面的id值
     * <p>
     * 例如获取/api/2.0/obms/5a0156213a03f4e44acf54db中的5a0156213a03f4e44acf54db
     * </p>
     * 
     * @author sxp
     * @since Nov 21, 2017
     */
    public static String getLastIdAfterLastSlash(String str) {
        if (StringUtils.isEmpty(str)) {
            return null;
        }

        // /api/2.0/obms/5a0156213a03f4e44acf54db
        int index = str.lastIndexOf("/");
        if (index < 0) {
            return null;
        }
        if (index + 1 > str.length() - 1) {
            return null;
        }

        String id = str.substring(index + 1);
        if (StringUtils.isEmpty(id)) {
            return null;
        }

        return id;
    }

    /**
     * 根据node节点中的obmRef信息获取obm的Id
     * 
     * @author sxp
     * @since Nov 10, 2017
     */
    public static String getObmId(String obmRef) {
        if (StringUtils.isEmpty(obmRef)) {
            return null;
        }

        // ref的格式: /api/2.0/obms/5a0156213a03f4e44acf54db
        return getLastIdAfterLastSlash(obmRef);
    }

    /**
     * 根据node节点中的catelogs信息获取catelogs的Id
     * 
     * @author sxp
     * @since Nov 21, 2017
     */
    public static String getNodeCatelogsId(String catalogs) {
        if (StringUtils.isEmpty(catalogs)) {
            return null;
        }

        // /api/2.0/nodes/5a13cbea8889dc984c675886/catalogs
        int index = catalogs.lastIndexOf("/catalogs");
        if (index < 0) {
            return null;
        }

        catalogs = catalogs.replace("/catalogs", "");
        return getLastIdAfterLastSlash(catalogs);
    }

    public static String modify(String info) {
        if (info == null) {
            return null;
        }
        if (info.contains(LEFT) && info.contains(RIGHT)) {
            String num = info.substring(info.indexOf(LEFT) + 2, info.indexOf(RIGHT));
            return info.substring(0, info.indexOf(LEFT)) + " (" + parse(num) + ")";
        }
        return info;
    }

    public static String parse(String str) {
        try {
            if (str.startsWith(PREFIX1)) {
                String num = str.substring(str.indexOf(PREFIX1) + 2, str.length());
                return String.valueOf(Long.parseLong(num, 16));
            } else if (str.startsWith(PREFIX2)) {
                String num = str.substring(str.indexOf(PREFIX2) + 2, str.length());
                return String.valueOf(Long.parseLong(num, 16));
            } else {
                return str;
            }
        } catch (Exception e) {
            return str;
        }
    }

    private RackHdUtils() {
        throw new IllegalStateException("Utility class");
    }

}
