package com.bocloud.ims.service.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.entity.enums.F5BasicObject;
import com.bocloud.common.model.BocloudResult;
import com.google.common.collect.Maps;

/**
 * BigIp工具类
 * 
 * @author liuyuanyuan
 *
 */
public class BigIpHelper {

    public enum EnumHelper {
        /**
         * 默认分区
         */
        Common,
        /**
         * 绝对路径
         */
        KIND,
        /**
         * 相对路径
         */
        FULLPATH
    }

    /**
     * 拼接分区路径
     * 
     * @param partition
     * @return
     */
    public static String jointPartition(String partition, String name) {
        return new StringBuilder().append("/").append(partition).append("/").append(name).toString();
    }

    /**
     * 返回分区标识
     * 
     * @param partition
     * @return
     */
    public static String partitionCode(String partition, String name) {
        return new StringBuilder().append("~").append(partition).append("~").append(name).toString();
    }

    /**
     * 获取资源状态信息
     * 
     * @param version
     * @param result
     * @param parentType
     * @param type
     * @param parentName
     * @param name
     * @return
     */
    public static String getResourceStatus(String version, BocloudResult result, String parentType, String type,
                                           String parentName, String name) {
        JSONObject resultData = JSONObject.parseObject(result.getMessage());
        JSONObject entries = resultData.getJSONObject("entries");
        String description = null;
        switch (version) {
        case "V11":
            // 获取V11版本的节点状态
            description = entries.getJSONObject("status.availabilityState").getString("description");
            break;
        case "V13":
            if (F5BasicObject.POOL_MEMBER.equals(type)) {
                String key = "https://localhost/mgmt/tm/ltm/" + parentType + "/" + parentName + "/" + type + "/" + name
                        + "/" + name;
                description = entries.getJSONObject(key + "/stats").getJSONObject("nestedStats")
                        .getJSONObject("entries").getJSONObject("status.availabilityState").getString("description");
            } else {
                String key = "https://localhost/mgmt/tm/ltm/" + type + "/" + parentName + "/" + parentName + "/stats";
                description = entries.getJSONObject(key).getJSONObject("nestedStats").getJSONObject("entries")
                        .getJSONObject("status.availabilityState").getString("description");
            }
            break;
        default:
            break;
        }
        return description;
    }

    /**
     * 解析资源名称数据,去除分区和名称
     * 
     * @param data
     * @param type
     * @return
     */
    public static Map<String, String> parse(String data, String type) {
        String sign = null;
        if ("KIND".equals(type)) {
            sign = "~";
        } else if ("FULLPATH".equals(type)) {
            sign = "/";
        }
        HashMap<String, String> dataMap = Maps.newHashMap();
        String partition = StringUtils.substringBefore(StringUtils.substringAfter(data.trim(), sign), sign);
        String name = StringUtils.substringAfterLast(data.trim(), sign);
        dataMap.put("partition", partition);
        dataMap.put("name", name);
        return dataMap;
    }

}
