package com.ruoyi.web.service;

import com.google.common.collect.Lists;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mxjb.domain.MxjbContants;
import com.ruoyi.sy.domain.SyTestAnswers;
import com.ruoyi.sy.domain.SyTestQuestion;
import com.ruoyi.web.domain.PaperDto;
import com.ruoyi.web.domain.QuestionDto;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

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

@Service
public class ExamUtilService {
    private final RedisCache redisCache;
    private final CacheService cacheService;

    public ExamUtilService(RedisCache redisCache, CacheService cacheService) {
        this.redisCache = redisCache;
        this.cacheService = cacheService;
    }

    public String getExamineeAnswersKey(String type, Long examineeId) {
        return type + "EA::" + examineeId;
    }

    public void updateTempAnswerAndSave(PaperDto paperDto, Long examineeId, String examineeType) {
        String type = MxjbContants.getExamineeTypeKey(examineeType);
        String key = getExamineeAnswersKey(type, examineeId);
        Map<String, QuestionDto> questionDtoMap = redisCache.getCacheMap(key);
        if (MapUtils.isNotEmpty(questionDtoMap)) {
            Long lastQuestionId = questionDtoMap.get("0").getQuestionId();
            paperDto.getQuestions().forEach(t -> {
                QuestionDto dto = questionDtoMap.get(t.getQuestionId().toString());
                if (null != dto) {
                    t.setAnswer(dto.getAnswer());
                    t.setAttachments(dto.getAttachments());
                    t.setDuration(dto.getDuration());
                    t.setCurrent(t.getQuestionId().equals(lastQuestionId));
                }
            });
        } else if (CollectionUtils.isNotEmpty(paperDto.getQuestions())) {
            paperDto.getQuestions().get(0).setCurrent(true);
        }
    }

    public QuestionDto toQuestionDto(SyTestQuestion t, boolean fillParseAndAnswer, Map<Long, SyTestAnswers> questionAnswersMap) {
        QuestionDto dto = new QuestionDto();
        dto.setQuestionId(t.getQuestionId());
        dto.setSeq(t.getSeq());
        dto.setTypeId(cacheService.getQuestionType(t.getQtpye()));
        dto.setType(t.getQtpye());
        dto.setTitle(t.getTitle());
        dto.setScoreTotal(t.getScore());
        dto.setOptions(getOptions(t.getOptionA(), t.getOptionB(), t.getOptionC(), t.getOptionD(), t.getOptionE()));
        if (fillParseAndAnswer) {
            dto.setParse(t.getParse());
        }
        if (null != questionAnswersMap) {
            SyTestAnswers answers = questionAnswersMap.get(t.getQuestionId());
            if (null != answers) {
                dto.setDuration(answers.getDuration());
                setAnswerAttachemnts(dto, answers.getAnswer());
            } else {
                dto.setAnswer(StringUtils.EMPTY);
                dto.setAttachments(new ArrayList<>());
            }
        } else {
            dto.setAnswer(StringUtils.EMPTY);
            dto.setAttachments(new ArrayList<>());
        }

        dto.setSource(t.getQuesitonCateogry());
        return dto;
    }

    public List<String> getOptions(String... options) {
        List<String> optionList = Lists.newArrayList();
        for (String option : options) {
            if (StringUtils.isNotBlank(option)) {
                optionList.add(option);
            }
        }
        return optionList;
    }

    public void setAnswerAttachemnts(QuestionDto dto, String answer) {
        if (StringUtils.isNotBlank(answer)) {
            String[] answerArray = StringUtils.splitByWholeSeparatorPreserveAllTokens(answer, "$@");
            if (answerArray.length > 0) {
                dto.setAnswer(answerArray[0]);
                if (answerArray.length > 1) {
                    List<String> attacheList = Lists.newArrayList();
                    for (int i = 1; i < answerArray.length; i++) {
                        if (StringUtils.isNotBlank(answerArray[i]) && !"null".equals(answerArray[i])) {
                            attacheList.add(answerArray[i]);
                        }
                    }
                    dto.setAttachments(attacheList);
                }
            }
        }
    }
}
