package cn.com.qmth.stmms.admin.dto;

import cn.com.qmth.stmms.common.enums.ObjectivePolicy;
import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;
import cn.com.qmth.stmms.common.annotation.ExcelField;
import cn.com.qmth.stmms.common.enums.QuestionType;

public class ObjectiveQuestionDTO implements QuestionDTO {

    private static final String NULL_PAPER_TYPE_PLACEHOLDER = "#";

    @ExcelField(title = "科目代码", align = 2, sort = 10)
    private String subjectCode;

    @ExcelField(title = "科目名称", align = 2, sort = 20)
    private String subjectName;

    @ExcelField(title = "试卷类型", align = 2, sort = 30)
    private String paperType;

    @ExcelField(title = "大题名称", align = 2, sort = 40)
    private String title;

    @ExcelField(title = "题目昵称", align = 2, sort = 50)
    private String name;

    @ExcelField(title = "大题号(只能用小写数字)", align = 2, sort = 60)
    private Integer mainNumber;

    @ExcelField(title = "小题号(只能用小写数字)", align = 2, sort = 70)
    private String subNumber;

    @ExcelField(title = "标准答案", align = 2, sort = 80)
    private String answer;

    @ExcelField(title = "小题满分", align = 2, sort = 90)
    private Double totalScore;

    @ExcelField(title = "题型(1-单选，2-多选，3-判断)", align = 2, sort = 100, type = 2)
    private Integer type;

    public ObjectiveQuestionDTO() {

    }

    public ObjectiveQuestionDTO(ExamQuestion question, ExamSubject subject) {
        setSubjectCode(question.getSubjectCode());
        setSubjectName(subject != null ? subject.getName() : "");
        setPaperType(question.getPaperType());
        setMainNumber(question.getMainNumber());
        setSubNumber(question.getSubNumber());
        setTitle(question.getMainTitle());
        setName(question.getName());
        setAnswer(question.getAnswer());
        setTotalScore(question.getTotalScore());
        if(question.getType()!=null){
            setType(question.getType().getValue());
        }
    }

    public ExamQuestion transform() {
        ExamQuestion question = new ExamQuestion();
        question.setSubjectCode(StringUtils.trimToNull(subjectCode));
        question.setPaperType(StringUtils.trimToNull(paperType));
        if (question.getPaperType() == null) {
            question.setPaperType(NULL_PAPER_TYPE_PLACEHOLDER);
        }
        question.setMainNumber(mainNumber);
        question.setSubNumber(subNumber);
        question.setGroupNumber(0);
        question.setObjective(true);
        question.setMainTitle(StringUtils.trimToNull(title));
        question.setName(StringUtils.trimToNull(name));
        question.setAnswer(StringUtils.trimToNull(answer) == null ? null : StringUtils.trimToNull(answer).toUpperCase());
        question.setTotalScore(totalScore);
        question.setIntervalScore(1d);
        question.setType(QuestionType.findByValue(type));
        if(QuestionType.MULTIPLE.equals(question.getType())){
            question.setObjectivePolicy(ObjectivePolicy.PROPORTION);
        }
        return question;
    }

    public String getSubjectCode() {
        return subjectCode;
    }

    public void setSubjectCode(String subjectCode) {
        this.subjectCode = subjectCode;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }

    public Integer getMainNumber() {
        return mainNumber;
    }

    public void setMainNumber(Integer mainNumber) {
        this.mainNumber = mainNumber;
    }

    public String getSubNumber() {
        return subNumber;
    }

    public void setSubNumber(String subNumber) {
        this.subNumber = subNumber;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public Double getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(Double totalScore) {
        this.totalScore = totalScore;
    }

    public String getPaperType() {
        return paperType;
    }

    public void setPaperType(String paperType) {
        this.paperType = paperType;
    }

    public Integer getType() {
        return type;
    }

    public void setType(Integer type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
