package com.ruoyi.web.service;

import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mxjb.domain.MxjbContants;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.stereotype.Service;

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

@Service
public class CacheService {
    private final ISysConfigService sysConfigService;
    private final ISysDictDataService sysDictDataService;
    private final ISysUserService sysUserService;

    private final String zyLocationsKey = "zy.locations";
    private final String chooseQuestionTypesKey = "question.choose.types";
    private final String multiQuestionTypesKey = "question.multi.types";
    public static final Set<String> MultiQuestionTypeSet = Sets.newHashSet("多选题", "不定项选择题");
    public static final Set<String> ChooseQuestionTypeSet = Sets.newHashSet("单选题", "选择题", "单项选择", "判断题","单项选择题");
    public static final Set<String> ChooseItemQuestionTypeSet = Sets.newHashSet("单选题", "选择题", "单项选择", "单项选择题");
    public static final Set<String> ChooseRightQuestionTypeSet = Sets.newHashSet("判断题");
    public static Map<String, String> quesitonTypeMap;

    public CacheService(ISysConfigService sysConfigService, ISysDictDataService sysDictDataService, ISysUserService sysUserService) {
        this.sysConfigService = sysConfigService;
        this.sysDictDataService = sysDictDataService;
        this.sysUserService = sysUserService;

        quesitonTypeMap = Maps.newHashMap();
        quesitonTypeMap.put("复选总分", MxjbContants.QuestionTypeMultiTotal); // 4
        quesitonTypeMap.put("复选评价", MxjbContants.QuestionTypeMultiScore); // 5
        quesitonTypeMap.put("单选计数", MxjbContants.QuestionTypeChooseCount); // 6
        quesitonTypeMap.put("单选分值", MxjbContants.QuestionTypeChooseScore); // 7
    }

    public String getQuestionType(String qtype) {
        String type = quesitonTypeMap.get(qtype);
        if (null != type) {
            return type;
        }
        if (selectChooseQuestionTypes().contains(qtype)) {
            return MxjbContants.QuestionTypeChoose;
        }
        return selectMultiQuestionTypes().contains(qtype) ? MxjbContants.QuestionTypeMultiChoose : MxjbContants.QuestionTypeSubjective;
    }
    public Set<String > selectMultiQuestionTypes () {
        String multiTypes = sysConfigService.selectConfigByKey(multiQuestionTypesKey);
        if(StringUtils.isNotBlank(multiTypes)) {
            return Sets.newHashSet(StringUtils.split(multiTypes, ","));
        }
        return MultiQuestionTypeSet;
    }

    public Set<String > selectChooseQuestionTypes () {
        String chooseTypes = sysConfigService.selectConfigByKey(chooseQuestionTypesKey);
        if(StringUtils.isNotBlank(chooseTypes)) {
            return Sets.newHashSet(StringUtils.split(chooseTypes, ","));
        }
        return ChooseQuestionTypeSet;
    }

    public Set<String > getZyLocations() {
        String locations = sysConfigService.selectConfigByKey(zyLocationsKey);
        if(StringUtils.isNotBlank(locations)) {
            return Sets.newHashSet(StringUtils.split(locations, ","));
        }
        return Sets.newHashSet();
    }

    public Map<String,String> selectDictDataMapByType(String dictType){
        List<SysDictData> dictDatas = sysDictDataService.selectDictDataByType(dictType);
        Map<String, String> dictMap = new HashMap<>();
        if(org.apache.commons.collections.CollectionUtils.isNotEmpty(dictDatas)) {
            for(SysDictData data : dictDatas) {
                dictMap.put(data.getDictValue(), data.getDictLabel());
            }
        }
        return dictMap;
    }

    public String getValue(String value, String defValue) {
        return StringUtils.isNotBlank(value) ? value : defValue;
    }
}
