package cn.com.qmth.stmms.biz.report.utils.module;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.qmth.stmms.biz.exam.dao.ExamStudentDao;
import cn.com.qmth.stmms.biz.exam.model.ExamStudent;
import cn.com.qmth.stmms.biz.report.model.ReportSubject;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectRangeService;
import cn.com.qmth.stmms.biz.report.service.ReportSubjectService;
import cn.com.qmth.stmms.biz.report.utils.Module;
import cn.com.qmth.stmms.biz.report.utils.ReportContext;
import cn.com.qmth.stmms.biz.report.utils.unit.RangeCounter;
import cn.com.qmth.stmms.biz.utils.SpringContextHolder;
import net.sf.json.JSONObject;

public class SubjectRangeModule implements Module {

    private Map<String, RangeCounter> counters;

    private ReportContext context;

    public SubjectRangeModule(ReportContext context) {
        this.counters = new HashMap<String, RangeCounter>();
        this.context = context;
    }

    @Override
    public void process(ExamStudent student) {
        if (student.isUpload() && !student.isAbsent() && !student.isBreach() && student.getSubject() != null) {
            findCounter(getKey(student)).process(student.getTotalScore());
        }
    }

    public String getKey(ExamStudent student) {
        return student.getSubjectCode();
    }

    @Override
    public void save() {
        ReportSubjectRangeService service = SpringContextHolder.getBean(ReportSubjectRangeService.class);
        for (String subjectCode : this.counters.keySet()) {
            RangeCounter counter = counters.get(subjectCode);
            // ReportSubjectRange subjectRange = new ReportSubjectRange();
            // subjectRange.setExamId(this.context.getExamId());
            // subjectRange.setSubjectCode(subjectCode);
            // subjectRange.setSubjectName(context.getSubject(subjectCode).getName());
            // service.save(subjectRange);
            ReportSubjectService subjectService = SpringContextHolder.getBean(ReportSubjectService.class);
            ExamStudentDao studentDao = SpringContextHolder.getBean(ExamStudentDao.class);
            ReportSubject subject = subjectService.findOne(this.context.getExamId(), subjectCode);
            JSONObject rangeLevel = new JSONObject();
            double highCount = Math.ceil(context.getHighValueConfig() * subject.getRealityCount() * 0.01);
            double lowCount = Math.ceil(context.getLowValueConfig() * subject.getRealityCount() * 0.01);
            List<BigDecimal> highScore = studentDao.findHighCountTotalSocreByExamIdAndSubjectCode(context.getExamId(),
                    subjectCode, (int) highCount);
            List<BigDecimal> lowScore = studentDao.findLowCountTotalSocreByExamIdAndSubjectCode(context.getExamId(),
                    subjectCode, (int) lowCount);
            rangeLevel.accumulate("highScore",
                    highScore.isEmpty() ? 0 : highScore.get(highScore.size() - 1).doubleValue());
            rangeLevel.accumulate("lowScore", lowScore.isEmpty() ? 0 : lowScore.get(lowScore.size() - 1).doubleValue());
            subject.setRangeLevel(rangeLevel.toString());

            JSONObject scoreRange = new JSONObject();
            for (int i = 0; i < subject.getTotalScore() + 1; i++) {
                scoreRange.accumulate(String.valueOf(i), counter.countGeAndLt((double) i, (double) (i + 1)));
            }
            subject.setScoreRange(scoreRange.toString());
            subjectService.save(subject);
        }

    }

    private RangeCounter findCounter(String key) {
        RangeCounter counter = counters.get(key);
        if (counter == null) {
            counter = new RangeCounter();
            counter.interval = context.getRangeInterval();
            counters.put(key, counter);
        }
        return counter;
    }

}
