package jnpf.model.cultivate.dto.course.testoption;

import cn.hutool.core.util.StrUtil;
import jnpf.model.cultivate.po.course.FtbCultivateChapterTestOption;
import lombok.Data;

@Data
public class FtbCultivateChapterTestOptionDTO {

    /**
     * 是否是正确选项（0否，1是）(开启必填)
     */
    private Integer isRightOption;

    /**
     * 选项内容(开启必填)
     */
    private String content;

    /**
     * 选项排序码,从0开始(开启必填)
     */
    private Integer sortCode;

    public static <T extends FtbCultivateChapterTestOptionDTO> FtbCultivateChapterTestOption convertFtbCultivateChapterTestOption(T ftbCultivateChapterTestOptionDTO) {
        if (ftbCultivateChapterTestOptionDTO == null) {
            return null;
        }
        FtbCultivateChapterTestOption ftbCultivateChapterTestOption = new FtbCultivateChapterTestOption();
        ftbCultivateChapterTestOption.setIsRightOption(ftbCultivateChapterTestOptionDTO.getIsRightOption());
        ftbCultivateChapterTestOption.setContent(ftbCultivateChapterTestOptionDTO.getContent());
        ftbCultivateChapterTestOption.setSortCode(ftbCultivateChapterTestOptionDTO.getSortCode());
        return ftbCultivateChapterTestOption;
    }

    /**
     * 随堂测试选项校验
     */
    public static <T extends FtbCultivateChapterTestOptionDTO> void checkChapterTestOptions(T ftbCultivateChapterTestOptionDTO) throws RuntimeException {
        if (ftbCultivateChapterTestOptionDTO.getSortCode() == null) {
            throw new RuntimeException("选项排序码不能为空");
        }
        if (StrUtil.isBlank(ftbCultivateChapterTestOptionDTO.getContent())) {
            throw new RuntimeException("选项内容必填");
        }
        if (ftbCultivateChapterTestOptionDTO.getContent().length() > 100) {
            throw new RuntimeException("选项内容不能超过50个字符");
        }
    }

    public FtbCultivateChapterTestOption convert(String testId) {
        FtbCultivateChapterTestOption option = new FtbCultivateChapterTestOption();
        option.setChapterTestId(testId);
        option.setIsRightOption(getIsRightOption());
        option.setContent(getContent());
        option.setSortCode(getSortCode());
        option.setEnableMark(0); // 设置为有效
        return option;
    }
}
