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

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

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

import org.apache.commons.lang.StringUtils;

@Entity
@Table(name = "eb_mark_step")
public class MarkStep implements Serializable {

    private static final long serialVersionUID = -4181588561648925132L;

    @EmbeddedId
    private MarkStepPK pk;

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

    @Column(name = "has_level")
    private boolean hasLevel;

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

    @Column(name = "max")
    private Double max;

    @Column(name = "min")
    private Double min;

    @Column(name = "default_score")
    private Double defaultScore;

    @Column(name = "score_interval")
    private Double interval;

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

    private String remark;

    @Transient
    private List<MarkLevel> markLevelList;

    public MarkStep() {
        this.pk = new MarkStepPK();
    }

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

    public void setBlockId(Integer blockId) {
        this.pk.setBlockId(blockId);
    }

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

    public void setNumber(Integer number) {
        this.pk.setNumber(number);
    }

    public boolean isHasLevel() {
        return hasLevel;
    }

    public void setHasLevel(boolean hasLevel) {
        this.hasLevel = hasLevel;
    }

    public Double getTotalScore() {
        return totalScore;
    }

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

    public Double getMax() {
        return max;
    }

    public void setMax(Double max) {
        this.max = max;
    }

    public Double getMin() {
        return min;
    }

    public void setMin(Double min) {
        this.min = min;
    }

    public Double getDefaultScore() {
        return defaultScore;
    }

    public void setDefaultScore(Double defaultScore) {
        this.defaultScore = defaultScore;
    }

    public double[] getScoreArray() {
        String[] values = StringUtils.split(StringUtils.trimToEmpty(scoreList), ",");
        if (values != null && values.length > 0) {
            List<Double> list = new ArrayList<Double>(values.length);
            for (String value : values) {
                try {
                    list.add(Double.valueOf(value));
                } catch (Exception e) {
                    continue;
                }
            }
            int length = list.size();
            if (length > 0) {
                double[] array = new double[length];
                for (int i = 0; i < length; i++) {
                    array[i] = list.get(i);
                }
                return array;
            }
        }
        return null;
    }

    public String getScoreList() {
        return scoreList;
    }

    public void setScoreList(String scoreList) {
        this.scoreList = scoreList;
    }

    public void setScoreList(double[] scoreList) {
        if (scoreList != null && scoreList.length > 0) {
            List<String> list = new LinkedList<String>();
            for (double value : scoreList) {
                list.add(String.valueOf(value));
            }
            this.scoreList = StringUtils.join(list, ",");
        }
    }

    public void setScoreList(List<Double> scoreList) {
        if (scoreList != null && scoreList.size() > 0) {
            List<String> list = new LinkedList<String>();
            for (double value : scoreList) {
                list.add(String.valueOf(value));
            }
            this.scoreList = StringUtils.join(list, ",");
        }
    }

    public String getRemark() {
        return remark;
    }

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

    public List<MarkLevel> getMarkLevelList() {
        return markLevelList;
    }

    public void setMarkLevelList(List<MarkLevel> markLevelList) {
        this.markLevelList = markLevelList;
    }

    public Double getInterval() {
        return interval;
    }

    public void setInterval(Double interval) {
        this.interval = interval;
    }

    public String getTitle() {
        return title;
    }

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

}

@Embeddable
class MarkStepPK implements Serializable {

    private static final long serialVersionUID = 4051005509820926166L;

    @Column(name = "block_id")
    private Integer blockId;

    private Integer number;

    public MarkStepPK() {

    }

    @Override
    public int hashCode() {
        final int PRIME = 31;
        int result = 1;
        result = PRIME * result + ((blockId == null) ? 0 : blockId.hashCode());
        result = PRIME * result + ((number == null) ? 0 : number.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final MarkStepPK other = (MarkStepPK) obj;
        if (blockId == null) {
            if (other.blockId != null)
                return false;
        } else if (!blockId.equals(other.blockId))
            return false;
        if (number == null) {
            if (other.number != null)
                return false;
        } else if (!number.equals(other.number))
            return false;
        return true;
    }

    public Integer getBlockId() {
        return blockId;
    }

    public void setBlockId(Integer blockId) {
        this.blockId = blockId;
    }

    public Integer getNumber() {
        return number;
    }

    public void setNumber(Integer number) {
        this.number = number;
    }

}
