package cn.axzo.msg.center.utils;

import cn.axzo.basics.common.util.AssertUtil;
import cn.axzo.msg.center.common.exception.ServiceException;
import cn.axzo.msg.center.service.enums.MessageCategoryEnum;
import cn.axzo.msg.center.service.enums.MessageGroupCategoryEnum;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author cold_blade
 * @date 2023/11/7
 * @version 1.0
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class MessageTemplateCategoryUtil {

    /**
     * 校验并返回对应的消息模板子类型
     *
     * @param category     消息模板类型
     * @param subCategory  消息模板子类型
     * @return 消息模板子类型
     */
    public static Optional<MessageCategoryEnum> checkAndReturnSubCategory(MessageGroupCategoryEnum category,
                                                                          MessageCategoryEnum subCategory) {
        AssertUtil.notNull(category, "category is null.");
        if (Objects.isNull(subCategory) && MessageGroupCategoryEnum.NOTIFICATION.equals(category)) {
            // 通知类型的消息模板无二级分类
            return category.getMsgCategories().stream().findFirst();
        }
        return category.getMsgCategories().stream()
                .filter(e -> e.equals(subCategory))
                .findFirst();
    }

    /**
     * 返回消息模板子类型对应的父类型
     *
     * @param subCategory  消息模板子类型
     * @return 消息模板父类型
     */
    public static MessageGroupCategoryEnum parentCategory(MessageCategoryEnum subCategory) {
        AssertUtil.notNull(subCategory, "subCategory is null.");
        return Arrays.stream(MessageGroupCategoryEnum.values())
                .filter(e -> e.getMsgCategories().contains(subCategory))
                .findFirst()
                .orElseThrow(() -> new ServiceException("无效的参数"));
    }

    /**
     * 返回消息模板子类型
     *
     * @param subCategory  消息模板子类型
     * @return 消息模板子类型
     */
    public static MessageCategoryEnum parseAndReturnSubCategory(MessageCategoryEnum subCategory) {
        AssertUtil.notNull(subCategory, "subCategory is null.");
        MessageGroupCategoryEnum parent = parentCategory(subCategory);
        if (MessageGroupCategoryEnum.NOTIFICATION.equals(parent)) {
            return null;
        }
        return subCategory;
    }

    /**
     * 解析消息模板类型查询条件
     *
     * @param category     消息模板类型
     * @param subCategory  消息模板子类型
     * @return 消息模板子类型
     */
    public static Set<MessageCategoryEnum> parseCategoryCondition(MessageGroupCategoryEnum category,
                                                                  MessageCategoryEnum subCategory) {
        if (Objects.isNull(category)) {
            return Collections.emptySet();
        }
        if (Objects.isNull(subCategory)) {
            return category.getMsgCategories();
        }
        return category.getMsgCategories().stream()
                .filter(e -> e.equals(subCategory))
                .collect(Collectors.toSet());
    }
}
