package cn.axzo.msg.center.service.dto;

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 lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

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

/**
 * @description
 *
 * @author cold_blade
 * @date 2023/9/23
 * @version 1.0
 */
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MessageButtonRouteStrategyDTO implements Serializable {

    private static final long serialVersionUID = -9083376003614521781L;

    /**
     * 按钮唯一标识
     */
    private String identityCode;
    /**
     * 预设按钮类型
     */
    private PresetButtonType presetButtonType;
    /**
     * 路由描述
     */
    private String desc;
    /**
     * 路由分类
     *     JUMP: 直接跳转
     *     ACTION: 接口调用
     *     PRESET_BUTTON: 预设按钮
     */
    private RouterCategoryEnum category;
    /**
     * 按钮来源
     *   SYSTEM: 系统
     *   CUSTOM: 自定义
     */
    private RouterButtonSourceEnum source;
    /**
     * 按钮样式配置
     *   HIGH_LIGHT: 按钮高亮展示
     *   OVER_CARD: 按钮显示在卡片上
     */
    private List<ButtonStyleEnum> style;
    /**
     * 执行人可见：true 不可见：false
     */
    private Boolean executorShow;
    /**
     * 待办状态可见可见：true 不可见：false
     */
    private Boolean pendingShow;
    /**
     * 路由按钮的跳转url配置
     */
    private List<MessageRouterTerminalDTO> terminals;
    /**
     * 当按钮类型为API调用时，该字段有值
     */
    private String apiUrl;

    /**
     * @see {@link MessageTemplateRouterDTO#isValid}
     * <p>要改2处都要记得改
     */
    public boolean isValid() {
        if (RouterCategoryEnum.ACTION.equals(category)) {
            return StringUtils.isNotBlank(apiUrl);
        }
        if (RouterCategoryEnum.PRESET_BUTTON.equals(category)) {
            return presetButtonType != null;
        }
        return CollectionUtils.isNotEmpty(terminals)
                && terminals.stream().allMatch(MessageRouterTerminalDTO::isValid);
    }

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