package cn.axzo.msg.center.event.payload;

import cn.axzo.im.center.common.enums.AppTypeEnum;
import cn.axzo.msg.center.domain.enums.MsgRouteTypeEnum;
import cn.axzo.msg.center.service.enums.TerminalTypeEnum;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MessageHistoryUpdatedPayload implements Serializable {

    private MessageHistory newMessageHistory;
    private MessageHistory oldMessageHistory;

    public boolean isSuccess() {
        return !Objects.equals(newMessageHistory.getStatus(), oldMessageHistory.getStatus())
                && Objects.equals(newMessageHistory.getStatus(), MessageHistory.Status.SUCCEED.name());
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MessageHistory implements Serializable {

        private static final long serialVersionUID = 1L;

        private Long id;

        /**
         * 上游业务请求ID
         */
        private String bizId;

        /**
         * 普通用户，通过appType包装
         * 包装以后进行账户注册
         */
        private String messageId;

        /**
         * 发送者IM账户
         */
        private String fromAccount;

        /**
         * 发送者IM账户
         */
        private String toAccount;

        /**
         * 终端类型
         *
         * @see AppTypeEnum
         */
        private String appType;

        /**
         * channel 网易云信
         */
        private String channel;

        private String messageBody;

        private String result;

        private Long imMessageTaskId;

        private String receivePersonId;

        private Long receiveOuId;

        private String status;

        private Integer isDelete;

        private Date createAt;

        private Date updateAt;

        public enum Status {
            PENDING,
            SUCCEED,
            FAILED,
            ;
        }

        public MessageBody resolveMessageBody() {
            return JSONObject.parseObject(messageBody, MessageBody.class);
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MessageBody {

        /**
         * 模板消息:Template、聊天消息:Chat、通知消息:Notify
         */
        private String msgType;

        /**
         * 消息标题
         */
        private String msgHeader;

        /**
         * 消息内容
         */
        private String msgContent;

        /**
         * 消息通知结构体
         */
        private String msgBody;
        
        public MsgBody resolveMsgBody() {
            return JSONObject.parseObject(msgBody, MsgBody.class);
        }
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class MsgBody {
        private CardDetailButton cardDetailButton;
    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class CardDetailButton {
        private List<ActionPath> actionPaths;


    }

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ActionPath {
        private TerminalTypeEnum platform;

        private String url;
    }
}
