package cn.com.qmth.examcloud.core.questions.service.bean.context;

import java.util.List;

import org.bson.types.ObjectId;

import cn.com.qmth.examcloud.core.questions.base.enums.PaperType;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnit;
import cn.com.qmth.examcloud.core.questions.dao.entity.PaperDetailUnitStruct;
import cn.com.qmth.examcloud.core.questions.dao.entity.Question;
import cn.com.qmth.examcloud.core.questions.service.bean.condition.Condition;

public class UnitContext extends Context {

    private DetailContext detailContext;

    private PaperDetailUnitStruct unitStruct;

    public UnitContext(PaperDetailUnitStruct unitStruct, DetailContext detailContext, List<Condition> conditions) {
        super(conditions);
        this.unitStruct = unitStruct;
        this.detailContext = detailContext;

    }

    public boolean check(Question question) {
        return super.check(question) && detailContext.check(question);
    }

    public void select(Question question) {
        super.select(question);
        detailContext.select(question);
    }

    public boolean finish() {
        return selectQues.size() > 0;
    }

    public PaperDetailUnit build(PaperDetailUnitStruct unitStruct) {
        PaperDetailUnit pdu = new PaperDetailUnit();
        pdu.setId(new ObjectId().toString());
        pdu.setQuestionType(unitStruct.getQuestionType());
        pdu.setScore(unitStruct.getScore());
        pdu.setNumber(unitStruct.getNumber());
        pdu.setQuestion(this.getSelectQues().iterator().next());
        pdu.setPaperType(PaperType.GENERATE);
        return pdu;
    }

    public DetailContext getDetailContext() {
        return detailContext;
    }

    public PaperDetailUnitStruct getUnitStruct() {
        return unitStruct;
    }

}
