package com.usky.rule.vo.log;

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

import com.usky.rule.util.JsonUtil;
import com.usky.rule.vo.visualization.AlarmSimpleVO;
import org.springframework.util.CollectionUtils;

public class ActionAlarmEventSerialization implements RuleEngineLogSerialization<AlarmEventLog> {
    public ActionAlarmEventSerialization() {
    }

    public AlarmEventLog serialize(String detail) {
        return null;
    }

    public Content toContent(Object obj) {
        String initStr = "已生成告警事件";
        if (obj instanceof Map) {
            AlarmActionLog alarmActionLog = (AlarmActionLog)JsonUtil.toObject((Map)obj, AlarmActionLog.class);
            return getContent(alarmActionLog, initStr);
        } else if (obj instanceof AlarmActionLog) {
            return getContent((AlarmActionLog)obj, initStr);
        } else {
            throw new ClassCastException("ActionAlarmEventSerialization不支持该类型的转换");
        }
    }

    static Content getContent(AlarmActionLog alarmActionLog, String initStr) {
        Content content = new Content();
        List<AlarmEventLog> alarmEventLogs = alarmActionLog.getAlarms();
        StringBuilder builder = new StringBuilder();
        if (!CollectionUtils.isEmpty(alarmEventLogs)) {
            for(int j = 0; j < alarmEventLogs.size(); ++j) {
                AlarmEventLog alarmEventLog = (AlarmEventLog)alarmEventLogs.get(j);
                if (j > 0) {
                    builder.append("\n");
                }

                builder.append(initStr);
                List<AlarmSimpleVO> notifiers = alarmEventLog.getNotifiers();
                if (!CollectionUtils.isEmpty(notifiers)) {
                    builder.append("，告警设备：");
                }

                for(int i = 0; i < notifiers.size(); ++i) {
                    AlarmSimpleVO notifier = (AlarmSimpleVO)notifiers.get(i);
                    if (i > 0 && i < notifiers.size() - 1) {
                        builder.append("、");
                    }

                    builder.append(notifier.getName());
                }
            }
        }

        content.setContent(builder.toString());
        content.setTime(alarmActionLog.getTime());
        return content;
    }

    public String getType() {
        return "action:alarmEvent";
    }
}
