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

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

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.exam.model.Marker;

/**
 * 标准评卷结果对象
 * 
 * @author luoshi
 *
 */
public class MarkResult {

    public static final String SPLIT = ",";

    /**
     * 评卷状态
     */
    private String statusValue;

    /**
     * 考生编号
     */
    private Integer studentId;

    /**
     * 评卷任务编号
     */
    private Integer libraryId;

    /**
     * 总分
     */
    private double markerScore;

    /**
     * 分值列表
     */
    private Double[] scoreList;

    /**
     * 阅卷轨迹列表
     */
    private TrackDTO[] trackList;

    /**
     * 特殊标记列表
     */
    private SpecialTagDTO[] specialTagList;

    /**
     * 所花时间
     */
    private int spent;

    /**
     * 是否问题卷
     */
    private boolean isProblem;

    /**
     * 问题类型
     */
    private Integer problemTypeId;

    private boolean unselective;

    public String getStatusValue() {
        return statusValue;
    }

    public void setStatusValue(String statusValue) {
        this.statusValue = statusValue;
    }

    public Integer getLibraryId() {
        return libraryId;
    }

    public void setLibraryId(Integer libraryId) {
        this.libraryId = libraryId;
    }

    public double getMarkerScore() {
        return markerScore;
    }

    public void setMarkerScore(double markerScore) {
        this.markerScore = markerScore;
    }

    public String getScoreList() {
        List<Object> list = new ArrayList<Object>();
        for (int i = 0; i < scoreList.length; i++) {
            Double d = scoreList[i];
            if (d % 1 == 0) {
                list.add(d.intValue());
            } else {
                list.add(d);
            }
        }
        return StringUtils.join(list, SPLIT);
    }

    public void setScoreList(Double[] scoreList) {
        this.scoreList = scoreList;
    }

    public TrackDTO[] getTrackList() {
        return trackList;
    }

    public void setTrackList(TrackDTO[] trackList) {
        this.trackList = trackList;
    }

    public SpecialTagDTO[] getSpecialTagList() {
        return specialTagList;
    }

    public void setSpecialTagList(SpecialTagDTO[] specialTagList) {
        this.specialTagList = specialTagList;
    }

    public int getSpent() {
        return spent;
    }

    public void setSpent(int spent) {
        this.spent = spent;
    }

    public Integer getStudentId() {
        return studentId;
    }

    public void setStudentId(Integer studentId) {
        this.studentId = studentId;
    }

    public Map<String, List<TrackDTO>> getTrackMap() {
        Map<String, List<TrackDTO>> map = new HashMap<String, List<TrackDTO>>();
        if (trackList != null) {
            for (TrackDTO dto : trackList) {
                String questionNumber = dto.getMainNumber() + "." + dto.getSubNumber();
                List<TrackDTO> list = map.get(questionNumber);
                if (list == null) {
                    list = new LinkedList<TrackDTO>();
                    map.put(questionNumber, list);
                }
                list.add(dto);
            }
        }
        return map;
    }

    public Map<String, List<MarkTrack>> getTrackMap(MarkLibrary library, Marker marker) {
        Map<String, List<MarkTrack>> map = new HashMap<>();
        if (trackList != null) {
            for (TrackDTO dto : trackList) {
                String questionNumber = dto.getMainNumber() + "." + dto.getSubNumber();
                List<MarkTrack> list = map.get(questionNumber);
                if (list == null) {
                    list = new LinkedList<MarkTrack>();
                    map.put(questionNumber, list);
                }
                list.add(dto.transform(library, marker));
            }
        }
        return map;
    }

    public List<HeaderTrack> getTrackList(ArbitrateHistory library) {
        List<HeaderTrack> list = new LinkedList<>();
        if (trackList != null) {
            for (TrackDTO dto : trackList) {
                list.add(dto.transform(library));
            }
        }
        return list;
    }

    public List<MarkTrack> getTrackList(MarkLibrary library, Marker marker) {
        List<MarkTrack> list = new LinkedList<>();
        if (trackList != null) {
            for (TrackDTO dto : trackList) {
                list.add(dto.transform(library, marker));
            }
        }
        return list;
    }

    public List<TrialTrack> getTrackList(TrialLibrary library) {
        int unansweredCount = 0;
        List<TrialTrack> list = new LinkedList<>();
        if (trackList != null) {
            for (TrackDTO dto : trackList) {
                list.add(dto.transform(library));
                if (dto.isUnanswered()) {
                    unansweredCount++;
                }
            }
        }
        library.setUnansweredCount(unansweredCount);
        return list;
    }

    public List<HeaderTag> getHeaderTagList(ArbitrateHistory library) {
        List<HeaderTag> list = new LinkedList<>();
        if (specialTagList != null) {
            for (SpecialTagDTO dto : specialTagList) {
                list.add(dto.transform(library));
            }
        }
        return list;
    }

    public List<MarkSpecialTag> getSpecialTagList(MarkLibrary library, Marker marker) {
        List<MarkSpecialTag> list = new LinkedList<>();
        if (specialTagList != null) {
            for (SpecialTagDTO dto : specialTagList) {
                list.add(dto.transform(library));
            }
        }
        return list;
    }

    public List<TrialTag> getTagList(TrialLibrary library) {
        List<TrialTag> list = new LinkedList<>();
        if (specialTagList != null) {
            for (SpecialTagDTO dto : specialTagList) {
                list.add(dto.transform(library));
            }
        }
        return list;
    }

    public boolean isProblem() {
        return isProblem;
    }

    public void setProblem(boolean isProblem) {
        this.isProblem = isProblem;
    }

    public Integer getProblemTypeId() {
        return problemTypeId;
    }

    public void setProblemTypeId(Integer problemTypeId) {
        this.problemTypeId = problemTypeId;
    }

    public boolean isUnselective() {
        return unselective;
    }

    public void setUnselective(boolean unselective) {
        this.unselective = unselective;
    }

}
