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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import cn.com.qmth.stmms.biz.exam.model.ExamQuestion;
import cn.com.qmth.stmms.biz.exam.model.ExamSubject;

public class PaperJsonUtils {

    public static final String COURSE_CODE = "courseCode";

    public static final String COURSE_NAME = "courseName";

    public static final String ID = "id";

    public static final String[] answerArray = new String[] { "#", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
            "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

    public static Map<Boolean, List<ExamQuestion>> toSubject(ExamSubject subject, String text) {
        JSONObject paperJson = JSONObject.fromObject(text);
        JSONArray questionArray = JSONArray.fromObject(paperJson.getString("details"));
        Map<Boolean, List<ExamQuestion>> map = new HashMap<Boolean, List<ExamQuestion>>();
        List<ExamQuestion> olist = new ArrayList<>();
        List<ExamQuestion> slist = new ArrayList<>();
        for (int i = 0; i < questionArray.size(); i++) {
            JSONObject questionJson = questionArray.getJSONObject(i);
            int mainNumber = questionJson.getInt("number");
            String mainTitle = questionJson.getString("name");
            JSONArray qArray = questionJson.getJSONArray("questions");
            for (int j = 0; j < qArray.size(); j++) {
                JSONObject sub = qArray.getJSONObject(j);
                if (sub.get("subQuestions") != null) {
                    JSONArray subJson = sub.getJSONArray("subQuestions");
                    for (int k = 0; k < subJson.size(); k++) {
                        JSONObject question = subJson.getJSONObject(k);
                        ExamQuestion q = new ExamQuestion();
                        q.setExamId(subject.getExamId());
                        q.setSubjectCode(subject.getCode());
                        q.setMainNumber(mainNumber);
                        String subNumber = sub.getString("number") + "-" + question.getString("number");
                        q.setSubNumber(subNumber);
                        q.setMainTitle(mainTitle);
                        double score = question.getDouble("score");
                        q.setTotalScore(score);
                        q.setIntervalScore(0.5);
                        q.setPaperType("#");
                        int structType = question.getInt("structType");
                        if (structType == 1 || structType == 2 || structType == 3) {
                            q.setObjective(true);
                            String answer = "";
                            if (structType == 3) {
                                answer = question.getBoolean("answer") ? "A" : "B";
                            } else {
                                Object listArray = question.get("answer");
                                if (listArray instanceof JSONArray) {
                                    JSONArray jsonArray = (JSONArray) listArray;
                                    for (int l = 0; l < jsonArray.size(); l++) {
                                        int index = jsonArray.getInt(l);
                                        answer = answer + answerArray[index];
                                    }
                                } else if (listArray instanceof JSONObject) {
                                    int index = question.getInt("answer");
                                    answer = answerArray[index];
                                }
                            }
                            q.setAnswer(answer);
                            olist.add(q);
                        } else {
                            q.setObjective(false);
                            slist.add(q);
                        }
                    }
                } else {
                    ExamQuestion q = new ExamQuestion();
                    q.setExamId(subject.getExamId());
                    q.setSubjectCode(subject.getCode());
                    q.setMainNumber(mainNumber);
                    String subNumber = sub.getString("number");
                    q.setSubNumber(subNumber);
                    q.setMainTitle(mainTitle);
                    double score = sub.getDouble("score");
                    q.setTotalScore(score);
                    q.setIntervalScore(0.5);
                    q.setPaperType("#");
                    int structType = sub.getInt("structType");
                    if (structType == 1 || structType == 2 || structType == 3) {
                        q.setObjective(true);
                        String answer = "";
                        if (structType == 3) {
                            answer = sub.getBoolean("answer") ? "A" : "B";
                        } else {
                            Object listArray = sub.get("answer");
                            if (listArray instanceof JSONArray) {
                                JSONArray jsonArray = (JSONArray) listArray;
                                for (int l = 0; l < jsonArray.size(); l++) {
                                    int index = jsonArray.getInt(l);
                                    answer = answer + answerArray[index];
                                }
                            } else if (listArray instanceof JSONObject) {
                                int index = sub.getInt("answer");
                                answer = answerArray[index];
                            }
                        }
                        q.setAnswer(answer);
                        olist.add(q);
                    } else {
                        q.setObjective(false);
                        slist.add(q);
                    }
                }
            }
        }
        map.put(Boolean.TRUE, olist);
        map.put(Boolean.FALSE, slist);
        return map;
    }

}
