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

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import cn.com.qmth.stmms.biz.file.enums.FormatType;
import cn.com.qmth.stmms.biz.file.service.FileService;
import cn.com.qmth.stmms.biz.mark.model.PictureConfigItem;

@Entity
@Table(name = "eb_exam_subject")
public class ExamSubject implements Serializable {

    private static final long serialVersionUID = -6942849844435873366L;

    @EmbeddedId
    private ExamSubjectPK pk;

    @Column(name = "name")
    private String name;

    @Column(name = "level")
    private String level;

    @Column(name = "category")
    private String category;

    @Column(name = "objective_score")
    private Double objectiveScore;

    @Column(name = "subjective_score")
    private Double subjectiveScore;

    @Column(name = "total_score")
    private Double totalScore;

    /**
     * 上传学生数量
     */
    @Column(name = "upload_count")
    private Integer uploadCount;

    @Column(name = "remark")
    private String remark;

    /**
     * 裁切图配置
     */
    @Column(name = "slice_config", nullable = true)
    private String sliceConfig;

    /**
     * 原图遮盖配置
     */
    @Column(name = "sheet_config", nullable = true)
    private String sheetConfig;

    /**
     * 题卡类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "card_type", length = 16, nullable = true)
    private FormatType cardType;

    /**
     * 试卷文件类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "paper_file_type", length = 16, nullable = true)
    private FormatType paperFileType;

    /**
     * 标答文件类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "answer_file_type", length = 16, nullable = true)
    private FormatType answerFileType;

    /**
     * 统计配置
     */
    @Column(name = "sas_config", nullable = true)
    private String sasConfig;

    /**
     * 试评数量
     */
    @Column(name = "trial_count", nullable = true)
    private Integer trialCount;

    /**
     * 自动跳转
     */
    @Column(name = "auto_scroll", nullable = true)
    private Boolean autoScroll;

    /**
     * 是否裁切
     */
    @Column(name = "enable_split", nullable = true)
    private Boolean enableSplit;

    /**
     * 显示题目昵称
     */
    @Column(name = "display_question_name", nullable = false)
    private Boolean displayQuestionName;
    
    /**
     * 复核轮数
     */
    @Column(name = "inspect_round", nullable = false)
    private Integer inspectRound;

    /**
     * 是否含选做题
     */
    @Column(name = "selective")
    private boolean selective;

    /**
     * 大题数量
     */
    @Transient
    private long groupCount;

    @Transient
    private String paperUrl;

    @Transient
    private String answerUrl;

    @Transient
    private String cardUrl;

    @Transient
    private String paperType;

    @Transient
    private boolean locked;

    public ExamSubject() {
        this.pk = new ExamSubjectPK();
    }

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

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

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

