package com.sagacloud.utils.objectLog;
/*
 * Author: Jxing
 * Create Time: 2018/7/10
 */

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

import java.util.ArrayList;

public class ActivityLogRunnable implements Runnable {
    ArrayList<ActivityLogObject> logList;
    String msg ;
    ArrayList<String> msgList ;
    public ActivityLogRunnable(ArrayList<ActivityLogObject> logList)
    {
        this.logList = logList;
    }
    public ActivityLogRunnable(String msg)
    {
        this.msg = msg;
    }
    public ActivityLogRunnable() { }
    @Override
    public void run() {
        if(logList != null) {
            for (int i = 0; i < logList.size(); ++i) {
                ActivityLogObject log = logList.get(i);
                try {
                    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 e) {
                    e.printStackTrace();
                }
            }
        }
        if(msg != null){
            MessageHandler.publish(msg);
        }
        if(msgList != null && msgList.size() > 0){
            for(int i = 0; i < msgList.size(); ++i){
                MessageHandler.publish(msgList.get(i));
            }
        }
    }
    public 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));
        if(msgList == null)
            msgList = new ArrayList<>();
        msgList.add(obj.toString());
    }

    public 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));
        if(msgList == null)
            msgList = new ArrayList<>();
        msgList.add(obj.toString());
    }

    public 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));
        if(msgList == null)
            msgList = new ArrayList<>();
        msgList.add(obj.toString());
    }

    public 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));
        if(msgList == null)
            msgList = new ArrayList<>();
        msgList.add(obj.toString());
    }
}
