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

import cn.axzo.msg.center.common.exception.ServiceException;
import cn.axzo.msg.center.service.dto.MessageButtonRouteStrategyDTO;
import cn.axzo.msg.center.service.dto.MessageCardContentItemDTO;
import cn.axzo.msg.center.service.dto.MessageDetailRouteStrategyDTO;
import cn.axzo.msg.center.service.enums.MessageCategoryEnum;
import cn.axzo.msg.center.service.enums.PushTerminalEnum;
import cn.axzo.msg.center.service.template.request.MessageTemplateCreateRequest;
import cn.axzo.msg.center.service.template.request.MessageTemplateSyncDto;
import cn.axzo.msg.center.service.template.request.MessageTemplateUpdateRequest;
import cn.axzo.msg.center.utils.MessageTemplateCategoryUtil;
import com.alibaba.fastjson.JSON;
import java.io.Serializable;
import java.util.List;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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 MessageTemplateSaveOrUpdateParam implements Serializable {

    private static final long serialVersionUID = 6657624182580261353L;

    /**
     * 模板编码
     */
    private String templateCode;
    /**
     * 操作者的自然人id
     */
    private Long operatorId;
    /**
     * 操作者的自然人姓名
     */
    private String operatorName;
    /**
     * 模板名称
     */
    private String templateName;
    /**
     * 消息类型
     *   GENERAL_MESSAGE: 通知
     *   BIZ_PENDING_MESSAGE: 业务待办
     *   APPROVAL_PENDING_MESSAGE: 审批待办
     */
    private MessageCategoryEnum msgCategory;
    /**
     * 消息分类树的叶结点的结点编码列表
     */
    private List<String> leafGroupNodes;
    /**
     * 推送终端配置
     *   B_ENTERPRISE_APP: B-安心筑企业版
     *   C_WORKER_APP: C-安心筑工人版
     */
    private List<PushTerminalEnum> pushTerminals;
    /**
     * 该模板最低支持的APP版本号
     */
    private String minAppVersion;
    /**
     * 消息标题
     */
    private String title;
    /**
     * 消息内容
     */
    private String content;
    /**
     * 消息图标
     */
    private String icon;
    /**
     * 消息卡片信息标签列表，可为空
     */
    private List<MessageCardContentItemDTO> msgCardContentItems;
    /**
     * 业务详情展示策略
     */
    private MessageDetailRouteStrategyDTO bizDetailShowStrategy;
    /**
     * 路由策略列表
     */
    private List<MessageButtonRouteStrategyDTO> routers;
    /**
     * 待办样式, BIZ_COMMON: 业务待办能用, FLOW_COMMON: 审批待办能用, 其它动态配置的
     */
    private String detailStyleCode;

    private JSONObject pushData;

    public static MessageTemplateSaveOrUpdateParam from(MessageTemplateCreateRequest request) {
        MessageCategoryEnum category = MessageTemplateCategoryUtil
                .checkAndReturnSubCategory(request.getCategory(), request.getSubCategory())
                .orElseThrow(() -> new ServiceException("无效的参数"));
        return MessageTemplateSaveOrUpdateParam.builder()
                .templateName(request.getTemplateName())
                .detailStyleCode(request.getDetailStyleCode())
                .operatorId(request.getOperatorId())
                .operatorName(request.getOperatorName())
                .msgCategory(category)
                .leafGroupNodes(request.getLeafGroupNodes())
                .title(request.getMsgTitle())
                .content(request.getMsgContent())
                .msgCardContentItems(request.getMsgCardContentItems())
                .icon(request.getMsgIcon())
                .minAppVersion(request.getMinAppVersion())
                .operatorId(request.getOperatorId())
                .routers(request.getRouters())
                .pushTerminals(request.getPushTerminals())
                .bizDetailShowStrategy(request.getDetailStrategy())
                .pushData(request.getPushData())
                .build();
    }

    public static MessageTemplateSaveOrUpdateParam from(MessageTemplateUpdateRequest request) {
        return MessageTemplateSaveOrUpdateParam.builder()
                .templateName(request.getTemplateName())
                .detailStyleCode(request.getDetailStyleCode())
                .operatorName(request.getOperatorName())
                .templateCode(request.getTemplateCode())
                .leafGroupNodes(request.getLeafGroupNodes())
                .title(request.getMsgTitle())
                .content(request.getMsgContent())
                .msgCardContentItems(request.getMsgCardContentItems())
                .minAppVersion(request.getMinAppVersion())
                .icon(request.getMsgIcon())
                .operatorId(request.getOperatorId())
                .operatorName(request.getOperatorName())
                .routers(request.getRouters())
                .pushTerminals(request.getPushTerminals())
                .bizDetailShowStrategy(request.getDetailStrategy())
                .pushData(request.getPushData())
                .build();
    }


    public static MessageTemplateSaveOrUpdateParam fromSync(MessageTemplateSyncDto request) {
        MessageCategoryEnum category = MessageTemplateCategoryUtil
                .checkAndReturnSubCategory(request.getCategory(), request.getSubCategory())
                .orElseThrow(() -> new ServiceException("无效的参数"));
        return MessageTemplateSaveOrUpdateParam.builder()
                .templateName(request.getTemplateName())
                .msgCategory(category)
                .leafGroupNodes(request.getLeafGroupNodes())
                .title(request.getMsgTitle())
                .content(request.getMsgContent())
                .msgCardContentItems(request.getCardContentItems())
                .icon(request.getMsgIcon())
                .minAppVersion(request.getMinAppVersion())
                .operatorId(request.getOperatorId())
                .routers(request.getRouters())
                .pushTerminals(request.getPushTerminals())
                .bizDetailShowStrategy(request.getDetailStrategy())
                .build();
    }

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