package cn.axzo.msg.center.domain.entity;

import cn.axzo.foundation.dao.support.mysql.type.BaseListTypeHandler;
import cn.axzo.im.center.common.enums.AppTypeEnum;
import cn.axzo.im.center.common.enums.YesOrNo;
import cn.axzo.msg.center.domain.persistence.BaseEntityExt;
import cn.axzo.msg.center.domain.utils.IgnorePropsJsonTypeHandler;
import cn.axzo.msg.center.service.enums.CardBizState;
import cn.axzo.msg.center.service.enums.CardState;
import cn.axzo.msg.center.service.pending.card.domain.CardButtonState;
import cn.axzo.msg.center.service.pending.request.CardContent;
import cn.axzo.msg.center.service.pending.request.CardStateInfo;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * @author yanglin
 */
@Setter
@Getter
@Accessors(chain = true)
@TableName(value = "card", autoResultMap = true)
public class Card extends BaseEntityExt<Card> implements CardContent {

    /**
     * 请求批次号
     */
    private String batchNo;

    /**
     * 调用方标识
     */
    private String appCode;

    /**
     * IM任务id
     */
    private Long imTaskId;

    /**
     * 业务消息ID
     */
    private String bizMessageId;

    /**
     * 卡片编码
     */
    private String identityCode;

    /**
     * 业务编码
     */
    private String bizCode;

    /**
     * 流程类代办的流程结点编码
     */
    private String subBizCode;

    /**
     * 卡片模版编码
     */
    private String templateCode;

    /**
     * 业务状态
     */
    private CardBizState bizState;

    /**
     * 卡片状态
     */
    private CardState cardState;

    /**
     * 标题
     */
    private String title;

    /**
     * 内容
     */
    private String content;

    /**
     * IM卡片信息 (解析后)
     */
    @TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
    private Object cardContent;

    /**
     * 业务扩展参数
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONObject bizParam;

    /**
     * 路由扩展参数
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONObject routerParam;

    /**
     * 发送者的端
     */
    private AppTypeEnum senderAppType;

    /**
     * 接收的端
     */
    private AppTypeEnum receiverAppType;

    /**
     * 发送人是否为机器人. YES: 是, NO: 否
     */
    private YesOrNo isSenderRobot;

    /**
     * 发起者的自然人ID
     */
    private Long senderPersonId;

    /**
     * 发起人的单位id
     */
    private Long senderOuId;

    /**
     * 发起者项目id
     */
    private Long senderWorkspaceId;

    /**
     * 接收人自然人id
     */
    private Long receiverPersonId;

    /**
     * 接收人单位id
     */
    private Long receiverOuId;

    /**
     * 接收人项目id
     */
    private Long receiverWorkspaceId;

    /**
     * 按钮状态
     */
    @TableField(typeHandler = CardButtonStateHandler.class)
    private List<CardButtonState> buttonStates;

    /**
     * 副标题
     */
    private String subtitle;

    /**
     * 消息是否可更新
     */
    private YesOrNo updatable;

    /**
     * 扩展字段
     */
    @TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
    private RecordExt recordExt;

    @Override
    public CardStateInfo getStateInfo() {
        CardStateInfo cardStateInfo = new CardStateInfo();
        cardStateInfo.setBizState(bizState);
        cardStateInfo.setCardState(cardState);
        return cardStateInfo;
    }

    @Override
    public JSONObject getBizParam() {
        return bizParam;
    }

    @Override
    public JSONObject getRouterParam() {
        return routerParam;
    }

    @Setter
    @Getter
    public static class RecordExt {
    }

    // @formatter:off
    public static class CardButtonStateHandler
            extends BaseListTypeHandler<CardButtonState> {}
    // @formatter:on
}