package com.ssj.service.sys.situation.service.impl;

import com.ssj.service.sys.situation.bo.DimesionCount;
import com.ssj.service.sys.situation.constant.SituationConstant;
import com.ssj.service.sys.situation.service.KnowledgeSituation;
import com.ssj.service.sys.situation.service.LearningSituationService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class AbstractKnowledgeSituation implements KnowledgeSituation {
    protected String studentId;
    protected String courseName;
    /**
     * 知识维度数量列表
     */
    protected List<DimesionCount> knowList = new ArrayList<>();
    /**
     * 技能维度数量列表
     */
    protected List<DimesionCount> skillList = new ArrayList<>();
    /**
     * 理解类错题数量
     */
    protected int understandWrongCount = 0;
    /**
     * 理解类总数量
     */
    protected int understandTotalCount = 0;
    /**
     * 记忆类错题数量
     */
    protected int memoryWrongCount = 0;
    /**
     * 记忆类总数量
     */
    protected int memoryTotalCount = 0;
    /**
     * 应用类错题数量
     */
    protected int applyWrongCount = 0;
    /**
     * 应用类总数量
     */
    protected int applyTotalCount = 0;

    protected LearningSituationService learningSituationService;


    @Override
    public void getCalculate() {
        this.getKnowDimensionCount();
        this.getSkillDimensionCount();
        this.getKnowledgeCount();
    }

    @Override
    public void findKnowDimensionCount(List<Map<String, Object>> wrongList, List<Map<String, Object>> totalList) {
        List<String> knows = SituationConstant.getKnows().get(this.courseName);
        if (knows != null){
            for (String dimension : knows){
                DimesionCount dc = new DimesionCount();
                dc.setName(dimension);
                if (wrongList != null){
                    List<Map<String, Object>> targetList = wrongList.stream().filter((map) -> (dimension.equals(map.get("module") + ""))).collect(Collectors.toList());
                    if (targetList != null && targetList.size() > 0){
                        Object countObj = targetList.get(0).get("count");
                        int count = 0;
                        if (countObj != null) {
                            count = Integer.parseInt(countObj.toString());
                        }
                        dc.setWrongCount(count);
                    }
                    targetList = totalList.stream().filter((map) -> (dimension.equals(map.get("module") + ""))).collect(Collectors.toList());
                    if (targetList != null && targetList.size() > 0){
                        Object countObj = targetList.get(0).get("count");
                        int count = 0;
                        if (countObj != null) {
                            count = Integer.parseInt(countObj.toString());
                        }
                        dc.setTotalCount(count);
                    }
                }
                this.knowList.add(dc);
            }
        }
    }

    @Override
    public void findSkillDimensionCount(Map<String, Integer> wrongMap, Map<String, Integer> totalMap) {
        List<String> skills = SituationConstant.getSkills().get(this.courseName);
        int wrongTotal = 0;
        if (skills != null) {
            for (String dimension : skills){
                DimesionCount dc = new DimesionCount();
                dc.setName(dimension);
                if (wrongMap != null){
                    if (wrongMap.get(dimension) != null){
                        dc.setWrongCount(wrongMap.get(dimension).intValue());
                        wrongTotal += wrongMap.get(dimension).intValue();
                    }
                    if (totalMap.get(dimension) != null){
                        dc.setTotalCount(totalMap.get(dimension).intValue());
                    }
                }
                this.skillList.add(dc);
            }

            //给所有维度设置总错题量
            for (DimesionCount dc : this.skillList){
                dc.setTotalCount(wrongTotal);
            }
        }
    }
}
