package com.qmth.teachcloud.mark.bean.answerbatch;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import com.qmth.boot.core.exception.ParameterException;
import com.qmth.teachcloud.mark.entity.ScanAnswerCard;
import com.qmth.teachcloud.mark.entity.ScanPaperPage;

public class AnswerPaper {

    @NotNull(message = "paper.number不能为空")
    @Min(value = 1, message = "paper.number不能小于1")
    private Integer number;

    @NotNull(message = "paper.assigned不能为空")
    private Boolean assigned;

    @NotNull(message = "paper.invalid")
    private Boolean invalid;

    @Valid
    @NotNull(message = "paper.pages不能为空")
    @Size(min = 1, message = "paper.pages不能为空")
    private List<AnswerPage> pages;

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

    public Boolean getAssigned() {
        return assigned;
    }

    public void setAssigned(Boolean assigned) {
        this.assigned = assigned;
    }

    public List<AnswerPage> getPages() {
        return pages;
    }

    public void setPages(List<AnswerPage> pages) {
        this.pages = pages;
    }

    /**
     * 根据卡格式定义验证页数
     *
     * @param answerCard
     */
    public void answerCardValidate(ScanAnswerCard answerCard) {
        if (answerCard.getSinglePage() && getPages().size() != 1) {
            throw new ParameterException("卡格式页数不一致");
        }
        if (!answerCard.getSinglePage() && getPages().size() != 2) {
            throw new ParameterException("卡格式页数不一致");
        }
    }

    public List<ScanPaperPage> buildPageList() {
        List<ScanPaperPage> list = new ArrayList<>();
        for (AnswerPage answerPage : pages) {
        	ScanPaperPage page = new ScanPaperPage();
            page.setPageIndex(answerPage.getIndex());
            page.setSheetPath(answerPage.getSheetUri());
            page.setAbsent(answerPage.getAbsent());
            page.setBreach(answerPage.getBreach());
            page.setQuestion(answerPage.getQuestion());
            page.setRecogData(answerPage.getRecogData());
            list.add(page);
        }
        return list;
    }

    public Boolean getInvalid() {
        return invalid;
    }

    public void setInvalid(Boolean invalid) {
        this.invalid = invalid;
    }
}
