package com.ruoyi.test.seriver.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.test.domain.Answer;
import com.ruoyi.test.domain.DO.QuestionAnswerDO;
import com.ruoyi.test.domain.ExamCreate;
import com.ruoyi.test.domain.Questionanswer;
import com.ruoyi.test.domain.Questionbank;
import com.ruoyi.test.domain.Vo.AnswerVo;
import com.ruoyi.test.domain.Vo.QuestionanswerVo;
import com.ruoyi.test.mapper.QuestionanswerMapper;
import com.ruoyi.test.seriver.IQuestionTypeService;
import com.ruoyi.test.seriver.IQuestionanswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Type;
import java.util.*;

@Service
public class QuestionanswerServiceImpl extends ServiceImpl<QuestionanswerMapper, Questionanswer> implements IQuestionanswerService {
    @Autowired
    private QuestionanswerMapper questionanswerMapper;

    @Autowired
    private IQuestionTypeService iQuestionTypeService;

    @Override
    public QuestionAnswerDO createTest(List<Questionbank> list, Answer answer) {
        QuestionAnswerDO questionAnswerDO = new QuestionAnswerDO();
//        System.out.println(answer.getTestid());
        questionAnswerDO.setTestid(answer.getTestid());
        questionAnswerDO.setSenderId(answer.getSenderId());
        String idanswer = answer.getIdanswer();
        Gson gson = new Gson();
        Type listType = new TypeToken<List<AnswerVo>>(){}.getType();
        List<AnswerVo> dataList = gson.fromJson(idanswer,listType);

        List<QuestionanswerVo> list1 = new ArrayList<>();
        for(Questionbank questionbank : list){
            QuestionanswerVo questionanswerVo = new QuestionanswerVo();
            questionanswerVo.setId(questionbank.getId());
            questionanswerVo.setContent(questionbank.getContent());
            questionanswerVo.setScore(questionbank.getScore());
            questionanswerVo.setDifficulty(questionbank.getDifficulty());
            questionanswerVo.setAnalysis(questionbank.getAnalysis());
            questionanswerVo.setAnswer(questionbank.getAnswer());
            String chance = questionbank.getChance();
            List<Map<String,String>> listMap = gson.fromJson(chance,List.class);
            questionanswerVo.setChance(listMap);
            questionanswerVo.setType(iQuestionTypeService.getQuestiontype(questionbank.getQuestiontype()));

//             使用流和 Lambda 表达式查找具有特定键的 HashMap
            Optional<AnswerVo> result = dataList.stream()
                    .filter(answerVo -> {
                        if(answerVo.getId()==questionbank.getId()){
                            return  true;
                        }
                        return false;
                    })
                    .findFirst();
            // 检查是否找到匹配的 HashMap
            String idanswer1 = "";
            if(result.isPresent()){
                AnswerVo answerVo = result.get();
                questionanswerVo.setIdanswer(answerVo.getAnswer());
                idanswer1 = answerVo.getAnswer();
            }else{
                questionanswerVo.setIdanswer(null);
            }
            if( questionbank.getQuestiontype()==1||questionbank.getQuestiontype()==2||questionbank.getQuestiontype()==3
                    ||questionbank.getQuestiontype()==5){
                String answer1 = questionbank.getAnswer();
                if(answer1.equals(idanswer1)){
                    questionanswerVo.setTf("true");
                }else{
                    questionanswerVo.setTf("false");
                }
            }
            list1.add(questionanswerVo);
        }
        questionAnswerDO.setList(list1);
        return questionAnswerDO;
    }

    @Override
    public int addTest(QuestionAnswerDO questionAnswerDO) {
        Questionanswer questionanswer = new Questionanswer();
        questionanswer.setSenderId(questionAnswerDO.getSenderId());
        questionanswer.setTestid(questionAnswerDO.getTestid());
        List<QuestionanswerVo> list = questionAnswerDO.getList();
        Gson gson =  new Gson();
        String qa = gson.toJson(list);
        questionanswer.setQa(qa);
        return questionanswerMapper.addQuestionanswer(questionanswer);
    }

    @Override
    public QuestionAnswerDO selectQuestionanswer(Long senderId, Long id) {
        LambdaQueryWrapper<Questionanswer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Questionanswer::getSenderId,senderId);
        queryWrapper.eq(Questionanswer::getTestid,id);
        queryWrapper.orderByDesc(Questionanswer::getCreatetime).last("LIMIT 1");
        Questionanswer questionanswer = questionanswerMapper.selectOne(queryWrapper);
        String qa = questionanswer.getQa();
        Gson gson = new Gson();
        Type listType = new TypeToken<List<QuestionanswerVo>>(){}.getType();
        List<QuestionanswerVo> questionAnswerList = gson.fromJson(qa, listType);
        QuestionAnswerDO questionAnswerDO = new QuestionAnswerDO();
        questionAnswerDO.setSenderId(senderId);
        questionAnswerDO.setTestid(questionanswer.getTestid());
        questionAnswerDO.setList(questionAnswerList);
//        System.out.println(questionAnswerDO);
        return questionAnswerDO;
    }

    @Override
    public int markObjectiveQuestions(QuestionAnswerDO questionAnswerDO) {
        Questionanswer questionanswer = new Questionanswer();
        questionanswer.setTestid(questionAnswerDO.getTestid());
        List<QuestionanswerVo> list = questionAnswerDO.getList();
        Gson gson = new Gson();
        String  qa = gson.toJson(list);
        questionanswer.setQa(qa);
        questionanswer.setSenderId(questionAnswerDO.getSenderId());
        return questionanswerMapper.addQuestionanswer(questionanswer);
    }
}
