package cn.com.qmth.stmms.ms.admin.assembler;

import cn.com.qmth.stmms.ms.admin.dto.SubjectOverview;
import cn.com.qmth.stmms.ms.admin.dto.WorkOverview;
import cn.com.qmth.stmms.ms.core.domain.Student;
import cn.com.qmth.stmms.ms.core.domain.Work;
import cn.com.qmth.stmms.ms.core.domain.enums.TrialEnum;
import cn.com.qmth.stmms.ms.core.domain.user.Role;
import cn.com.qmth.stmms.ms.core.repository.ExamQuestionRepo;
import cn.com.qmth.stmms.ms.core.repository.MarkUserRepo;
import cn.com.qmth.stmms.ms.core.repository.PaperRepo;
import cn.com.qmth.stmms.ms.core.repository.StudentRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * Created by zhengmin on 2016/11/21.
 */
@Component
public class WorkOverviewAssembler {

    @Autowired
    private StudentRepo studentRepo;

    @Autowired
    private ExamQuestionRepo examQuestionRepo;

    @Autowired
    private MarkUserRepo markUserRepo;

    @Autowired
    private PaperRepo paperRepo;

    public WorkOverview toDTO(Work work) {
        WorkOverview workOverview = null;
        if (work != null) {
            workOverview = new WorkOverview();
            workOverview.setWorkName(work.getName());
            workOverview.setPaperCount(paperRepo.countByWorkIdAndTest(work.getId(), TrialEnum.DEFAULT.getId()));
            workOverview.setMarkerCount(markUserRepo.countByWorkIdAndRole(work.getId(), Role.MARKER));
            workOverview.setQuestionCount(examQuestionRepo.countByWorkIdAndTest(work.getId(), TrialEnum.DEFAULT.getId()));

            List<Student> students = studentRepo.findByWorkIdAndTest(work.getId(), String.valueOf(TrialEnum.DEFAULT.getId()));
            workOverview.setStuTotalCount(students.stream().count());
            workOverview.setStuAbsentCount(students.stream().filter(s -> s.isAbsent()).count());
            workOverview.setStuUploadedCount(workOverview.getStuTotalCount() - workOverview.getStuAbsentCount());

            ////// subject overview assemble

            List<SubjectOverview> subjectOverviews = new ArrayList<>();
            work.getSubjects().forEach(s -> {
                SubjectOverview so = new SubjectOverview();
                so.setWorkId(s.getWorkId());
                so.setSubjectId(s.getId());
                so.setSubjectName(s.getName());
                so.setSubject(s.getSubject().toString());
                so.setMarkStage(s.getStage());
                subjectOverviews.add(so);
            });
            Map<String, Long> subjectMap = new HashMap<>();
            for (SubjectOverview so : subjectOverviews) {
                subjectMap.put(so.getSubject(), 0L);
            }
            for (Student student : students) {
                String[] uploadStatus = student.getUploadStatus().split(",");
                for (String s : uploadStatus) {
                    if ("1".equals(s.split(":")[1])) {
                        if(!subjectMap.containsKey(s.split(":")[0])){
                            break;
                        }
                        long c = subjectMap.get(s.split(":")[0]);
                        subjectMap.put(s.split(":")[0], ++c);
                    }
                }
            }
            for (SubjectOverview so : subjectOverviews) {
                so.setUploadedCount(subjectMap.get(so.getSubject()));
                so.setLeftCount(workOverview.getStuTotalCount() - workOverview.getStuAbsentCount() - so.getUploadedCount());
            }
            workOverview.setSubjectOverviews(subjectOverviews);
        }
        return workOverview;
    }
}
