package com.qxgmat.service.extend;

import com.alibaba.fastjson.JSONObject;
import com.qxgmat.data.constants.enums.SettingKey;
import com.qxgmat.data.dao.entity.QuestionNo;
import com.qxgmat.data.dao.entity.Setting;
import com.qxgmat.data.dao.entity.UserQuestion;
import com.qxgmat.data.relation.entity.QuestionNoRelation;
import com.qxgmat.service.inline.SettingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ToolsService {

    @Resource
    private SettingService settingService;

    /**
     * 根据练习时间设置计算考题考试时间
     *      setting: {struct: {difficult: ""}}
     * @param relationList
     * @return
     */
    public Integer computerTime(List<QuestionNoRelation> relationList){
        Setting setting = settingService.getByKey(SettingKey.EXERCISE_TIME);
        JSONObject value = setting.getValue();

        Integer time = 0;
        for(QuestionNoRelation relation:relationList){
            JSONObject difficultMap = null;
            for (Integer struct : relation.getModuleStruct()){
                // 判断结构体
                difficultMap = value.getJSONObject(String.valueOf(struct));
                if (difficultMap != null) break;
            }
            if (difficultMap == null) continue;
            // 判断难度
            String t = difficultMap.getString(relation.getQuestion().getDifficult());
            if (t == null || t.isEmpty()) continue;
            time += Integer.valueOf(t);
        }
        return time;
    }

    /**
     * 根据练习时间设置获取单个考题时间
     *      setting: {struct: {difficult: ""}}
     * @param relation
     * @return
     */
    public Integer computerTime(QuestionNoRelation relation){
        Setting setting = settingService.getByKey(SettingKey.EXERCISE_TIME);
        JSONObject value = setting.getValue();

        JSONObject difficultMap = null;
        for (Integer struct : relation.getModuleStruct()){
            // 判断结构体
            difficultMap = value.getJSONObject(String.valueOf(struct));
            if (difficultMap != null) break;
        }
        if (difficultMap == null) return 0;
        // 判断难度
        String t = difficultMap.getString(relation.getQuestion().getDifficult());
        if (t == null || t.isEmpty()) return 0;
        return Integer.valueOf(t);
    }

    /**
     * 根据后台设置剔除时间，判断试题统计情况
     * @param question
     * @return
     */
    public boolean filterTime(UserQuestion question){
        Setting setting = settingService.getByKey(SettingKey.FILTER_TIME);
        JSONObject value = setting.getValue();
        if (value.getInteger("min")> question.getTime()) return false;
        if (value.getInteger("max") < question.getTime()) return false;
        return  true;
    }
}
