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.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;

    @Override
    public QuestionAnswerDO createTest(List<Questionbank> list, Answer answer) {
        QuestionAnswerDO questionAnswerDO = new QuestionAnswerDO();
//        System.out.println(answer.getTestid());
        questionAnswerDO.setSender(answer.getSender());
        questionAnswerDO.setTestid(answer.getTestid());
        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());

//             使用流和 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.setSender(questionAnswerDO.getSender());
        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(String sender, Long id) {
        LambdaQueryWrapper<Questionanswer> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Questionanswer::getSender,sender);
        queryWrapper.eq(Questionanswer::getTestid,id);
        queryWrapper.orderByDesc(Questionanswer::getCreatetime).last("LIMIT 1");
        Questionanswer questionanswer = questionanswerMapper.selectOne(queryWrapper);
        String qa = questionanswer.getQa();
//        System.out.println(qa);
        Gson gson = new Gson();
        List<QuestionanswerVo> dataList = gson.fromJson(qa,List.class);
        QuestionAnswerDO questionAnswerDO = new QuestionAnswerDO();
        questionAnswerDO.setSender(questionanswer.getSender());
        questionAnswerDO.setTestid(questionanswer.getTestid());
        questionAnswerDO.setList(dataList);
//        System.out.println(questionAnswerDO);
        return questionAnswerDO;
    }

    @Override
    public int markObjectiveQuestions(QuestionAnswerDO questionAnswerDO) {
        Questionanswer questionanswer = new Questionanswer();
        questionanswer.setSender(questionAnswerDO.getSender());
        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);
    }
}
