package com.sagacloud.utils.objectLog;

import com.google.common.base.Strings;
import com.sagacloud.broadcast.MessageHandler;
import com.sagacloud.utils.CommonMethods;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentLinkedQueue;

public class JMSRunnable implements Runnable {
    private static ConcurrentLinkedQueue<ActivityLogObject> logQueue = new ConcurrentLinkedQueue<>();
    private static ConcurrentLinkedQueue<String> jmsQueue = new ConcurrentLinkedQueue<>();
    private static final Logger log = LoggerFactory.getLogger(JMSRunnable.class);
    @Override
    public void run() {
        while(true){
            try {
                ActivityLogObject log = logQueue.poll();
                if (log != null){
                    if (log.getProjectId() != null)
                        ActivityLogUtil.objectLog(log.getId(), log.getTime(), log.getOt(), log.getType(), log.getProjectId());
                    else
                        ActivityLogUtil.objectLog(log.getId(), log.getTime(), log.getOt(), log.getType());
                }
            }catch (Exception ex){
                ex.printStackTrace();
                log.error("保存到log表失败:" + ex.getMessage());
            }
            String jmsMsg = jmsQueue.poll();
            try {

                if (jmsMsg != null && jmsMsg.length() > 0) {
                    MessageHandler.publish(jmsMsg);
                }
            }catch (Exception ex){
                ex.printStackTrace();
                log.error("发送到JMS失败:" + jmsMsg + "," + ex.getMessage());
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static void setRelationChangeMsg_8(String projectId, String peId, String linkId, String description){
        JSONObject obj = new JSONObject();
        obj.put("type", 8);
        obj.put("projectId", Strings.nullToEmpty(projectId));
        obj.put("id", Strings.nullToEmpty(peId));
        obj.put("linkId", Strings.nullToEmpty(linkId));
        obj.put("name", "relationChange");
        obj.put("description", Strings.nullToEmpty(description));
        jmsQueue.offer(obj.toString());
    }

    public static void setUpdateFamilyMsg_9(String peId, String oldFamily, String newFamily, String projectId, String description){
        JSONObject obj = new JSONObject();
        obj.put("type", 9);
        obj.put("id", Strings.nullToEmpty(peId));
        obj.put("oldFamily", Strings.nullToEmpty(oldFamily));
        obj.put("family", Strings.nullToEmpty(newFamily));
        obj.put("projectId", Strings.nullToEmpty(projectId));
        obj.put("description", Strings.nullToEmpty(description));
        jmsQueue.offer(obj.toString());
    }

    public static void setUpdateProjectMsg_10(String peId, String previousProjectId, String projectId, String description){
        JSONObject obj = new JSONObject();
        obj.put("type", 10);
        obj.put("id", Strings.nullToEmpty(peId));
        obj.put("projectId", CommonMethods.getProject(Strings.nullToEmpty(projectId), true));
        obj.put("previousProjectId", CommonMethods.getProject(Strings.nullToEmpty(previousProjectId), true));
        obj.put("description", Strings.nullToEmpty(description));
        jmsQueue.offer(obj.toString());
    }

    public static void setUpdateFamilyDeleteInfosMsg_11(String peId, String projectId, String description){
        JSONObject obj = new JSONObject();
        obj.put("type", 11);
        obj.put("id", Strings.nullToEmpty(peId));
        obj.put("projectId", CommonMethods.getProject(Strings.nullToEmpty(projectId), true));
        obj.put("description", Strings.nullToEmpty(description));
        jmsQueue.offer(obj.toString());
    }

    public static void offerLog(ArrayList<ActivityLogObject> logList)
    {
        if(logList == null || logList.size() == 0)
            return;
        for(ActivityLogObject log : logList){
            logQueue.offer(log);
        }
    }

    public static void offerLog(ActivityLogObject log){
        logQueue.offer(log);
    }
    public static void offerJMSMsg(String msg){
        jmsQueue.offer(msg);
    }
}
