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

import cn.com.qmth.examcloud.core.questions.base.IdUtils;
import cn.com.qmth.examcloud.core.questions.dao.entity.*;
import cn.com.qmth.examcloud.core.questions.service.bean.condition.Condition;

import java.util.*;

public class DetailContext extends Context {

    private PaperContext paperContext;

    private PaperDetailStruct detailStruct;

    private List<UnitContext> units;

    public DetailContext(PaperDetailStruct detailStruct, PaperContext paperContext, List<Condition> conditions) {
        super(conditions);
        this.detailStruct = detailStruct;
        this.units = new LinkedList<UnitContext>();
        this.paperContext = paperContext;
        for (PaperDetailUnitStruct unit : detailStruct.getPaperDetailUnitStructs()) {
            units.add(new UnitContext(unit, this, conditions));
        }
    }

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

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

    public boolean finish() {
        return selectQues.size() == units.size();
    }

    public Map<PaperDetail, List<PaperDetailUnit>> build(PaperDetailStruct detailStruct) {
        Map<PaperDetail, List<PaperDetailUnit>> detailMap = new HashMap<>();
        PaperDetail pd = new PaperDetail();
        List<PaperDetailUnit> unitList = new ArrayList<>();
        pd.setId(IdUtils.uuid());
        for (int i = 0; i < units.size(); i++) {
            UnitContext uc = units.get(i);
            PaperDetailUnit pdu = uc.build(uc.getUnitStruct());
            pdu.setPaperDetail(pd);
            unitList.add(pdu);
        }
        pd.setUnitCount(detailStruct.getDetailCount());
        pd.setName(detailStruct.getName());
        pd.setTitle(detailStruct.getRemark());
        pd.setNumber(detailStruct.getNumber());
        pd.setScore(detailStruct.getTotalScore());
        detailMap.put(pd, unitList);
        return detailMap;
    }

    public PaperContext getPaperContext() {
        return paperContext;
    }

    public PaperDetailStruct getDetailStruct() {
        return detailStruct;
    }

    public List<UnitContext> getUnits() {
        return units;
    }

}
