package com.yihu.hos.services;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.jms.Queue;

/**
 * @created Airhead 2016/8/2.
 */
@Component
public class ServiceFlowEventService {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 当外界组件通知一个新的processor处理器被定义时，该事件被触发。
     *
     * @param serviceFlow 本次processor处理器变化，所涉及的服务流程Code标识。
     * @param packageName processor处理器定义涉及的class包名
     * @param className   processor处理器定义涉及的class类名
     * @param path        processor处理器定义涉及的class内容，如果zookeeper数据结构中class分片存储，在业务级接口层面上也进行了合并
     */
    public void processorAdded(String serviceFlow, String packageName, String className, String path) {
        sendMsg("processorAdded", serviceFlow, packageName, className, path);
    }

    /**
     * 当外界组件通知一个已有的processor处理器data部分发生变化时，该事件被触发。
     */
    public void processorDataChanged(String serviceFlow, String packageName, String className, String path) {
        this.sendMsg("processorDataChanged", serviceFlow, packageName, className, path);
    }

    public void processorDataDeleted(String serviceFlow, String packageName, String className, String path) {
        this.sendMsg("processorDataDeleted", serviceFlow, packageName, className, path);
    }

    /**
     * 当外界组件通知一个新的RouteDefine路由被定义时，该事件被触发
     */
    public void routeDefineAdded(String serviceFlow, String packageName, String className, String path) {
        this.sendMsg("routeDefineAdded", serviceFlow, packageName, className, path);
    }

    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被改变时，主要就是路由定义内容被改变时，该事件被触发。
     */
    public void routeDefineChanged(String serviceFlow, String packageName, String className, String path) {
        this.sendMsg("routeDefineChanged", serviceFlow, packageName, className, path);
    }

    /**
     * 当外界组件通知一个已有的RouteDefine路由定义被删除时，该事件被触发。
     */
    public void routeDefineDelete(String serviceFlow, String packageName, String className) {
        this.sendMsg("routeDefineDelete", serviceFlow, packageName, className, null);
    }

    private void sendMsg(String event, String serviceFlow, String packageName, String className, String path) {
        ObjectNode objectNode = objectMapper.createObjectNode();
        objectNode.put("event", event);
        objectNode.put("serviceFlow", serviceFlow);
        objectNode.put("packageName", packageName);
        objectNode.put("className", className);
        objectNode.put("path", path);
        try {
            String msg = objectMapper.writeValueAsString(objectNode);
            this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
    }
}
