package cn.axzo.msg.center.im.domain.event;

import cn.axzo.msg.center.im.domain.enums.IMSendMessageStatusEnum;
import cn.axzo.msg.center.im.domain.param.IMSendSingleMessageParam;
import com.alibaba.fastjson.JSON;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.concurrent.atomic.LongAdder;

/**
 * @description
 *   即时通信发送消息事件
 *
 * @author cold_blade
 * @date 2023/9/27
 * @version 1.0
 */
@Setter
@Getter
public class IMSendSingleMessageEvent implements Serializable {

    private static final long serialVersionUID = 4652087357183307179L;

    /**
     * 待发送的消息
     */
    private IMSendSingleMessageParam param;
    /**
     * 充实次数,默认重试3次,必须为非负数
     */
    private long retryCnt = 3L;
    /**
     * 充实次数计数器
     */
    private final LongAdder retryCounter = new LongAdder();
    /**
     * 消息发送状态
     */
    private IMSendMessageStatusEnum status = IMSendMessageStatusEnum.SENDING;
    /**
     * 发送失败时的错误信息
     */
    private String errInfo;

    public static IMSendSingleMessageEvent build(IMSendSingleMessageParam param) {
        IMSendSingleMessageEvent event = new IMSendSingleMessageEvent();
        event.setParam(param);
        return event;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
