package com.qmth.teachcloud.mark.dto.mark;

import com.qmth.teachcloud.mark.entity.MarkSubjectiveScore;

import java.text.DecimalFormat;

public class ScoreItem {

    public static final String SPLIT = ":";

    public static final DecimalFormat NUMBER_FORMAT = new DecimalFormat("####.##");
    // 大题号
    private int mainNumber;
    // 小题号
    private Integer subNumber;
    // 标题名称
    private String title;
    // 小题满分
    private Double totalScore;
    // 小题得分
    private Double score;
    // 答案（客观题有，主观题为null）
    private String answer;
    // 是否客观题
    private boolean objective;

    public ScoreItem(MarkSubjectiveScore ss) {
        this.objective = false;
        this.mainNumber = ss.getMainNumber();
        this.subNumber = ss.getSubNumber();
        this.score = ss.getScore();
    }

    public ScoreItem(boolean objective) {
        this.objective = objective;
    }

    public int getMainNumber() {
        return mainNumber;
    }

    public void setMainNumber(int mainNumber) {
        this.mainNumber = mainNumber;
    }

    public Integer getSubNumber() {
        return subNumber;
    }

    public void setSubNumber(Integer subNumber) {
        this.subNumber = subNumber;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public Double getScore() {
        return score;
    }

    public void setScore(Double score) {
        this.score = score;
    }

    public String getAnswer() {
        return answer;
    }

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public boolean isObjective() {
        return objective;
    }

    public void setObjective(boolean objective) {
        this.objective = objective;
    }

    @Override
    public String toString() {
        return objective ? answer + SPLIT + getScoreString() : getScoreString();
    }

    public static ScoreItem parse(String content, boolean objective) {
        ScoreItem item = new ScoreItem(objective);
        try {
            if (objective) {
                String[] values = content.split(SPLIT);
                item.setAnswer(values[0]);
                item.setScore(Double.valueOf(values[1]));
            } else {
                item.setScore(Double.valueOf(content));
            }
        } catch (Exception e) {
            item = null;
        }
        return item;
    }

    public String getScoreString() {
        try {
            return NUMBER_FORMAT.format(score);
        } catch (Exception e) {
            return "";
        }
    }

    public Double getTotalScore() {
        return totalScore;
    }

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


}