    public void setCode(String code) {
        pk.setCode(code);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getObjectiveScore() {
        return objectiveScore;
    }

    public void setObjectiveScore(Double objectiveScore) {
        this.objectiveScore = objectiveScore;
    }

    public Double getSubjectiveScore() {
        return subjectiveScore;
    }

    public void setSubjectiveScore(Double subjectiveScore) {
        this.subjectiveScore = subjectiveScore;
    }

    public void setScore(double score, boolean objective) {
        if (objective) {
            setObjectiveScore(score);
        } else {
            setSubjectiveScore(score);
        }
    }

    public Double getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(Double totalScore) {
        this.totalScore = totalScore;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getDisplayName() {
        StringBuilder sb = new StringBuilder();
        sb.append(name);
        if (StringUtils.isNotBlank(remark)) {
            sb.append("_").append(remark);
        }
        return sb.toString();
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public Integer getUploadCount() {
        return uploadCount;
    }

    public void setUploadCount(Integer uploadCount) {
        this.uploadCount = uploadCount;
    }

    public long getGroupCount() {
        return groupCount;
    }

    public void setGroupCount(long groupCount) {
        this.groupCount = groupCount;
    }

    public String getSliceConfig() {
        return sliceConfig;
    }

    public void setSliceConfig(String sliceConfig) {
        this.sliceConfig = sliceConfig;
    }

    public List<PictureConfigItem> getSliceConfigList() {
        return PictureConfigItem.parse(sliceConfig);
    }

    public String getSheetConfig() {
        return sheetConfig;
    }

    public void setSheetConfig(String sheetConfig) {
        this.sheetConfig = sheetConfig;
    }

    public List<PictureConfigItem> getSheetConfigList() {
        return PictureConfigItem.parse(sheetConfig);
    }

    public FormatType getCardType() {
        return cardType;
    }

    public void setCardType(FormatType cardType) {
        this.cardType = cardType;
    }

    public FormatType getPaperFileType() {
        return paperFileType;
    }

    public void setPaperFileType(FormatType paperFileType) {
        this.paperFileType = paperFileType;
    }

    public FormatType getAnswerFileType() {
        return answerFileType;
    }

    public void setAnswerFileType(FormatType answerFileType) {
        this.answerFileType = answerFileType;
    }

    public String getPaperUrl() {
        return paperUrl;
    }

    public void setPaperUrl(String paperUrl) {
        this.paperUrl = paperUrl;
    }

    public String getAnswerUrl() {
        return answerUrl;
    }

    public void setAnswerUrl(String answerUrl) {
        this.answerUrl = answerUrl;
    }

    public String getCardUrl() {
        return cardUrl;
    }

    public void setCardUrl(String cardUrl) {
        this.cardUrl = cardUrl;
    }

    public void setPaperAnswerUrl(FileService fileService) {
        if (paperFileType != null) {
            paperUrl = fileService.getPaperUri(getExamId(), getCode(), paperFileType);
        }
        if (answerFileType != null) {
            answerUrl = fileService.getAnswerUri(getExamId(), getCode(), answerFileType);
        }
        if (cardType != null) {
            cardUrl = fileService.getCardUri(getExamId(), getCode(), cardType);
        }
    }

    public String getSasConfig() {
        return sasConfig;
    }

    public void setSasConfig(String sasConfig) {
        this.sasConfig = sasConfig;
    }

    public Double getPassScore() {
        String text = StringUtils.trimToEmpty(sasConfig);
        if (StringUtils.isNotBlank(text)) {
            JSONObject jsonObject = JSONObject.fromObject(text);
            Double passScore = jsonObject.getDouble("passScore");
            return passScore;
        }
        return null;
    }

    public Double getExcellentScore() {
        String text = StringUtils.trimToEmpty(sasConfig);
        if (StringUtils.isNotBlank(text)) {
            JSONObject jsonObject = JSONObject.fromObject(text);
            Double excellentScore = jsonObject.getDouble("excellentScore");
            return excellentScore;
        }
        return null;
    }

    public boolean isLocked() {
        return locked;
    }

    public void setLocked(boolean locked) {
        this.locked = locked;
    }

    public String getPaperType() {
        return paperType;
    }

    public void setPaperType(String paperType) {
        this.paperType = paperType;
    }

    public Integer getTrialCount() {
        return trialCount;
    }

    public void setTrialCount(Integer trialCount) {
        this.trialCount = trialCount;
    }

    public Boolean getAutoScroll() {
        return autoScroll;
    }

    public void setAutoScroll(Boolean autoScroll) {
        this.autoScroll = autoScroll;
    }

    public Boolean getEnableSplit() {
        return enableSplit;
    }

    public void setEnableSplit(Boolean enableSplit) {
        this.enableSplit = enableSplit;
    }

    public Boolean getDisplayQuestionName() {
        return displayQuestionName;
    }

    public void setDisplayQuestionName(Boolean displayQuestionName) {
        this.displayQuestionName = displayQuestionName;
    }

    public Integer getInspectRound() {
        return inspectRound;
    }

    public void setInspectRound(Integer inspectRound) {
        this.inspectRound = inspectRound;
    }

    public boolean isSelective() {
        return selective;
    }

    public void setSelective(boolean selective) {
        this.selective = selective;
    }

}
