package cn.axzo.msg.center.message.domain.dto;

import cn.axzo.im.center.api.feign.SendPriority;
import cn.axzo.msg.center.domain.entity.MessageBaseTemplate;
import cn.axzo.msg.center.message.domain.dto.MessageTemplateRouterDTO.MessageRouteButtonDTO;
import cn.axzo.msg.center.service.dto.MessageCardContentItemDTO;
import cn.axzo.msg.center.service.enums.BizDetailJumpStrategyEnum;
import cn.axzo.msg.center.service.enums.MessageCategoryEnum;
import cn.axzo.msg.center.service.enums.PushTerminalEnum;
import cn.axzo.msg.center.service.enums.RouterCategoryEnum;
import cn.axzo.msg.center.utils.JSONObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.collections.CollectionUtils;

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

/**
 * @description
 *
 * @author cold_blade
 * @date 2023/10/5
 * @version 1.0
 */
@Setter
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MessageTemplateDTO implements Serializable {

    private static final long serialVersionUID = -2658385672181452557L;

    private Long id;
    /**
     * 模板名称
     */
    private String name;
    /**
     * 系统自动生成的模板code
     */
    private String code;
    /**
     *  消息详情样式. BIZ_COMMON: 业务待办能用, FLOW_COMMON: 审批待办能用, 其它动态配置的
     */
    private String detailStyleCode;
    /**
     * 模板标题
     */
    private String title;
    /**
     * 模板类容
     */
    private String content;
    /**
     * 消息卡片信息标签列表
     */
    private List<MessageCardContentItemDTO> msgCardContentItems;
    /**
     * 所属消息类型
     */
    private MessageCategoryEnum msgCategory;
    /**
     * 模板icon
     */
    private String icon;
    /**
     * 模板路由信息
     */
    private MessageTemplateRouterDTO msgTemplateRouter;
    /**
     * 推送终端
     */
    private List<PushTerminalEnum> pushTerminals;
    /**
     * APP最小版本支持，可不配
     */
    private String minAppVersion;
    /**
     * 创建人id
     */
    private Long createPersonId;
    /**
     * 创建人姓名
     */
    private String createPersonName;
    /**
     * 更新人id
     */
    private Long updatePersonId;
    /**
     * 更新人姓名
     */
    private String updatePersonName;

    private JSONObject pushData;

    /**
     * IM发送优先级
     */
    private Integer imSendPriority;

    public Integer determineImSendPriority() {
        return imSendPriority == null ? SendPriority.TEMPLATE_MESSAGE.getPriority() : imSendPriority;
    }

    public boolean hasPresetButtons() {
        if (msgTemplateRouter == null)
            return false;
        List<MessageRouteButtonDTO> buttons = msgTemplateRouter.getRouteButtons();
        if (CollectionUtils.isEmpty(buttons))
            return false;
        return buttons.stream()
                .anyMatch(btn -> btn.getCategory() == RouterCategoryEnum.PRESET_BUTTON);
    }

    public boolean isCardJumpToBiz() {
        if (msgTemplateRouter == null) {
            return false;
        }
        MessageTemplateRouterDTO.MessageRouteDetailDTO routeDetail = msgTemplateRouter.getRouteDetail();
        if (routeDetail == null) {
            return false;
        }
        return routeDetail.getJumpStrategy() == BizDetailJumpStrategyEnum.BUSINESS;
    }

    public static MessageTemplateDTO from(MessageBaseTemplate baseTemplate, MessageTemplateRouterDTO msgTemplateRouter) {
        // 业务详情展示策略
        return MessageTemplateDTO.builder()
                .id(baseTemplate.getId())
                .name(baseTemplate.getName())
                .code(baseTemplate.getCode())
                .detailStyleCode(baseTemplate.getDetailStyleCode())
                .title(baseTemplate.getTitle())
                .content(baseTemplate.getContent())
                .msgCardContentItems(JSONObjectUtil.parseArray(baseTemplate.getCardContent(), MessageCardContentItemDTO.class))
                .msgCategory(baseTemplate.getMsgCategory())
                .icon(baseTemplate.getIcon())
                .msgTemplateRouter(msgTemplateRouter)
                .pushTerminals(JSON.parseArray(baseTemplate.getPushTerminal(), PushTerminalEnum.class))
                .minAppVersion(baseTemplate.getMinAppVersion())
                .createPersonId(baseTemplate.getCreatePersonId())
                .createPersonName(baseTemplate.getCreatePersonName())
                .updatePersonId(baseTemplate.getUpdatePersonId())
                .updatePersonName(baseTemplate.getUpdatePersonName())
                .pushData(baseTemplate.getPushData())
                .imSendPriority(baseTemplate.getImSendPriority())
                .build();
    }

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