package com.bocloud.ims.service.internal;

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.ims.entity.resource.Alarm;
import com.bocloud.ims.service.utils.Operation;
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;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * csp服务调用
 *
 * @author tangcq
 */
@Component("cspAlarmService")
public class CspAlarmService implements InitializingBean {

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

    /**
     * 查询多个节点的告警 key是guid value是数组
     *
     * @param guids
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String, List<Alarm>> topologyAlarm(List<String> guids) {
        Map<String, Object> paramsMap = Maps.newHashMap();
        paramsMap.put("guids", JSONArray.toJSON(guids));
        Map<String, Object> params = Maps.newHashMap();
        params.put("params", JSONObject.toJSON(paramsMap));
        RemoteService remoteService = serviceFactory.build(service, "/v1/alarms/topology", BoCloudMethod.GET, headers,
                params);
        BocloudResult result = remoteService.invoke();
        if (result.isFailed()) {
            logger.error("查询错误", result.getMessage());
            return Maps.newHashMap();
        }
        HashMap<String, List<Alarm>> rMap = Maps.newHashMap();
        List<Alarm> alarms = JSONArray.parseArray(String.valueOf(result.getData())).toJavaList(Alarm.class);
        for (Alarm alarm : alarms) {
            List<Alarm> alarmsData = rMap.get(alarm.getGuid());
            if (null == alarmsData) {
                alarmsData = Lists.newArrayList();
                alarmsData.add(alarm);
                rMap.put(alarm.getGuid(), alarmsData);
            } else {
                int oldLevel = Operation.grade(alarmsData.get(0).getLevel());
                int newLevel = Operation.grade(alarm.getLevel());
                if (0 == oldLevel || 0 == newLevel) {
                    logger.error("告警等级存在问题，请检查！");
                    return Maps.newHashMap();
                }
                if (oldLevel > newLevel) {
                    alarmsData.remove(0);
                    alarmsData.add(alarm);
                }
            }
        }
        return rMap;
    }

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

}
