package com.elab.log.utils;

import com.alibaba.fastjson.JSON;
import com.dianping.cat.Cat;
import com.dianping.cat.message.TaggedTransaction;
import junit.framework.TestCase;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @Module TODO
 * @Description TODO
 * @Author liukaixiong
 * @Date 2020/11/4 11:41
 */
public class CatCrossProcessTest extends TestCase {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Test
    public void testCatMsg() throws Exception {
        CatCrossProcess.createRemoteMQMsg("TEST", "LOG-MSG", (idMap) -> {
            String logInfo = JSON.toJSONString(idMap);
            String nodeLogId = idMap.get(Cat.Context.CHILD);
            String logId = Cat.getCurrentMessageId();
            logger.info("cat test");
            logger.info("---------->>>>>" + JSON.toJSONString(idMap));
            logger.info("---------->>>>>msg id : " + Cat.getCurrentMessageId());
//            ForkedTransaction forkedTransaction = Cat.newForkedTransaction("fork-test", "remote-msg");
//            String forkedMessageId = forkedTransaction.getForkedMessageId();
//            forkedTransaction.setSuccessStatus();
//            logger.info("----forkedMessageId----" + forkedMessageId);
//            forkedTransaction.complete();

            try {
                buildRemoteMsg(idMap, "  1 收到消息");
            } catch (Exception e) {
                e.printStackTrace();
            }


            // 全新的远程消息
//            CatCrossProcess.createRemoteMsg("TEST", "LOG-MSG", (idMaps) -> {
//                try {
//                    buildRemoteMsg(idMaps, "  4 收到消息");
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//                return null;
//            });

            return null;
        });

        System.out.println("处理完成了");
        Thread.sleep(50000);
        System.in.read();
    }


    @Test
    public void testCreateConsumerCross() throws Exception {
        CatCrossProcess.createRemoteMQMsg("TEST", "LOG-MSG", (idMap) -> {
            String logInfo = JSON.toJSONString(idMap);
            String logId = Cat.getCurrentMessageId();
            logger.info("cat test");
            logger.info("---------->>>>>" + JSON.toJSONString(idMap));
            logger.info("---------->>>>>msg id : " + Cat.getCurrentMessageId());
            System.out.println(logInfo);
            try {
                // 根据当前上下文产生的消息
                buildRemoteMQMsg(idMap, "  1 收到消息");
                buildRemoteMQMsg(idMap, "  2 收到消息");
                buildRemoteMQMsg(idMap, "  3 收到消息");
            } catch (Exception e) {
                e.printStackTrace();
            }
            // 全新的远程消息
            CatCrossProcess.createRemoteMsg("TEST", "LOG-MSG", (idMaps) -> {
                try {
                    buildRemoteMsg(idMaps, "  4 收到消息");
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            });

            return null;
        });
        // Thread.sleep(5000);
        System.in.read();
    }

    private void buildRemoteMQMsg(Map<String, String> idMap, String text) throws Exception {
        new Thread(() -> {
            CatCrossProcess.buildRemoteMQMsg("TEST", "REMOTE-LOG-MSG", idMap, () -> {
                logger.info(" 远程接收到消息 :" + text + " \t 消息编号 : " + Cat.getCurrentMessageId());


                return null;
            });
        }).start();

    }

    private void buildRemoteMsg(Map<String, String> idMap, String text) throws Exception {
        new Thread(() -> {
            CatCrossProcess.buildRemoteMsg("TEST", "REMOTE-LOG-MSG", idMap, () -> {
                logger.info(" 远程接收到消息 :" + text + " \t 消息编号 : " + Cat.getCurrentMessageId());
                String childLog = idMap.get(Cat.Context.PARENT);
                TaggedTransaction taggedTransaction = Cat.newTaggedTransaction("remote-test", "remote-name", Cat.getCurrentMessageId());
                taggedTransaction.bind(childLog, childLog, "MQ远程测试啊");
                taggedTransaction.setSuccessStatus();
                taggedTransaction.complete();

                return null;
            });
        }).start();
    }
}