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

import cn.axzo.msg.center.domain.utils.IgnorePropsJsonTypeHandler;
import cn.axzo.msg.center.service.ButtonV3;
import cn.axzo.msg.center.service.domain.UrlConfig;
import cn.axzo.msg.center.service.enums.ButtonStyleEnum;
import cn.axzo.msg.center.service.enums.PresetButtonType;
import cn.axzo.msg.center.service.enums.RouterButtonSourceEnum;
import cn.axzo.msg.center.service.enums.RouterCategoryEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
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 java.util.Collections;
import java.util.List;

/**
 * @author yanglin
 */
@Setter
@Getter
@TableName(value = "message_template_button_v3", autoResultMap = true)
public class MessageTemplateButtonV3 extends BaseEntityWithOperator<MessageTemplateButtonV3> implements ButtonV3 {

    /**
     * 按钮名称
     */
    private String name;

    /**
     * 按钮动作完成名称(失效名称)
     */
    private String actionPerformedName;

    /**
     * 编码
     */
    private String code;

    /**
     * 模板code
     */
    private String templateCode;

    /**
     * 预设按钮类型
     */
    private PresetButtonType presetBtnType;

    /**
     * 按钮来源
     */
    private RouterButtonSourceEnum source;

    /**
     * 路由类型
     */
    private RouterCategoryEnum category;

    /**
     * API地址,仅当按钮是接口调用类型时有值
     */
    private String apiUrl;

    /**
     * 按钮链接配置
     */
    @TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
    private UrlConfig urlConfig;

    /**
     * 按钮style配置
     */
    @TableField(typeHandler = FastjsonTypeHandler.class)
    private JSONArray style;

    /**
     * 执行人可见：true 不可见：false
     */
    private Boolean executorShow;

    /**
     * 待办状态可见可见：true 不可见：false
     */
    private Boolean pendingShow;

    /**
     * 按钮优先级，数值越大优先级越低
     */
    private Integer priority;

    @TableField(typeHandler = IgnorePropsJsonTypeHandler.class)
    private RecordExt recordExt;

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

    public RecordExt getOrCreateRecordExt() {
        if (recordExt == null) recordExt = new RecordExt();
        return recordExt;
    }

    @Override
    public List<ButtonStyleEnum> getStyles() {
        if (style == null) return Collections.emptyList();
        return JSON.parseArray(style.toJSONString(), ButtonStyleEnum.class);
    }

    @Override
    public String key() {
        return code;
    }

    @Setter
    @Getter
    public static class RecordExt {
        private Boolean isMigrated;
        private Boolean isSyncedForTest;
    }

}