package cn.com.qmth.stmms.biz.exam.model;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import org.apache.commons.lang3.StringUtils;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;

/**
 * 针对个人专属的试卷结构，对接分布式印刷模式
 * 
 * @author luoshi
 *
 */
@Entity
@Table(name = "eb_exam_student_paper")
public class ExamStudentPaper implements Serializable {

    private static final long serialVersionUID = -8728455437479640113L;

    @EmbeddedId
    private ExamNumberPK pk;

    @Column(name = "struct", nullable = false, columnDefinition = "text")
    private String struct;

    public ExamStudentPaper() {
        this.pk = new ExamNumberPK();
    }

    public Integer getExamId() {
        return pk.getExamId();
    }

    public void setExamId(Integer examId) {
        pk.setExamId(examId);
    }

    public String getExamNumber() {
        return pk.getExamNumber();
    }

    public void setExamNumber(String examNumber) {
        pk.setExamNumber(examNumber);
    }

    public void setObjective(boolean objective) {
        pk.setObjective(objective);
    }

    public boolean isObjective() {
        return pk.isObjective();
    }

    public String getStruct() {
        return struct;
    }

    public void setStruct(String struct) {
        this.struct = struct;
    }

    public List<QuestionDetail> getDetailList() {
        return parseFromJson(struct);
    }

    public List<ExamQuestion> getQuestionList() {
        List<ExamQuestion> list = null;
        List<QuestionDetail> details = getDetailList();
        if (details != null && !details.isEmpty()) {
            list = new LinkedList<>();
            int index = 1;
            QuestionDetail lastDetail = null;
            for (QuestionDetail detail : details) {
                Collections.sort(detail.getQuestions());
                int offset = detail.getIndex() - index;
                for (int i = 0; i < offset; i++) {
                    ExamQuestion q = new ExamQuestion();
                    if (lastDetail != null) {
                        q.setMainNumber(lastDetail.getNumber());
                        q.setMainTitle(lastDetail.getTitle());
                        q.setSubNumber(index - lastDetail.getIndex() + 1);
                    }
                    q.setExamId(getExamId());
                    q.setObjective(isObjective());
                    q.setAnswer("#");
                    q.setTotalScore(0.0);
                    q.setIntervalScore(0.0);
                    list.add(q);
                    index++;
                }
                for (QuestionUnit unit : detail.getQuestions()) {
                    ExamQuestion q = new ExamQuestion();
                    q.setExamId(getExamId());
                    q.setObjective(isObjective());
                    q.setMainNumber(detail.getNumber());
                    q.setMainTitle(detail.getTitle());
                    q.setSubNumber(unit.getNumber());
                    q.setAnswer(unit.getAnswer());
                    q.setTotalScore(unit.getScore());
                    // 根据本题总分是否有小数决定间隔分是1还是0.5
                    q.setIntervalScore(
                            q.getTotalScore() != null && q.getTotalScore() > ((int) q.getTotalScore().doubleValue())
                                    ? 0.5
                                    : 1.0);
                    list.add(q);
                    index++;
                }
                lastDetail = detail;
            }
        }
        return list;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List<QuestionDetail> parseFromJson(String struct) {
        List<QuestionDetail> list = null;
        try {
            JsonConfig config = new JsonConfig();
            Map classMap = new HashMap();
            classMap.put("questions", QuestionUnit.class);
            config.setClassMap(classMap);
            list = JSONArray.toList(JSONArray.fromObject(StringUtils.trimToEmpty(struct)), new QuestionDetail(),
                    config);
            if (list != null) {
                Collections.sort(list);
            }
        } catch (Exception e) {
        }
        return list;
    }

    public static void main(String[] args) {
        ExamStudentPaper paper = new ExamStudentPaper();
        paper.struct = "[{\"title\":\"多选题\",\"number\":2,\"index\":1,\"questions\":[{\"number\":1,\"answer\":\"ACD\",\"score\":2.0},{\"number\":2,\"answer\":\"ABD\",\"score\":2.0},{\"number\":3,\"answer\":\"ABD\",\"score\":2.0},{\"number\":4,\"answer\":\"ABC\",\"score\":2.0},{\"number\":5,\"answer\":\"ABD\",\"score\":2.0}]},{\"title\":\"单选题\",\"number\":1,\"index\":21,\"questions\":[{\"number\":1,\"answer\":\"A\",\"score\":1.0},{\"number\":2,\"answer\":\"A\",\"score\":1.0},{\"number\":3,\"answer\":\"B\",\"score\":1.0},{\"number\":4,\"answer\":\"C\",\"score\":1.0},{\"number\":5,\"answer\":\"A\",\"score\":1.0},{\"number\":6,\"answer\":\"D\",\"score\":1.0},{\"number\":7,\"answer\":\"B\",\"score\":1.0},{\"number\":8,\"answer\":\"D\",\"score\":1.0},{\"number\":9,\"answer\":\"D\",\"score\":1.0},{\"number\":10,\"answer\":\"A\",\"score\":1.0},{\"number\":11,\"answer\":\"C\",\"score\":2.0},{\"number\":12,\"answer\":\"C\",\"score\":2.0},{\"number\":13,\"answer\":\"C\",\"score\":2.0},{\"number\":14,\"answer\":\"C\",\"score\":2.0},{\"number\":15,\"answer\":\"C\",\"score\":2.0}]}]";
        // paper.struct =
        // "[{\"title\":\"填空题\",\"number\":1,\"questions\":[{\"number\":1,\"score\":2.0},{\"number\":2,\"score\":2.0},{\"number\":3,\"score\":2.0},{\"number\":4,\"score\":2.0},{\"number\":5,\"score\":2.0}]},{\"title\":\"问答题\",\"number\":2,\"questions\":[{\"number\":1,\"score\":5.0},{\"number\":2,\"score\":5.0},{\"number\":3,\"score\":5.0},{\"number\":4,\"score\":5.0},{\"number\":5,\"score\":5.0}]},{\"title\":\"套题_1\",\"number\":3,\"questions\":[{\"number\":1,\"score\":2.0},{\"number\":2,\"score\":3.0}]},{\"title\":\"套题_3\",\"number\":4,\"questions\":[{\"number\":1,\"answer\":\"C\",\"score\":2.0},{\"number\":2,\"answer\":\"C\",\"score\":2.0},{\"number\":3,\"answer\":\"C\",\"score\":2.0},{\"number\":4,\"answer\":\"C\",\"score\":2.0},{\"number\":5,\"answer\":\"C\",\"score\":2.0},{\"number\":6,\"score\":2.0},{\"number\":7,\"score\":3.0}]}]";
        List<ExamQuestion> list = paper.getQuestionList();
        for (ExamQuestion q : list) {
            System.out.println(q.getMainTitle() + "," + q.getMainNumber() + "," + q.getSubNumber() + "," + q.getAnswer()
                    + "," + q.getTotalScore());
        }
    }

}
