package cn.axzo.msg.center.service.pending.response.v3.model;

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 cn.axzo.msg.center.service.pending.response.MessageButton;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.Setter;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

/**
 * @author yanglin
 */
@Setter
@Getter
public class ParsedButtonV3 implements MessageButton {

    private Long id;

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

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

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

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

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

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

    /**
     * API地址,仅当按钮是接口调用类型时有值
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private String apiUrl;

    /**
     * 按钮链接配置
     */
    @JsonIgnore
    private UrlConfig urlConfig;

    /**
     * 按钮链接
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private SessionUrlInfo url;

    /**
     * 按钮style配置
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private JSONArray style;

    /**
     * 执行人可见：true 不可见：false
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private Boolean executorShow;

    /**
     * 待办状态可见可见：true 不可见：false
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private Boolean pendingShow;

    /**
     * 按钮优先级，数值越大优先级越低
     */
    @Nullable @JsonInclude(JsonInclude.Include.NON_NULL)
    private Integer priority;

    public int determinePriority() {
        return priority == null ? Integer.MAX_VALUE : priority;
    }

    public boolean determinePendingShow() {
        return pendingShow != null && pendingShow;
    }

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

    public boolean hasStyle(ButtonStyleEnum style) {
        return parseStyle().contains(style);
    }

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

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