package com.qmth.teachcloud.common.util;

import com.alibaba.fastjson.JSON;
import com.qmth.teachcloud.common.bean.vo.PaperInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 命题任务工具类
 */

@Component
public class ExamTaskUtil {
    private final static Logger log = LoggerFactory.getLogger(ExamTaskUtil.class);

    /**
     * 解析试卷数据，指定了paperTypes,则返回对应卷型对象，为空，返回所有卷型对象
     *
     * @param path       试卷json
     * @param paperTypes 指定卷型（可为空）
     */
    public static List<PaperInfoVo> parsePaperAttachmentPath(String path, String paperTypes) {
        List<PaperInfoVo> paperInfoVos = new ArrayList<>();
        if (StringUtils.isNotBlank(path)) {
            paperInfoVos = JSON.parseArray(path, PaperInfoVo.class);
            if (StringUtils.isBlank(paperTypes)) {
                return paperInfoVos;
            } else {
                List<String> paperTypeList = Arrays.asList(paperTypes.split(","));
                paperInfoVos = paperInfoVos.stream().filter(m -> paperTypeList.contains(m.getName())).collect(Collectors.toList());
            }
        }
        return paperInfoVos;
    }

    public static List<PaperInfoVo> parsePaperAttachmentPath(String path) {
        return parsePaperAttachmentPath(path, null);
    }

}
