package com.bocloud.ims.service.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BocloudResult;
import com.bocloud.common.model.BocloudToken;
import com.bocloud.common.model.Param;
import com.bocloud.common.model.Sign;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.service.core.CurrentService;
import com.bocloud.service.core.Service;
import com.bocloud.service.http.core.ServiceFactory;
import com.bocloud.service.http.model.RemoteService;

/**
 * cms服务内部访问
 *
 * @author wangyu
 * @version 4.1.0-SNAPSHOT
 * @since 2018/12/25
 */
@Component
public class CmsInternalService implements InitializingBean {

	@Autowired
	private ServiceFactory serviceFactory;
	@Autowired
	private CurrentService currentService;
	private static Service service = Service.create(BoCloudService.CMS);
	private Map<String, Object> headers = new HashMap<>(4);

	public BocloudResult vmCpu(String apiKey) {
		headers.put(Common.API_KEY, apiKey);
		RemoteService remote = serviceFactory.build(service, "/v1/tops", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.PARAMS, "vmCpu"));
		return remote.invoke();
	}

	public BocloudResult vmMem(String apiKey) {
		headers.put(Common.API_KEY, apiKey);
		RemoteService remote = serviceFactory.build(service, "/v1/tops", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.PARAMS, "vmMem"));
		return remote.invoke();
	}

	public BocloudResult alarmStats(String apiKey, String level) {
		headers.put(Common.API_KEY, apiKey);
		List<Param> paramList = new ArrayList<>();
		paramList.add(new Param(MapTools.simpleMap("eventAlarm", 0), Sign.EQ));
		paramList.add(new Param(MapTools.simpleMap("status", "SOLVED"), Sign.UEQ));
		if (StringUtils.isNotBlank(level)) {
			paramList.add(new Param(MapTools.simpleMap("level", level), Sign.EQ));
		}
		Map<String, Object> params = MapTools.simpleMap(Common.PARAMS, JSONArray.toJSONString(paramList));
		params.put("page", 1);
		params.put("rows", Integer.MAX_VALUE);
		params.put("simple", false);
		RemoteService remote = serviceFactory.build(service, "/v1/alarms/stats", BoCloudMethod.GET, headers, params);
		return remote.invoke();
	}

	public Double cephTotal() {
		JSONObject params = new JSONObject();
		params.put("expr", "round(sum(ceph_osd_stat_bytes)/1024^3,0.01)");
		params.put("simple", true);
		RemoteService remote = serviceFactory.build(service, "/v1/prometheus", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.PARAMS, params.toJSONString()));
		BocloudResult result = remote.invoke();
		if (result.isFailed()) {
			return 0.0;
		}
		Object data = result.getData();
		if (null == data) {
			return 0.0;
		}
		return Double.valueOf(JSONObject.toJSONString(data));
	}

	public Double cephUsed() {
		JSONObject params = new JSONObject();
		params.put("expr", "round(sum(ceph_osd_stat_bytes_used)/1024^3,0.01)");
		params.put("simple", true);
		RemoteService remote = serviceFactory.build(service, "/v1/prometheus", BoCloudMethod.GET, headers,
				MapTools.simpleMap(Common.PARAMS, params.toJSONString()));
		BocloudResult result = remote.invoke();
		if (result.isFailed()) {
			return 0.0;
		}
		Object data = result.getData();
		if (null == data) {
			return 0.0;
		}
		return Double.valueOf(JSONObject.toJSONString(data));
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		this.headers.put(BocloudToken.INTERNAL_TOKEN, currentService.getToken());
		this.headers.put("source", currentService.getService().getName());
	}
}
